tensorflow改写为pytorch的方法总结

本文提供了一套从TensorFlow迁移到PyTorch的实用转换方法,详细对比了两个框架中的相似功能,如变量定义、损失计算、张量操作等,并介绍了TensorArray的使用技巧。

替换方法:


1.
matrix = tf.get_variable("Matrix", [output_size, input_size], dtype=input_.dtype)
=
 matrix = Variable(torch.randn(output_size, input_size))

 

2.
self.D_l2_loss = tf.constant(0.0)
=
  self.D_l2_loss = torch.Tensor(0.0)

 

3.
 losses = tf.nn.softmax_cross_entropy_with_logits(logits=D_scores, labels=self.D_input_y)
=
losses = torch.nn.CrossEntropyLoss(D_scores, D_input_y)

 

4.
 tf.reduce_mean(losses)
=
torch.mean(losses)

 

5.
embedded_chars = tf.nn.embedding_lookup(W_fe, Feature_input + 1)
=
   embedded_chars = torch.index_select(W_fe, 0, Feature_input + 1)


6.
embedded_chars_expanded = tf.expand_dims(embedded_chars, -1)
=
embedded_chars_expanded = torch.unsqueeze(embedded_chars, -1)


7.
 h_pool = tf.concat(pooled_outputs, 3)
=
 h_pool = torch.cat(pooled_outputs, 3)

 

8.
tf.transpose(matrix)
=
matrix.transpose(1, 0)

 

9.
TensorArray可以看做是具有动态size功能的Tensor数组。通常都是跟while_loop或map_fn结合使用。

 

10.tf.TensorArray简单教程    https://blog.csdn.net/z2539329562/article/details/80639199

ta.stack(name=None) 将TensorArray中元素叠起来当做一个Tensor输出
ta.unstack(value, name=None) 可以看做是stack的反操作,输入Tensor,输出一个新的TensorArray对象
ta.write(index, value, name=None) 指定index位置写入Tensor
ta.read(index, name=None) 读取指定index位置的Tensor

 

11.
torch.squeeze
=?????不确定
ta.stack


12.
torch.matmul
=
tf.matmul


13.
tf.expand_dims
=
torch.unsqueeze


14.
torch.split( torch.cat([torch.split(input_x, i, 1)[0], self.padding_array], 1),
                     self.sequence_length, 1)[0]
=有一点不一样
tf.split(tf.concat([tf.split(input_x, [i, self.sequence_length - i], 1)[0], self.padding_array], 1),[self.sequence_length, i], 1)[0]


14.
tf.squeeze()
=
torch.squeeze


16,
sub_goal = tf.nn.l2_normalize(sub_goal, 1)
=
sub_goal = sklearn.preprocessing.normalize(sub_goal , norm='l2')


17.
tf.log(tf.nn.softmax()
=
F.log_softmax()

 

18.
tf.multinomial(log_prob, 1)
=
 torch.multinomial(log_prob, 1,  replacement=True)

 

19.
tf.multiply
=
*或者torch.mul


20.
tf.one_hot
=???
onehot_encoder = OneHotEncoder(sparse=False)
onehot_encoder.fit_transform(next_token)


21.
tf.reduce_sum(a, 0)
=
a.sum(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值