pytorch初学者的函数收集手册。(一)

6 篇文章 0 订阅

这些函数都在pytorch的中文手册中有,但是我把我用到的会记录下来。

ps:这里面有些东西是复制了其他博主的,目的只是方便我以后查阅,如果侵权了,请联系我删除,万分抱歉。


1. torch.mm()

torch.mm(mat1, mat2, out=None) → Tensor
#按照矩阵的乘法相乘;例如a是mXn的,bshi nXc的那么得到的就是mXc的一个矩阵
“”“
>>> mat1 = torch.randn(2, 3)
>>> mat2 = torch.randn(3, 3)
>>> torch.mm(mat1, mat2)
 0.0519 -0.3304  1.2232
 4.3910 -5.1498  2.7571
[torch.FloatTensor of size 2x3]”“”

2. torch.clamp()

torch.clamp(input, min, max, out=None) → Tensor
将输入input张量每个元素的夹紧到区间 [min,max],并返回结果到一个新张量。

 


3. torch.t()

torch.t(input, out=None) → Tensor
输入一个矩阵(2维张量),并转置0, 1维。 可以被视为函数transpose(input, 0, 1)的简写函数。

"""
>>> x = torch.randn(2, 3)
>>> x

 0.4834  0.6907  1.3417
-0.1300  0.5295  0.2321
[torch.FloatTensor of size 2x3]

>>> torch.t(x)

 0.4834 -0.1300
 0.6907  0.5295
 1.3417  0.2321
[torch.FloatTensor of size 3x2]


"""

 


4. torch.from_numpy()

torch.from_numpy(ndarray) → Tensor

>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
torch.LongTensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1,  2,  3])

5. torch.rand和torch.randn

 

torch.rand(*sizes, out=None) → Tensor

返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。

参数:

    sizes (int...) - 整数序列,定义了输出张量的形状
    out (Tensor, optinal) - 结果张量

例子:

    torch.rand(2, 3)
    0.0836 0.6151 0.6958
    0.6998 0.2560 0.0139
    [torch.FloatTensor of size 2x3] 

torch.randn(*sizes, out=None) → Tensor

返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。

参数:

    sizes (int...) - 整数序列,定义了输出张量的形状
    out (Tensor, optinal) - 结果张量

6. tensor的索引与切片

索引:
#1.pytorch风格:
import torch
 
a = torch.rand(4, 3, 28, 28)
print(a[0].shape) #取到第一个维度
print(a[0, 0].shape) # 取到二个维度
print(a[1, 2, 2, 4])  # 具体到某个元素
"""
上述代码创建了一个shape=[4, 3, 28, 28]的Tensor,我们可以理解为4张图片,每张图片有3个通道,每个通道是28x28的图像数据。a代表这个Tensor,a后面跟着的列表[]表示对Tensor进行索引,a的维度dim = 4,决定了[]中的元素个数不能超过4个,[]中的值表示对应维度上的哪一个元素,比如 a[0]表示取第一个维度上的第一个元素,可以理解为第一张图片,a[1]表示取第一个维度上的第二个元素,可以理解为第二张图片。a[0, 0]表示取第一个维度上第一个元素的与第二个维度上的第一个元素,也就是第一张图片第一个通道的元素。a[1, 2, 2, 4]表示取第第一个维度上的第二个元素与第二个维度上的第三个元素与第三个维度上的第三个元素与第四个维度上的第5个元素,也就是第二张图片第三个通道第三行第四列的像素值是一个标量值。
"""
输出:
torch.Size([3, 28, 28])
torch.Size([28, 28])
tensor(0.1076)
***************************************************************

#2.python
import torch
 
# 譬如:4张图片,每张三个通道,每个通道28行28列的像素
a = torch.rand(4, 3, 28, 28)
 
# 在第一个维度上取后0和1,等同于取第一、第二张图片
print(a[:2].shape)  
 
# 在第一个维度上取0和1,在第二个维度上取0,
# 等同于取第一、第二张图片中的第一个通道
print(a[:2, :1, :, :].shape)  
 
# 在第一个维度上取0和1,在第二个维度上取1,2,
# 等同于取第一、第二张图片中的第二个通道与第三个通道
print(a[:2, 1:, :, :].shape) 
 
# 在第一个维度上取0和1,在第二个维度上取1,2,
# 等同于取第一、第二张图片中的第二个通道与第三个通道
print(a[:2, -2:, :, :].shape)  
 
