Pytorch中的一些模块
1.nn.Linear()
nn.Linear()用来设置神经网络中的全连接层。全连接层的输入和输出均为二维张量,一般形状为[batch_size, size
]。这与卷积层所要求的的输入输出量均为四维张量不同。其用法和形参如下:
in_features
指的是输入的二维张量的大小,即输入的[batch_size, size]
中的size
。
out_features
指的是输出的二维张量的大小,即输出的二维张量的形状为[batch_size,output_size]
,当然,它也代表了该全连接层的神经元个数。
从输入输出的张量的shape角度来理解,相当于一个输入为[batch_size, in_features]
的张量变换成了[batch_size, out_features]
的输出张量。
用法示例:
import torch as t
from torch import nn
# in_features由输入张量的形状决定,out_features则决定了输出张量的形状
connected_layer = nn.Linear(in_features = 64*64*3, out_features = 1)
# 假定输入的图像形状为[64,64,3]
input = t.randn(1,64,64,3)
# 将四维张量转换为二维张量之后,才能作为全连接层的输入
input = input.view(1,64*64*3)
print(input.shape)
output = connected_layer(input) # 调用全连接层
print(output.shape)
1234567891011121314
这段代码运行结果为:
input shape is %s torch.Size([1, 12288])
output shape is %s torch.Size([1, 1])
引用:https://blog.csdn.net/qq_42079689/article/details/102873766
2.Variable(可弃用)
Pytorch中的两个基本对象为:Tensor(张量)和Variable(变量)。Variable可以反向传播,而Tensor不可以进行反向传播(如今Tensor也可以反向传播,只要设置requires_grad=True)。
Variable参数如下:
Variable(tensor)
Variable(tensor, requires_grad)
引用:https://blog.csdn.net/KeEN_Xwh/article/details/104588645/
https://pytorch.org/docs/stable/autograd.html?highlight=variable
3.nn.Parameter(data,requires_grad=True)
Tensor的一种子类。与Module一同使用时,会被赋值到Module属性中,它们将会自动添加到参数列表中,出现在Parameters()迭代器里。
Parameters的参数:
- data (Tensor) – parameter tensor.
- requires_grad (bool, optional) – if the parameter requires gradient. See Excluding subgraphs from backward for more details. Default: True
4.nn.init.xavier_uniform(tensor, gain=1.0)
依据 *Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010)*提出的一种方法,利用一个均匀分布来填充输入Tensor。填充后的Tensor内为 U ( − a , a ) U(-a,a) U(−a,a) ,其中
a = g a i n × 2 F a n i n + F a n o u t a = gain \times \sqrt{\frac{2}{Fan_{in}+Fan_{out}}} a=gain×Fanin+Fanout