tensorflow与pytorch的移植转换函数对比表

相信有了这份表格对比,tensorflow与pytorch的基本移植转换,应该是手到擒来。

名称tensorflowpytorch
二维卷积tf.nn.conv2d(input_x, w, strides=[1, 1, 1, 1], padding='SAME')torch.nn.Conv2d(in_channels, mid_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
relu激活函数tf.nn.relu(input_x)torch.nn.ReLU()
填充函数tf.pad(input_x, [(a0,b0), (a1,b1), (a2,b2), (a3,b3)])

pad_num = (a3,b3, a2, b2, a1, b1, a0, b0)

torch.nn.functional.pad(input_x, pad_num, mode='constant')

元素个数tf.size(input_x)torch.numel(input_x)
展平tf.reshape(input_x, (tf.size(input_x), -1))input_x.view(torch.numel(input_x), -1)
softmaxtf.nn.softmax(input_x, axis=1)torch.nn.functional.softmax(input_x, dim=1)
调整类型tf.cast(input_x, tf.int32)input_x.type(torch.LongTensor)
除去维度为1tf.squeeze(input_x, squeeze_dims=1)torch.squeeze(input_x)
合并tf.concat((input_x1, input_x2), axis=3)torch.cat((input_x1, input_x2), dim=3)
划分成相同维度的块tf.split(input_x, axis=3, num_or_size_splits=2)torch.chunk(input_x, dim=3, chunks=2)
产生1的矩阵tf.ones((a,b))torch.ones(a,b)
重复tf.tile()input_x.repeat()
交换维度tf.transpose(input_x, [0,1,2,3])input_x.permute((0,1,2,3))  注意这种用法:p01.permute((0, 2, 3, 1)).contiguous().view(int(np.prod(shape01)), -1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值