# 使用step隔行采样
# 在第一、第二维度取所有元素,在第三、第四维度隔行采样
# 等同于所有图片所有通道的行列每个一行或者一列采样
# 注意:下面的代码不包括28
print(a[:, :, 0:28:2, 0:28:2].shape) 
print(a[:, :, ::2, ::2].shape)  # 等同于上面语句



**************************************************************
#3u index_select()选择特定的索引

"""
选择特定下标有时候很有用,比如上面的a这个Tensor可以看作4张RGB(3通道)的MNIST图像,长宽都是28px。那么在第一维度上可以选择特定的图片,在第二维度上选择特定的通道,在第三维度上选择特定的行等:
"""
# 选择第一张和第三张图
print(a.index_select(0, torch.tensor([0, 2])).shape)
 
# 选择R通道和B通道
print(a.index_select(1, torch.tensor([0, 2])).shape)
 
# 选择图像的0~8行
print(a.index_select(2, torch.arange(8)).shape)
注意:index_select()第二个参数必须是索引的类型
************************************************************
#4.使用索引任意多的维度
#代码:
import torch
 
a = torch.rand(4, 3, 28, 28)
 

# 第一张图片的所有维度
print(a[0, ...].shape)
 
# 所有图片第二通道的所有维度
print(a[:, 1, ...].shape)
 
# 所有图像所有通道所有行的第一、第二列
print(a[..., :2].shape)

6. .cuda() .to(device = cuda)

with torch.cuda.device(1):
    # allocates a tensor on GPU 1
    a = torch.tensor([1., 2.], device=cuda)
 
    # transfers a tensor from CPU to GPU 1
    b = torch.tensor([1., 2.]).cuda()
    # a.device and b.device are device(type='cuda', index=1)
 
    # You can also use ``Tensor.to`` to transfer a tensor:
    b2 = torch.tensor([1., 2.]).to(device=cuda)
    # b.device and b2.device are device(type='cuda', ind
#两种写法等同

7. torch.ones()

"""
之后很多都是英文
torch.ones(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
"""
>>> torch.ones(2, 3)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

8. torch.variable()

链接:https://blog.csdn.net/liuhongkai111/article/details/81291003

链接:https://blog.csdn.net/qq_37385726/article/details/81706820#1.%E7%AE%80%E4%BB%8B

注意Variable创建的时候(require_grad = true)代表更新参数


autograd根据用户对Variable的操作来构建其计算图。

  1. requires_grad
    variable默认是不需要被求导的,即requires_grad属性默认为False,如果某一个节点的requires_grad为True,那么所有依赖它的节点requires_grad都为True
  2. volatile
    variable的volatile属性默认为False,如果某一个variable的volatile属性被设为True,那么所有依赖它的节点volatile属性都为True。volatile属性为True的节点不会求导,volatile的优先级比requires_grad高。(目前已经被废弃,使用
    .volatile已经被torch.no_grad()torch.set_grad_enabled(grad_mode)替代,在0.4版本中。)
  3. retain_graph
    多次反向传播(多层监督)时,梯度是累加的。一般来说,单次反向传播后,计算图会free掉,也就是反向传播的中间缓存会被清空【这就是动态度的特点】。为进行多次反向传播需指定retain_graph=True来保存这些缓存
  4. .backward()
    反向传播,求解Variable的梯度。放在中间缓存中。

补充:现如今torch的variable已经和tensor进行了合并,下面的代码可以进行改写

#第一种方式
a = torch.tensor([1, 2, 3], requires_grad = True)
#第二种方式
a = torch.randn(1,2)
v = Variable(a, reuires_grad = True)

 

 


9. torch的四种乘法

链接:link


10. tensor.data()

获得该张量的数据的具体值。


11. tensor.view()

 

"""
在PyTorch中view函数作用为重构张量的维度,相当于numpy中的resize()的功能,但是用法不太一样
"""
import torch
tt1=torch.tensor([-0.3623,-0.6115,0.7283,0.4699,2.3261,0.1599])
#则tt1.size()为torch.Size([6]),是一个一行的tensor。现在通过view可以将其重构一下形状。
result=tt1.view(3,2)
result

tensor([[-0.3623, -0.6115],
        [ 0.7283,  0.4699],
        [ 2.3261,  0.1599]])
