关于Pytorch相关的资料
(1)torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0,dilation=1,groups=1, bias=True)
参数详解:
- in_channels :输入通道。在文本分类中,即为向量的维度:word_vector。input_shape=[batch_size, word_vector, sequence_length]
- out_channels:卷积产生的通道,有多少个out_channels,就需要多少1维卷积。(如果是隐藏层的话,也是下一层的输入:in_channels)
- kernel_size: 卷积核的尺寸(k,in_channels)
- stride: 卷积步长
- padding: 输入的每一条边补充0的层数,如果使得卷积后,和原来相同。则应该设置该值为: floor(kernel_size/2)
- dilation:卷积核元素之间的间距(空洞卷积时,需要用到这个值)。一般情况默认值
- groups: 输入通道到输出通道的阻塞连接数。一般情况默认值
- bias: 如果bias为True,添加偏置。一般情况默认值
(2)torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False,ceil_mode=False)
参数详解:
- kernel_size (int or tuple): max pooling的窗口大小
- stride (int or tuple,optional) - max pooling的窗口移动的步长。默认值是kernel_size
- padding (int or tuple, optional)-输入的每一条补充0的层数
- dilation (int or tuple,optional)-一个控制窗口中元素步幅的参数
- return_indices -如果等于True,会返回输出最大值的序号,对于上采样操作会有帮助
- ceil_mode-如果等于True,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
(3)激活函数
- torch.nn.ReLU(alpha=1.0, inplace=False)
数学表达式:ELU(x)=max(0,x)+min(0,α∗(exp(x)−1))
其中 α是超参数,默认为1.0 - torch.nn.LeakyReLU(negative_slope=0.01,inplace=False)
数学表达式LeakyReLU(x)=max(0,x)+negative_slope∗min(0,x)
其中 negative_slope是超参数,控制x为负数时斜率的角度,默认为1e-2 - torch.nn.PReLU(num_parameters=1,init=0.25)
数学表达式:PReLU(x)=max(0,x)+a∗min(0,x)
其中a 是一个可学习的参数,当不带参数调用时,即nn.PReLU(),在所有的输入通道上使用同一个a,当带参数调用时,即nn.PReLU(nChannels),在每一个通道上学习一个单独的a。
(4) torch.nn.Linear(in_features, out_features, bias=True)
nn.Linear()是用来设置网络中的全连接层的,需要注意的是全连接层的输入与输出都是二维张量,一般形状为[batch_size,size],不同于卷积层要求的输入是四维张量:
参数详解:
- kernel_size: (int or tuple) max pooling的窗口大小
- stride:
in_features:指的是输入二维张量的大小。即输入[batchsize,size]里面的size。
out_features:指的是输出二维张量的大小,即输出的二维张量形状为[batch_size, output_size],当然,它也代表了该全连接层的神经元个数。
从输入输出的张量shape角度来理解,相当于输入为[batch_size,in_fetaures]的张量变换成了[batch_size,out_fetaures]的输出张量。
用法示例:
import torch as t
from torch import nn
# in_feature由输入张量的形状决定,out_features则决定了输出张量的形状
connected_layer = nn.Linear(in_fetaures=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)
(5) torch.nn.Dropout(p=0.5,inplace=False)
其作用是,在 training 模式下,基于伯努利分布抽样,以概率 p 对张量 input 的值随机置0;
training 模式中,对输出以 1/(1-p) 进行 scaling,而 evaluation 模式中,使用恒等函数;
p:默认 0.5,张量元素被置0的概率;
inplace:默认 False,是否原地执行;
(6)torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None,
max_norm=None, norm_type=2.0, scale_grad_by_freq=False,
sparse=False, _weight=None)
参数详解:
- num_embeddings(pytorch:int)-词典的大小尺寸
- embedding_dim(pytorch:int)-嵌入向量的维度
- padding_idx:填充id
- max_norm:最大范数
- norm_type: 指定利用什么范数计算,并用于对比max_norm,默认为2范数
- scale_grad_by_freq:根据单词在mini_batch中出现的频率,对梯度进行放缩
- sparse: 若为True,则与权重矩阵相关的梯度转变为稀疏张量
(7)torch.nn.Sequential()对象
建立nn.Sequential()对象,必须小心确保一个块的输出大小与下一个块的输入大小匹配。基本上,它的行为就像一个nn.Module。
模型建立方式
- 第一种写法:
nn.Sequential()对象.add_module(层名,层class的实例)
net1 = nn.Seuential()
net1.add_module("conv",nn.Conv1d(3,3,3))
net1.add_module("batchnorm",nn.BatchNorm1d(3))
net1.add_module("activation_layer",nn.ReLU())
- 第二种写法:
nn.sequential(多个层class的实例)
net2 = nn.Sequential(
nn.Conv1d(3,3,3),
nn.BatchNorm1d(3),
nn.ReLU()
)
- 第三种写法:
nn.Sequential( OrderedDict([ 多个(层名, 层class的实例)])
from collections import OrderedDict
net3 = nn.Sequential(OrderedDict(
[
("conv", nn.Conv1d(3,3,3)),
('batchnorm', nn.BatchNorm1d(3)),
("activation_layer", nn.ReLU())
]
))
(8) torch.nn.ModuleList()函数
使用ModuleList可以简化写法
ModuleList可以存储对各model,传统的方法,一个model就要写一个forward,但是如果把他们都存到一个ModuleList的话,就可以使用一个forward。
ModuleList是Module的子类,当在Module中使用它的时候,就能自动识别为子module
示例:
class model2(nn.Module):
def __init__(self):
suoer(model2,self).__init__()
self.layers = nn.ModuleList([
nn.Linear(1,10), nn.ReLU(),
nn.Linear(10,100),nn.ReLU(),
nn.Linear(100,10),nn.ReLU(),
nn.Linear(10,1)])
def forward(self,x):
out = x
for i, layer in enumerate(self.layers):
out = layer(out)
return = out
(9) torch.nn.GRU(input_size,hidden_size, num_layers,bias=True, batch_first, dropout,bidirectional)
参数解读:
- input_size: 输入维度
- **hidden_size:**隐状态的维度
- num_layers: RNN的层数
- bias: 如果为False,那么RNN层将不会使用bias,默认为True。
- batch_first: 如果为True的话,那么输入和输出的tensor的形状是(batch, seq, feature)
- dropout: 如果非零的话,将会在RNN的输出上加个dropout,最后一层除外
- bidirectional: 如果为True,将会变成一个双向RNN,默认为False。