paddle复现pytorch踩坑(四):Tensor

paddlepaddle从1.5版本以上采用动态图的思想,本博客基于1.8.0以上版本。
关于Tensor的用法:

  • pytorch可以使用静态数组索引;可以使用tensor索引
  • paddlepaddle可以使用静态数组索引;不可以使用tensor索引

示例1:索引示例

# 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
  • 其中
    activate = tensor [1,… 0, 1…0]
    clc = tenor.shape [300, 4]
  • 原因:在pytorch里可以这样写,但是在paddle里不行
  • 改为:利用nonzero 和 gather函数
# 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)

示例2

# 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)

示例3

# 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))

示例4

# 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))

示例5:不能维度直接赋值

报错:

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
])

示例6:维度报错

报错:

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])

示例7:tensor的值不能直接利用

报错: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()

Tensor数据类型判断

# 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]

需要注意pytorch中.contiguous方法

.contiguous()方法,使tensor的元素在内存空间中连续
通常

tensor.contiguous().view()
==
tensor.reshape()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值