Tensorflow与Pytorch互换(2)

OK,果然容易忘记。
好记性不如烂笔头啊好记性不如烂笔头啊好记性不如烂笔头啊
1.tensorflow中get_shape()改为Pytorch对应的函数
①tensor.get_shape()本身获取tensor的维度信息并以元组的形式返回,由于元组内容不可更改,故该函数常常跟.as_list()连用,返回一个tensor维度信息的列表,以供后续操作使用。
②Numpy 里,V.shape 得到整型元组类型的 V 的维度。
③ PyTorch 里,V.size() 得到对象
torch.Size 对象为 Python 列表:

2.tf.concat()在pytorch对应
在pytorch中,常见的拼接函数主要是两个,分别是:
torch.stack() 函数stack()对序列数据内部的张量进行扩维拼接,指定维度由程序员选择、大小是生成后数据的维度区间。通常为了保留–[序列(先后)信息] 和 [张量的矩阵信息] 才会使用stack。
torch.cat()
3.tf.expand_dims()对应
unsqueeze()函数起升维的作用,参数表示在哪个地方加一个维度。。
squeeze:删减维度 与unsqueeze作用刚好相反。
4.tf.tile()
tile(a1,a2,…,an)
通过复制原张量中的第i轴的向量并填充将第i轴向量个数扩大为原本的ai倍

tf.tile和torch.repeat的使用是一样的. 在翻译项目的时候,直接替换即可.
5.tf.matmul()对应
①torch.mm() 当torch.mm用于大于二维时将报错。
m1 (nxm)
m2 (mxp) ->(nxp)
②torch.bmm()
m1(bxnxm)
m2(bxmxp) ->(bxnxp)
③matmul()

点乘:torch.mul(a, b)
矩阵乘:矩阵相乘有torch.mm和torch.matmul两个函数
6.tf.multiply()对应
首先区分tf.multiply()和tf.matmul()的区别
tf.multiply()是点乘 对应的torch.mul(tensorA, tensorB)
7.tf.transpose()

permute(dims)
a (shape:[1,2,3])
a.permute(2,0,1) ->(shape:[3,1,2])

8.tf.sequence_mask()
def sequence_mask(self,lengths, max_len=None):
lengths_shape = lengths.shape # torch.size() is a tuple
lengths = lengths.reshape(-1)

    batch_size = lengths.numel()
    max_len = max_len or int(lengths.max())
    lengths_shape += (max_len,)

    return (torch.arange(0, max_len, device=lengths.device)
            .type_as(lengths)
            .unsqueeze(0).expand(batch_size, max_len)
            .lt(lengths.unsqueeze(1))).reshape(lengths_shape)

9.tf.nn.softmax()
tf.nn.functional.softmax(x,dim = -1)中的参数dim是指维度的意思,设置这个参数时会遇到0,1,2,-1等情况

import torch.nn.functional as F
F.softmax()

10.tf.nn.relu()
import torch.nn.functional as F
F.relu(input)
激活函数都类似
tensorflow
tf.sigmoid()
tf.tanh()
tf.nn.relu()
tf.nn.softmax()
pytorch
import torch.nn.functional as F
F.sigmoid()
F.tanh()
F.relu()
F.softmax()

11.tf.einsum()

12 tf.nn.dropout

tf.nn.dropout(joined_feature, keep_prob=self.dropout_keep_prob)
pytorch:
self.dropout = nn.Dropout(args.dropout_keep_prob)
joined_feature = self.dropout(joined_feature)

13.全连接
tensorflow中:
full_out = _layers.fully_connected(joined_feature, hidden_output_size,
activation_fn=tf.nn.relu,
reuse=False,
trainable=True,
scope=“projected_layer”)

14tf.argmax()
torch.argmax(input)函数解析
输入input张量,无论有几维,首先将其reshape排列成一个一维向量,然后找出这个一维向量里面最大值的索引。

15.tf.equal()
torch.equal()
torch.eq()

16.tf.cast()

17.tf.reduce_mean ()
torch.mean()

18.关于学习率衰减 tf.train.exponential_decay()
tf.train.exponential_decay(
learning_rate, # 初始学习率
global_step, #
decay_steps, # 衰减步长
decay_rate, # 衰减率
staircase=False #
)
对应Pytorch
torch.optim.lr_scheduler.StepLR(optimizer,step_size=100,gamma=0.99,last_epoch=-1)
optimizer:自己定义的优化器
step_size:衰减步长,即每隔step_size个epoch,更新一次学习率
gamma(float):衰减系数
last_epoch(int):最后一次epoch的索引默认为-1

optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=100, gamma=0.99)

19.tf.constant(initializer, name=“word_embedding”)
在tensorflow中,可以通过tf.consatnt()和tf.Variable()来建立张量
与pytorch旧版本类似的是,tf.constant()对应torch.Tensor(),tf.Variable()对应torch.Variable(),tf.constant创建的是常数,tf. Variable创建的是变量

20.多分类任务中的交叉熵损失和均方损失对比
均方损失:均方损失下,对于同样的样本【0 0 1 0 0】,除了目标类别的损失外,非目标类别同样引入损失
loss_mse = nn.MSELoss()
交叉熵损失:对于多分类场景,假设某一样本的one-hot标签为【0 0 1 0 0】,那么从交叉熵损失的公式可以看出,交叉熵损失只关注目标类别带来的损失
criterion = nn.CrossEntropyLoss()

21.tf.train.latest_checkpoint()
自动寻求最近的checkpoint()

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值