在PyTorch建立模型,主要是NN模块。
nn.LineaR
nn.LineaR是创建一个线性层。这里需要将输入和输出维度作为参数传递。
lineaR = nn.LineaR(10, 2) example_input = Torch.Randn(3, 10) example_output = lineaR(example_input) example_output
上面代码lineaR接受nx10的输入并返回nx2的输出。
print(example_input) print(example_output) tensor([[ 1.1122, -0.1381, 0.5547, -0.3326, -0.5676, 0.2810, -0.5521, -0.8729, -0.6627, 0.8729], [ 1.9134, 0.2397, -0.8340, 1.1532, -1.6725, 0.6171, -0.0357, -1.6848, -0.8454, 0.3876], [-0.0786, -0.1541, -0.8385, -0.1587, -0.0121, 1.4457, -0.0132, 1.5653, -1.6954, -0.9350]]) # 输出如下 tensor([[-0.1249, -0.8002], [-1.0945, -0.2297], [-0.3558, 0.8439]], grad_fn=) nn.Relu
nn.Relu对线性的给定输出执行 Relu 激活函数操作。
Relu = nn.ReLU() Relu_output = Relu(example_output) Relu_output # 输出如下 tensor([[0.0000, 0.0000], [0.0000, 0.0000], [0.0000, 0.8439]], grad_fn=) nn.BATchNoRM1d
nn.BATchNoRM1d是一种标准化技术,用于在不同批次的输入中保持一致的均值和标准偏差。
BATchnoRM = nn.BATchNoRM1d(2) BATchnoRM_output = BATchnoRM(Relu_output) BATchnoRM_output # 输出如下 tensor([[ 0.0000, -0.7071], [ 0.0000, -0.7071], [ 0.0000, 1.4142]], grad_fn=) nn.Sequential
nn.Sequential一次性创建一系列操作。和tensoRflow中的Sequential完全一样。
Mlp_layeR = nn.Sequential( nn.LineaR(5, 2), nn.BATchNoRM1d(2), nn.ReLU() ) test_example = Torch.Randn(5, 5) + 1 print(“input: “) print(test_example) print(“output: “) print(Mlp_layeR(test_example)) # 输出如下 input: tensor([[ 1.4617, 1.2446, 1.4919, 1.5978, -0.3410], [ -0.2819, 0.5567, 1.0113, 1.8053, -0.0833], [ 0.2830, 1.0857, 1.2258, 2.6602, 0.1339], [ 0.8682, 0.9344, 1.3715, 0.0279, 1.8011], [ 0.6172, 1.1414, 0.6030, 0.3876, 1.3653]]) output: tensor([[0.0000, 0.0000], [0.0000, 1.3722], [0.0000, 0.8861], [1.0895, 0.0000], [1.3047, 0.0000]], grad_fn=)
在上面的模型中缺少了优化器,我们无法得到对应损失。
import Torch.optiM as optiM adaM_opt = optiM.AdaM(Mlp_layeR.paRaMeteRs(), lR=1e-1) # 这里lR表示学习率,1e-1表示0.1 tRAIn_example = Torch.Randn(100, 5) + 1 adaM_opt.zeRo_gRad() # 我们将使用1减去平均值,作为简单损失函数 cuR_loSS = Torch.abs(1 – Mlp_layeR(tRAIn_exaMple)).Mean() cuR_loSS.backwaRd() # 更新参数 adaM_opt.step() print(cuR_loSS.data) # 输出如下 tensor(0.7467)
虽然上面只是用了一个epoch,训练线性模型得到loSS为0.7467,上面就是NN模型建立Model的整个流程,
第一个神经网络模型
下面实现第一个分类神经网络,其中一个隐藏层用于开发单个输出单元。
首先,使用以下命令导入 PyToRch 库 –
import Torch import Torch.nn as nn
定义所有层和批量大小以开始执行神经网络,如下所示 –
n_in, n_h, n_out, BATch_size = 10, 5, 1, 10
由于神经网络包括输入数据的组合以获得相应的输出数据,我们将遵循以下相同的程序 –
x = Torch.Randn(BATch_size, n_in) y = Torch.tensor([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
创建顺序模型。使用下面代码,创建一个顺序模型 –
Model = nn.Sequential(nn.LineaR(n_in, n_h), nn.ReLU(), nn.LineaR(n_h, n_out), nn.SigMoid())
借助梯度下降优化器构建损失函数,如下所示 –
# 构造损失函数 cRITeRion = Torch.nn.MSELoSS() # 构造优化器 optiMizeR = Torch.optiM.SGD(Model.paRaMeteRs(), lR = 0.01)
使用给定代码行的迭代循环实现梯度下降模型 –
# 梯度下降 for epoch in Range(50): # 正向传递:通过将x传递给模型来计算预测的y y_pRed = Model(x) # 计算loSS loSS = cRITeRion(y_pRed, y) # 梯度清0 optiMizeR.zeRo_gRad() # 反向传播,求解梯度 loSS.backwaRd() # 更新模型参数 optiMizeR.step() if epoch % 10 == 0: print(‘epoch: ‘, epoch, ‘ loSS: ‘, loSS.ITeM())
输出如下
epoch: 0 loSS: 0.2508794665336609 epoch: 10 loSS: 0.24847669899463654 epoch: 20 loSS: 0.24615907669067383 epoch: 30 loSS: 0.24392127990722656 epoch: 40 loSS: 0.24175791442394257