"""
torch.view(参数a,参数b,.....),其中参数a=3,参数b=2决定了将一维的tt1重构成3*2维的张量。
有时候会出现torch.view(-1)或者torch.view(参数a,-1)这种情况。则-1参数是需要估算的。

"""
import torch
tt3=torch.tensor([[-0.3623,-0.6115],[0.7283,0.4699],[2.3261,0.1599]])
result2=tt3.view(2,-1).contiguous()
>>>result
tensor([[-0.3623, -0.6115,  0.7283],
        [ 0.4699,  2.3261,  0.1599]])

12. torch.coutiguous()

返回一个内存连续的有相同数据的tensor,如果原tensor内存连续,则返回原tensor


13. torch.matmul()

当输入是都是二维时,就是普通的矩阵乘法,和tensor.mm函数用法相同。

 

这个例子是说先对tensor1的第0维提取,即对tensor2的第0维做torch.boradcast(),然后通过批处理让tensor1和tensor2做矩阵的乘法就行了。

>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(4, 5)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3, 5])

 


14. torch的transform类

参看链接如下:

link_1 link_2

备注: transform.transpose:

这个函数将原先的矩阵如:dXhXw 转换成wXdXh,对于图片来说就是将通道数提前,将其重新输出图片要逆变


15. transform.normalize()

比如原来的tensor是三个维度的,值在0到1之间,经过以下变换之后就到了-1到1区间。

transforms.Normalize((.5,.5,.5),(.5,.5,.5))

 


 16. ImageFolder()

参看链接:https://blog.csdn.net/weixin_40123108/article/details/85099449

                  https://www.cnblogs.com/wanghui-garcia/p/10649364.html

from torchvision import transforms as T
import matplotlib.pyplot as plt
from torchvision.datasets import ImageFolder
 
 
dataset = ImageFolder('data/dogcat_2/')
 
# cat文件夹的图片对应label 0,dog对应1
print(dataset.class_to_idx)
 
# 所有图片的路径和对应的label
print(dataset.imgs)
 
# 没有任何的transform,所以返回的还是PIL Image对象
#print(dataset[0][1])# 第一维是第几张图,第二维为1返回label
#print(dataset[0][0]) # 为0返回图片数据
plt.imshow(dataset[0][0])
plt.axis('off')
plt.show()

注意:例子中倒数第五行和倒数第四行。


17.DataLoader类

链接1:链接

链接2:链接

这里综合一下Dataset和DataLoader的理解:

DataLoader相当于一个容器,它里面存储了daraset这个东西,只能通过迭代(迭代器、for)调出他的Dataset。

for i, data in enumerate(datas):
	# i表示第几个batch, data表示该batch对应的数据,包含data和对应的labels
    print("第 {} 个Batch \n{}".format(i, data))

结果:

这说明了反映在DataLoader中的时候就是一个以批为单位的二元向量元组(tensora,tensorb)前者是数据,而后者则是标签。


18 torch.linspace()

torch.linspace(start, end, steps=100, out=None) → Tensor

返回一个1维张量,包含在区间start和end上均匀间隔的step个点。

m = nn.Linear(20, 30)

input = autograd.Variable(torch.randn(128, 20))

output = m(input)

print(output.size())

 


19. torch.nn.Linear(in_features: int, out_features: int, bias: bool = True)

Applies a linear transformation to the incoming data: y=xAT+b

Parameters

  • in_features – size of each input sample

  • out_features – size of each output sample

  • bias – If set to False, the layer will not learn an additive bias. Default: True

Shape:

  • Input: (N,∗,Hin)(N, *, H_{in})(N,∗,Hin​) where ∗*∗ means any number of additional dimensions and Hin=in_featuresH_{in} = \text{in\_features}Hin​=in_features

  • Output: (N

    ,∗,Hout)(N, *, H_{out})(N,∗,Hout​) where all but the last dimension are the same shape as the input and Hout=out_featuresH_{out} = \text{out\_features}Hout​=out_features .

 


20. torch.nn.Linear()


21.torch.linspace()

torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Returns a one-dimensional tensor of steps equally spaced points between start and end.

The output tensor is 1-D of size steps.

Parameters

  • start (float) – the starting value for the set of points

  • end (float) – the ending value for the set of points

  • steps (int) – number of points to sample between start and end. Default: 100.

  • out (Tensor, optional) – the output tensor.

  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).

  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.

  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.


22torch.nn.model()

link


23. torch.optim.lr_scheduler

link


