无锡做设计公司网站,开发公司总工程师职责,wordpress数据收集,用文件传输协议登录网站目录
神经网络-非线性激活
神经网络-线形层及其他层介绍 神经网络-非线性激活
官方文档地址#xff1a;torch.nn — PyTorch 2.0 documentation
常用的#xff1a;Sigmoid、ReLU、LeakyReLU等。 作用#xff1a;为模型引入非线性特征#xff0c;这样才能在训练过程中…目录
神经网络-非线性激活
神经网络-线形层及其他层介绍 神经网络-非线性激活
官方文档地址torch.nn — PyTorch 2.0 documentation
常用的Sigmoid、ReLU、LeakyReLU等。 作用为模型引入非线性特征这样才能在训练过程中训练出符合更多特征的模型。
其中有个参数是inplace默认为False表示是否就地改变输入值True则表示直接改变了input不再有另外的返回值False则没有直接改变input并有返回值建议是inplaceFalse。
import torch
from torch import nninput torch.tensor([[3, -1],[-0.5, 1]])
input torch.reshape(input, (1, 1, 2, 2))relu nn.ReLU()
input_relu relu(input)print(input{}\ninput_relu:{}.format(input, input_relu))# inputtensor([[[[ 3.0000, -1.0000],
# [-0.5000, 1.0000]]]])
# input_relu:tensor([[[[3., 0.],
# [0., 1.]]]])神经网络-线形层及其他层介绍
Linear Layers中的torch.nn.Linear(in_features, out_features, biasTrue)。默认biasTrue。对传入数据应用线性变换
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如果为False则该层不会学习加法偏置默认为true
Shape分别关注输入、输出的最后一个维度的大小在训练过程中nn.Linear往往是当作的展平为一维后最后几步的全连接层所以此时就只关注了通道数即往往Input和Outputs是一维的
“展平为一维”经常用到torch.nn.Flatten(start_dim1, end_dim- 1)
想说一下start_dim它表示“从start_dim开始把后面的维度都展平到同一维度上”默认是是1在实际训练中从start_dim1开始展平因为在训练中的tensor是4维的分别是[batch_size, C, H, W]而第0维的batch_size不能动它所以是从1开始的。
还比较重要的有torch.nn.BatchNorm2d、torch.nn.Dropout、Loss Functions之后再讲。其它的Transformer Layers、Recurrent Layers都不是很常用。
import torch# 对4维tensor展平start_dim1input torch.arange(54)
input torch.reshape(input, (2, 3, 3, 3))y_0 torch.flatten(input)
y_1 torch.flatten(input, start_dim1)print(input.shape)
print(y_0.shape)
print(y_1.shape)# torch.Size([2, 3, 3, 3])
# torch.Size([54])
# torch.Size([2, 27])