Pytorch与tensorflow转换

替换方法:

matrix = tf.get_variable(“Matrix”, [output_size, input_size], dtype=input_.dtype)

matrix = Variable(torch.randn(output_size, input_size))

self.D_l2_loss = tf.constant(0.0)

self.D_l2_loss = torch.Tensor(0.0)

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)

tf.reduce_mean(losses)

torch.mean(losses)

embedded_chars = tf.nn.embedding_lookup(W_fe, Feature_input + 1)

embedded_chars = torch.index_select(W_fe, 0, Feature_input + 1)

embedded_chars_expanded = tf.expand_dims(embedded_chars, -1)

embedded_chars_expanded = torch.unsqueeze(embedded_chars, -1)

h_pool = tf.concat(pooled_outputs, 3)

h_pool = torch.cat(pooled_outputs, 3)

tf.transpose(matrix)

matrix.transpose(1, 0)

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

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

torch.matmul

tf.matmul

tf.expand_dims

torch.unsqueeze

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]

tf.squeeze()

torch.squeeze

16,
sub_goal = tf.nn.l2_normalize(sub_goal, 1)

sub_goal = sklearn.preprocessing.normalize(sub_goal , norm=‘l2’)

tf.log(tf.nn.softmax()

F.log_softmax()

tf.multinomial(log_prob, 1)

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

tf.multiply

*或者torch.mul

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

tf.reduce_sum(a, 0)

a.sum(0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值