24.tensor.item()

.item()可以显示一个张量的元素。但是这个张量必须只有一个元素

>>> a = torch.ones(2,2)
>>> a
tensor([[1., 1.],
        [1., 1.]])
>>> a.item()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: only one element tensors can be converted to Python scalars
>>> a = torch.tensor([1])
>>> a
tensor([1])
>>> a.item()
1

 


 25.torch.max

分两种情况:

1.当调用函数中只有一个参数tensor时

返回该向量中最大的元素值(以tensor的形式)。

>>> a = torch.tensor([1, 2, 3])
>>> torch.max(a)
tensor(3)

2.当调用函数中有tensor和dim时:

当dim = 1 时返回一个命名的元组,内容为{value,indice},表示每一行最大值和位置。

>>> a = torch.tensor([[1, 2, 3], [3, 5, 1]])
>>> torch.max(a, 1)
torch.return_types.max(
values=tensor([3, 5]),
indices=tensor([2, 1]))

 


26.softmax层

softmax层本质上是对一个tensor的值1.映射到0-1之间。2.确保其之和为1。

参看链接:link


27.nn.CrossEntropyloss()

参看链接:Pytorch里的CrossEntropyLoss详解

官方公式:loss(x,class) = -log(exp(x[class])/sum(x[j]))


下面谈谈我对这个交叉熵函数的理解:

  • 原理层面:

交叉熵函数的公式:

这其中表示在真实情况下的取值为的概率,表示算法所估计的取的概率。

  • 函数的实现:

交叉熵函数分三个过程:

1.对input执行softmax()函数得到input_1,可以将它看成是对目标事件的概率估计。

sofmax函数的公式:

2.对所得到的imput_1,执行log()变换得到imput_2,目标是得到对数形式。

3.对得到的imput_2,执行NLLLoss()变换,最终得到的结果就是损失。

NLLLoss()的官方解释:

这其中class代表的是label的值。

显而易见我们可以理解为,计算所有的概率,对这些概率log,然后就套用交叉熵函数的公式,但是由于一般分类问题的真实可能值就是只有一个1其它全为0,那么只用提取label中下标为1的input_2元素。


28.torch.sum()

torch.sum(tensor)返回一个只有一个元素的张量,内容为参数的和。

>>> x = torch.ones(2,3,5)
>>> torch.sum(x)
tensor(30.)

29.nn.Module()

g我觉得可以参看博主:LoveMiss_Y的文章:pytorch教程之nn.Module类详解——使用Module类来自定义模型

补充:https://blog.csdn.net/sinat_33761963/article/details/104332723


30.torch.nn.Conv2d()函数详解

import torch

x = torch.randn(2,1,7,3)
conv = torch.nn.Conv2d(1,8,(2,3))
res = conv(x)

print(res.shape)    # shape = (2, 8, 6, 1)

输入x:

[ batch_size, channels, height_1, width_1 ]

batch_size 一个batch中样例的个数2
channels 通道数,也就是当前层的深度1
height_1图片的高7
width_1图片的宽 3

Conv2d的参数

[ channels, output, height_2, width_2 ] 

channels通道数,和上面保持一致,也就是当前层的深度1
output输出的深度 8
height_2过滤器filter的高2
weight_2过滤器filter的宽 3
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
in_channels
 Number of channels in the input image
out_channels
 Number of channels produced by the convolution
kernel_size
卷积核尺寸
stride
步长,控制cross-correlation的步长,可以设为1个int型数或者一个(int, int)型的tuple。
padding
(补0):控制zero-padding的数目。
dilation
(扩张):控制kernel点(卷积核点)的间距
groups
 (卷积核个数):通常来说,卷积个数唯一,但是对某些情况,可以设置范围在1 —— in_channels中数目的卷积核:
bias
adds a learnable bias to the output.

 

 

输出res:

[ batch_size,output, height_3, width_3 ]

batch_size一个batch中样例的个数,同上2
output输出的深度8
height_3 卷积结果的高度h1-h2+1 = 7-2+1 = 6
weight_3卷积结果的宽度w1-w2+1 = 3-3+1 = 1

 


  • 补充:关于卷及神经网络的原理和某些技巧:

卷积原理的图像化表述:

参看:link

卷积中的通道:

参看:link

卷积后计算输出矩阵的大小方法:

