tensorflow改写为pytorch的方法总结

替换方法:


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)
 

你可能感兴趣的:(深度学习)