paddlepaddle从1.5版本以上采用动态图的思想,本博客基于1.8.0以上版本。
关于Tensor的用法:
# pytorch code
aa = cls[active, :]
# paddlepaddle code
aa = cls[active, :]
报错:
SystemError: <built-in method __getitem__ of PyCapsule object at 0x0000023217C75F90> returned a result with an error set
# paddlepaddle code
index = fluid.layers.nonzero(active)
aa = fluid.layers.gather(cls, index)
测试全代码
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
import numpy as np
data = np.ones([300, 4]).astype('float32')
index = np.zeros([300]).astype('int')
index[0] = 1
index[2] = 1
index[10] = 1
with fluid.dygraph.guard():
data = to_variable(data)
index = to_variable(index)
index = fluid.layers.nonzero(index)
data = fluid.layers.gather(data, index)
# data = data[index, :]
print(data.shape)
# pytorch code
active = labels_weight > 0
y = bbox_x[active]
# paddlepaddle code
active = labels_weight > 0
index = fluid.layers.nonzero(active)
y = fluid.layers.concat([fluid.layers.reshape(fluid.layers.gather(bbox_x[i, :], index[i, :]), [1, -1]) for i in range(index.shape[0])], axis=0)
# pytorch code
loss_bbox_y = fluid.layers.mean(loss_bbox_y * bbox_weights[active])
# paddlepaddle code
loss_bbox_y = fluid.layers.mean(fluid.layers.cast(loss_bbox_y, 'float64')
* fluid.layers.concat([fluid.layers.reshape(fluid.layers.gather(bbox_weights[i, :], index[i, :]), [1, -1]) for i in range(index.shape[0])], axis=0))
# pytorch code
bbox_x3d_dn_fg = bbox_x3d_dn[bind, fg_inds]
# paddlepaddle code
bbox_x3d_dn_fg = fluid.layers.gather(bbox_x3d_dn[bind], fluid.dygraph.to_variable(fg_inds))
报错:
TypeError: 'paddle.fluid.core_avx.VarBase' object does not support item assignment
# pytorch code
Pred_boxes[:, 0] = pred_ctr_x - 0.5 * pred_w
pred_boxes[:, 1] = pred_ctr_y - 0.5 * pred_h
pred_boxes[:, 2] = pred_ctr_x + 0.5 * pred_w
pred_boxes[:, 3] = pred_ctr_y + 0.5 * pred_h
# paddlepaddle code
pred_boxes = fluid.layers.concat([
pred_ctr_x - 0.5 * pred_w,
pred_ctr_y - 0.5 * pred_h,
pred_ctr_x + 0.5 * pred_w,
pred_ctr_y + 0.5 * pred_h
])
报错:
too many indices (3) for tensor of dimension 2
# pytorch code
bbox_x[bind, :, np.newaxis ]
# paddlepaddle code
fluid.layers.reshape(bbox_x[bind, :], [1, -1, 1])
报错:paddlepaddle中的value不能直接拿出来用。
TypeError: The type of 'shape' in reshape must be list[int] or tuple(int) in Dygraph mode, but received <class 'list'>, which contains Variable.
错误代码:其中stack_size, feat_size 为 tensor。
# paddlepaddle code
shift_x1 = fluid.layers.reshape(fluid.dygraph.to_variable(shift_x1), [1, stack_size, feat_size[1]])
改进加入
# paddlepaddle code
stack_size = stack_size.numpy()
feat_size = feat_size.numpy()
# pytorch code
if data_type == torch.tensor:
# paddlepaddle code
if data_type == fluid.core_avx.VarBase:
# pytorch code
b = q_lt[..., :N]
# paddlepaddle code
b = q_lt[:, :, :, :N]
.contiguous()方法,使tensor的元素在内存空间中连续
通常
tensor.contiguous().view()
==
tensor.reshape()