ps:

  1. wide: 原矩阵的长度。
  2. flitter:flitter的边长。
  3. padding:当有是使用某个大小的flitter卷积的时候不能正确显示,那么要对图像进行补全,这就是给边的像素。
  4. step:每一次进行卷积之后经过step的长度对下一个元素进行卷积。


      30.tensor.data()和tensor.detach()

在Pytorch中,创建模型和数据运算传递时,经常会使用到tensor.data和tensor.detach(),对于这两种使用方式,都是对Variable中的tensor进行处理,但是都不进行梯度计算和被进行梯度跟踪,即requires_grad=False,简单来说,他们的区别如下:

相同点

两者都和原数据共享同一块数据;
都和原来数据的计算历史无关;
requires_grad = False;


不同点

detach会进行提示,相比data更安全,在使用in-place操作后,会修改原数据的值,而如果在反向传播过程中使用到原数据会导致计算错误,而使用.detach就会报错。

参考链接:link

 


31.tensor.view()

 首先,view( ) 是对 PyTorch 中的 Tensor 操作的,若非 Tensor 类型,可使用 data = torch.tensor(data)来进行转换。

(1) 作用:该函数返回一个有__相同数据__但不同大小的 Tensor。通俗一点,就是__改变矩阵维度__,相当于 Numpy 中的 resize() 或者 Tensorflow 中的 reshape() 。

(2) 参数:view( *args )data.contiguous().view(-1)

在实际操作中,我们常会看到这个用法,contiguous()就是为了保证一个Tensor是连续的,这样才能被 view() 处理。

x = torch.randn(4, 4)
print(x.size())
y = x.view(16)
print(y.size())
z = x.view(-1, 8)  # -1表示该维度取决于其它维度大小,即(4*4)/ 8
print(z.size())
m = x.view(2, 2, 4) # 也可以变为更多维度
print(m.size())

其输出结果依次为:

torch.Size([4, 4])
torch.Size([16])
torch.Size([2, 8])
torch.Size([2, 2, 4])

补充: 

view(-1):只有一个参数-1代表view()根据其他参数来确定形状。

一般情况下这种操作,会把所有的tensor转化成一个一维的tensor。

注意:在处理DataLoader的dada的时候,一个迭代的的数据有很多批,每一批又有很多张独立的数据(如:图片),我们不希望将各个图片中的数据混合,因此在扁平化的时候不能仅仅使用:view(-1),这样子会将多张图片混合到一个矩阵,因此我们使用view(-1,pic_size),来处理扁平化的工作,这一点需要格外注意

32. torch.contiguous()

contiguous:view只能用在contiguous的variable上。如果在view之前用了transpose, permute等,需要用contiguous()来返回一个contiguous copy。

原理:


在使用transpose()进行转置操作时,pytorch并不会创建新的、转置后的tensor,而是修改了tensor中的一些属性(也就是元数据),使得此时的offset和stride是与转置tensor相对应的。转置的tensor和原tensor的内存是共享的!  

transpose()后改变元数据,代码示例: 

x = torch.randn(3, 2)
y = torch.transpose(x, 0, 1)
print("修改前:")
print("x-", x)
print("y-", y)
 
print("\n修改后:")
y[0, 0] = 11
print("x-", x)
print("y-", y)

运行结果  :

修改前:
x- tensor([[-0.5670, -1.0277],
           [ 0.1981, -1.2250],
           [ 0.8494, -1.4234]])
y- tensor([[-0.5670,  0.1981,  0.8494],
           [-1.0277, -1.2250, -1.4234]])
 
修改后:
x- tensor([[11.0000, -1.0277],
           [ 0.1981, -1.2250],
           [ 0.8494, -1.4234]])
y- tensor([[11.0000,  0.1981,  0.8494],
           [-1.0277, -1.2250, -1.4234]])

可以看到,改变了y的元素的值的同时,x的元素的值也发生了变化

因此可以说,x是contiguous的,但y不是(因为内部数据不是通常的布局方式)。注意不要被contiguous的字面意思“连续的”误解,tensor中数据还是在内存中一块区域里,只是布局的问题!

为什么这么说:因为,y里面数据布局的方式和从头开始创建一个常规的tensor布局的方式是不一样的。这个可能只是python中之前常用的浅拷贝y还是指向x变量所处的位置,只是说记录了transpose这个变化的布局。

 


使用了contiguous():

如果想要断开这两个变量之间的依赖(x本身是contiguous的),就要使用contiguous()针对x进行变化感觉上就是我们认为的深拷贝

