最近在融合两份代码时,发现一份代码使用的是tensorflow而自己的代码使用的是pytorch框架。在转换过程中使用到的一些函数替换方法做一个简单的记录。
名称 | TensorFlow | pytorch | np |
二维卷积 | tf.nn.conv2d(input,w, strides=[1,1,1,1], padding='SAME') |
torch.nn.Conv2d(in_channels,mid_channels, kernel_size=(3,3),strides=(1,1),padding=(1,1)) |
|
生成张量(1的矩阵) | tf.ones(shape=(1,1,1,1), dtype=tf.float32) |
torch.ones(1,1,1,1) |
|
创建张量 |
tf.constant(x) |
torch.tensor(x) |
|
relu激活函数 | tf.nn.relu(x) | torch.nn.ReLU(x) | |
填充函数 | 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) | |
softmax | tf.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) | |
去除维度1 | tf.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) | |
重复 | tf.tile() | input_.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) |
|
增加一个维度 | tf.expand_dims |
np.expand_dims |
|
比较两个张量取最大值 | tf.maximum(input_x) |
np.maximum (input_x) |
|
tf.transpose(input,[1, 0, 2]) |
input.permute([1, 0, 2]) |
||
tf.range(10) |
torch.arange(0) |
||
tf.reduce_sum(x, axis=1, keep_dims=True) |
torch.sum(x,dim=1,keepdim=True) |
||
tf.clip_by_value(x, min, max) |
torch.clamp(x, min, max) |
||
tf.multinomial(logits=a, num_samples=1 |
torch.multinomial(input=a, num_samples=1, replacement=False) |
||
tf.equal(x, y) |
torch.eq(x, y) |
||
tf.nn.embedding_lookup(W_fe, Feature_input + 1) |
torch.index_select(W_fe, 0, Feature_input + 1) |
||
tf.one_hot() |
functional.one_hot() |
部分举例
1、tf.maximum和np.maximum等价
2、torch.nn.Con2d 和 tf.nn.conv2d 的互相转换
torch:
data_c=padded[:,:,:,channel_idx:(channel_idx+1)]
conv2d=torch.nn.Conv2d(kernel_i[2],kernel_i[3],kernel_size=(kernel_i[0],kernel_i[1]),stride=(1,1),padding=(0,0))
data_c=conv2d(data_c)
tf:
data_c=padded[:,:,:,channel_idx:(channel_idx+1)]
data_c=tf.nn.conv2d(data_c,kernel_i,[1,1,1,1],'VALID')
3、tf.pad和torch.nn.functional.pad的相互转换
tf: padded=tf.pad(img,[[0,0],[pad_w,pad_w],[pad_w,pad_w],[0,0]],mode='REFLECT')
Torch: pad_num=(0,0,pad_w,pad_w,pad_w,pad_w,0,0)
padded=torch.nn.functional.pad(img,pad_num,mode='REFLECT')
4、tf.tile和torch.repeat的使用是一样的
tx_1=tf.tile(tx,[1,1,1,3])
tx_1=tx(1,1,1,3)
5、tf.newaxis 给tensor增加维度
在torch中增加tensor维度的方法
(1) 使用None来增加维度
diff = X[:, None, :] - X[None, :, :]
diff
(2) 使用unsqueeze增加维度
diff = X.unsqueeze(1) - X.unsqueeze(0)
diff
(3) 使用einpos.rearrange增加维度
from einops import rearrange
diff = rearrange(X, 'i j -> i () j') - rearrange(X, 'i j -> () i j')
diff输出都是:
6、tf.cast() 转 x.type() or x.to()
x=tf.cast(tf.range(-radius,radius+1),dtype=dtype)
x=torch.range(-radius,radius+1).type(dtype=dtype)
7、torch.tensor和tf.constant 相互转换
张量的基本操作——创建张量
在Python对象上创建,例如:列表, TensorFlow版本如下所示:
PyTorch版本如下所示:
后续如果有类似的应用会继续补充。
-----2023.02.03
参考:1、https://blog.csdn.net/strawberry47/article/details/125100924
2、记录一下,tensorflow2.0和torch的函数移植对比_Tony Einstein的博客-CSDN博客_tensorflow torch