torch的一些函数

0. 长期更新,看到奇怪的弄懂了就写

ref:
https://www.bilibili.com/video/BV1wQ4y1q7Bm/?spm_id_from=333.788
https://blog.csdn.net/m0_46384757/article/details/120509481

1.torch.gather

这个的效果是切片

https://www.jianshu.com/p/b7d8d3c26f2d
上面的文章告诉我举例子的时候不要用1m或m1
也不要用n*n这种,不然歧义很多

这里是按我的理解写一遍,为了我能看懂

torch的一些函数_第1张图片
torch的一些函数_第2张图片
补充,如果你出现了**Dimension out of range (expected to be in range of [-2, 1], but got 2)**之类的报错,这是你的index的维度和gather源的维度不同,下面是正确的index,注意index的括号数

在这里插入图片描述

1.1 模糊的官方文档的解释

https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/#math-operations
torch的一些函数_第3张图片
敲过之后感觉还是能看的就是对初学者不太友好

#假设输入的是一个三维的张量
#比如 对 
# index=[
#   [[0,1],[2,3]],
#   [[4,5],[6,7]]
# ]
#其中5对应的坐标就是(101--(i.j.k)
#下面说的i,j,k都指的是index中某一数字在index中的坐标
#记 z=index(i,j,k)
# output   <-- (z, j,k) if dim=0
# output   <-- (i, z, k) if dim=1
# output   <-- (i, j, z) if dim=2

2.torch.transpose

这个的效果真的是坐标对换
比如说, 敲了个 torch.transpose(x,0,1), 第一维与第三维调换,原本(2,3,4)的数值出现在了(4,3,2)的位置上
torch的一些函数_第4张图片

3. torch.unbind(x,dim=0)

这个的效果是沿着某一维度得到原张量x的切片

torch的一些函数_第5张图片

4.torch.autograd.Function

推荐直接看这个
https://zhuanlan.zhihu.com/p/344802526

4.1 实战例子

首先记录一下这里想说的是apply()里面不是传入function的情况,function的话可以看下这个https://blog.csdn.net/qq_37025073/article/details/106739513

class Exp(Function):

    @staticmethod
    def forward(ctx, i):
        result = i.exp()
        ctx.save_for_backward(result)
        return result

    @staticmethod
    def backward(ctx, grad_output):
        result, = ctx.saved_tensors
        return grad_output * result

#Use it by calling the apply method:
output = Exp.apply(input)

# forward()和backward()都应该是staticmethod。
# forward()的输入只有2个(ctx, i),ctx必须有,i是input。
# ctx.save_for_backward(result)表示forward()的结果要存起来,以后给backward()。
# backward()的输入只有2个(ctx, grad_output),ctx必须有,grad_output是最终object对的forward()输出的导数。
# result, = ctx.saved_tensors得到之前forward()存的结果。
# 因为 exp(x) 的导数还是 exp(x) ,所以return grad_output (上一层求导结果)* result(exp(x))。
# 调用时,直接 Exp.apply(input)即可。

torch的一些函数_第6张图片
实验结果如上,functiion.apply(input)会自动调用forward,但不会子弟哦那个调用backward

上面这部分代码来自于【2】,你可以去【1】看BNN的实战例子

4.2 ref

[1]http://giantpandacv.com/project/%E9%83%A8%E7%BD%B2%E4%BC%98%E5%8C%96/AI%20%E9%83%A8%E7%BD%B2%E5%8F%8A%E5%85%B6%E5%AE%83%E4%BC%98%E5%8C%96%E7%AE%97%E6%B3%95/%E5%9F%BA%E4%BA%8EPytorch%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%8F%AF%E8%AE%AD%E7%BB%83%E7%9A%84BNN/#23
[2]https://zhuanlan.zhihu.com/p/344802526

5. le 与 ge

暂时认为是比大小

input=torch.arange(15)*0.2
print(input)
>>tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000, 1.0000, 1.2000, 1.4000, 1.6000,
        1.8000, 2.0000, 2.2000, 2.4000, 2.6000, 2.8000])
print(input.ge(1))
>>tensor([False, False, False, False, False,  True,  True,  True,  True,  True,
         True,  True,  True,  True,  True])
print(input.le(1))
>>tensor([ True,  True,  True,  True,  True,  True, False, False, False, False,
        False, False, False, False, False])

6.torch.min

这里主要讲的是它的维度问题

# 一般是输入像这种torch.min(input, dim=3, keepdim=True)
# 这里想说的是第二个位置的数字
# 比如你输入的是一个size为[2,3,4,4] 
#  dim=0--2,dim=1--3

# 但是其实我想讲的重点是channel-wise,layer-wise那些该怎么处理
# layer-wise--->
# torch.min(input)

# channel-wise-->
# size(input)=[2,3,4,4]
# torch.min(torch.min(torch.min(input, 3, keepdim=True)[0], 2, keepdim=True)[0], 1, keepdim=True)[0]
# [0]是结果,[1]是对应的位置
# 此处dim=从内到外为321 表示层层选择,从size(input)=[2,3,4,4]最右的43
# 最里面的dim=3表示对每行
# dim=2表示对每一个4*4的平面
# dim=1 表示对于每一个由三个平面搭成的filter

torch的一些函数_第7张图片

7.register_buffer

这个是用来生成一个缓存器的,我在定义一个model类的时候看到这个,
在model中,我们需要更新其中的参数,训练结束将参数保存下来。但在某些时候,我们可能希望模型中的某些参数参数不更新(从开始到结束均保持不变),但又希望参数保存下来,就用这个

# 用法
class ...
	def....
		self.register_buffer('my_buffer', self.tensor)
# 表明将 创建一个叫my_buffer的缓存器(通过self.mybuffer调用),里面复制粘贴了#self.tensor

7.1 register_buffer.add_()

对这个buffer中的每一个元素都进行+1的操作

import torch
import torch.nn as nn
class my_model(nn.Module):
    def __init__(self,out_channels):
        super(my_model, self).__init__()
        self.register_buffer('mybuff', torch.zeros(out_channels, 1, 1, 1))

    def a(self):
        self.mybuff.add_(1)


A=my_model(5)
A.a()
print((A.mybuff))

效果:
torch的一些函数_第8张图片

8.torch.nn.embedding

作用:构建一个词向量库,认为每一行为对应编号的单词

#测试代码
mport torch
import torch.nn as nn
from torch.autograd import Variable

# 10个size为3的tensor
# 嵌入字典的大小为10(即有10个词),每个词向量的维度为3
embedding = nn.Embedding(10, 3)
# a batch of 2 samples of 4 indices each
# 该LongTensor的数字范围只能在0~9(因为设置了10)
print(embedding.weight)
print('-' * 60)
input1 = torch.LongTensor([[1, 2, 4, 5], 
                           [4, 3, 2, 9]])
emb1 = embedding(input1) # tensor([[1, 0, 2, 2, 3]])
print(emb1)
print(emb1.shape)
# torch.Size([2, 4, 3]) # 即每个词都被映射为一个3维向量
print('-' * 60)

torch的一些函数_第9张图片

你可能感兴趣的:(pytorch,pytorch)