一、pytorh中的torch.nn的作用
torch.nn是pytorch中自带的一个函数库,里面包含了神经网络中使用的一些常用函数。
二、Modoule类
(1)线性层torch.nn.Linear
功能:math:y=xA^T+b
输入:输入神经元,输出神经元,是否有偏执b
线性层会自动生成权重参数。如果要控制,可以采用随机种子。
示例:
import torch
input_data = 1000
hidden_layer = 100
torch1 = torch.nn.Linear(input_data, hidden_layer, bias=False)
print(torch1)
(2)激活层ReLU
功能:完成激活层,进行非线性处理max(0, x)。
输入:可以暂时没有输入
torch.nn包内还提供多种激活函数可以选择,如PReLU、LeaKyReLU、Tanh、Sigmoid、Softmax等。
示例
(3)MSELoss
计算均方误差
import torch
from torch.autograd import Variable
loss = torch.nn.MSELoss()
x = Variable(torch.randn(10,10))
y = Variable(torch.randn(10,10))
loss = loss(x,y)
print(loss.data)
(4)序列容器Sequential
在容器中嵌套各种实现神经网络模型的搭建,等于堆积木中的积木的壳子。
models = torch.nn.Sequential(
torch.nn.Linear(input_data,hidden_layer),
torch.nn.ReLU(),
torch.nn.Linear(hidden_layer,output_data)
)
(5)之后遇到了再补充