调用contiguous()时会强制拷贝一份tensor,让它的布局和从头创建的一模一样,但是两个tensor完全没有联系

x = torch.randn(3, 2)
y = torch.transpose(x, 0, 1).contiguous()
print("修改前:")
print("x-", x)
print("y-", y)
 
print("\n修改后:")
y[0, 0] = 11
print("x-", x)
print("y-", y)

运行结果:

修改前:
x- tensor([[ 0.9730,  0.8559],
           [ 1.6064,  1.4375],
           [-1.0905,  1.0690]])
y- tensor([[ 0.9730,  1.6064, -1.0905],
           [ 0.8559,  1.4375,  1.0690]])
 
修改后:
x- tensor([[ 0.9730,  0.8559],
           [ 1.6064,  1.4375],
           [-1.0905,  1.0690]])
y- tensor([[11.0000,  1.6064, -1.0905],
           [ 0.8559,  1.4375,  1.0690]])

可以看到修改前和修改后是没有影响。


33.torch.nn.softmax()

官方解释:


下面对softmax的dim的使用方法作一个讲解:

  • 首先说明tensor中的维度问题:
>>> x = torch.ones(2,2,3)
>>> x
tensor([[[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]]])
>>> x.dim()
3
>>> x[0]#这是第零维
tensor([[1., 1., 1.],
        [1., 1., 1.]])
>>> x[0][0]#这是第一维
tensor([1., 1., 1.])
>>> x[0][0][0]#这是第二维
tensor(1.)

由上面的代码可以看出从0->1->2越来越精确。

  • 重新看softmax中的维度所指:

首先在一个tensor之中,如:torch.ones(2,2,3),第一个2代指dim0,以此类推。

dim = x ,(x可以取零)x就是在哪一个dim上相加为1,这两者的dim是一样

>>> s = nn.Softmax(dim = 2)
>>> y = s(x)
>>> y
tensor([[[0.2269, 0.3270, 0.3197, 0.1264],
         [0.0761, 0.1326, 0.5079, 0.2834]],

        [[0.5902, 0.2372, 0.0695, 0.1031],
         [0.3855, 0.1251, 0.0122, 0.4773]],

        [[0.3930, 0.0614, 0.1801, 0.3655],
         [0.4990, 0.0276, 0.3864, 0.0871]]])
>>> y.sum(0)
tensor([[1.2102, 0.6256, 0.5693, 0.5949],
        [0.9606, 0.2853, 0.9065, 0.8477]])
>>> y.sum(1)
tensor([[0.3030, 0.4596, 0.8276, 0.4098],
        [0.9757, 0.3623, 0.0817, 0.5804],
        [0.8920, 0.0890, 0.5665, 0.4525]])
>>> y.sum(2)
tensor([[1.0000, 1.0000],
        [1.0000, 1.0000],
        [1.0000, 1.0000]])

比如一个二维矩阵x,如果dim = 1的话,那么就是令第二维的和为1,也就是每一行之和为1.可以想成x[i][j]中i不动,完全遍历每一个j(可以记成:dim是哪一维,哪一维就变),另这些元素和为1.见下图:


34.nn.Dropout2d

 

 


35. view_as(other)

 


36.nn.torch.NLLLoss()

链接:link


37.tensor == tensor

作用是比较两个tensor中的每一个元素,如果为相等就输出true不等就输出false,将会构成一个新的tensor。

>>> x = torch.tensor([1,1,1])
>>> y = torch.tensor([0,1,0])
>>> x == y
tensor([False,  True, False])

 

 


38.Batch Normalization

链接:link


39.torch.model里model.train model.eval区别

链接:https://blog.csdn.net/qq_38410428/article/details/101102075


40.torch.no_grad

 参看:

torch.no_grad()


41.torch中进行图像增强

代码如下:

#对图像进行随机裁减和水平旋转
train_transform = transforems.Compose(transforms.Resize(224, 224),
                                      transforms.RandomHorizontalFlip(),
                                      transforms.RandomRotation(0.2),
                                      transforms.ToTensor(),
                                      transforms.Nomalize([0.485, 0.456,0.456],
                                      [0.229, 0.224, 0.225]))
  • torchvision.transforms.RandomHorizontalFlip():

 

  • torchvision.transforms.RandomRotation(degrees):

注意:使用这些方法的时候要import torch.torchvision.tansforms as transforms

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值