关于pytorch搭建简单网络的流程

创建torch

·通过list数据创建
·通过numpy创建
·通过tensor库创建

import torch 
from torch import nn
from torchsummary import summary

构建神经网络模型

数据的构造:数据转化为tensor
构造batch

模型的构造:

优化相关的设置:设置optimizer的选择以及配置等信息

训练模型:循环数据,通过optimizer来优化模型参数

数据的构造

# 加载波士顿房价数据集
from sklearn.datasets import load_boston
X, y =load_boston(return_X_y=True)

# 划分数据集方法
from sklearn.model_selection import train_test_split
# 归一化处理
from sklearn.preprocessing import MinMaxScaler
# 划分为测试集与训练集
X_train, X_test, Y_train, Y_test = train_test_split(X,y,test_size = 0.3,random_state = 0)

# 归一化处理操作
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
print(type(X_train))
<class 'numpy.ndarray'>
# 将ndarray的数据转化为tensor类型
# 增加一个新的维度再dim的位置
inputs = torch.FloatTensor(X_train.astype(np.float32))
targets = torch.FloatTensor(Y_train.astype(np.float32))
# inputs = torch.unsqueeze(torch.FloatTensor(X_train.astype(np.float32)),dim=1)
# targets = torch.unsqueeze(torch.FloatTensor(Y_train.astype(np.float32)),dim=1).reshape(-1,1,1)
dataset = torch.utils.data.TensorDataset(inputs, targets)
# shuffle 是否打乱
loader =  torch.utils.data.DataLoader(dataset, batch_size = 64,shuffle=True)

print(inputs.shape)
# print(X_train)
print(targets.shape)
torch.Size([354, 13])
torch.Size([354])
for x in loader:
    print("----------------------")
#     print(x)
    print(x[0].shape)
----------------------
torch.Size([64, 13])
----------------------
torch.Size([64, 13])
----------------------
torch.Size([64, 13])
----------------------
torch.Size([64, 13])
----------------------
torch.Size([64, 13])
----------------------
torch.Size([34, 13])

模型的构造

对于模型这部分,需要设计的是前向传播部分(forward),因为这部分其实决定了整个模型的细节,比如一个数据x进入模型之后,如何一步步转换成最终的输出。转换细节实际上就是模型的细节。在构建模型时,我们通常会创建一个新的类(class),并起一个合适的名字给到神经网络,之后在初始化阶段定义模型中所使用的参数和部件,接着在forward函数中设计输入到输出中所经历的所有的过程。

class LinearRegression (nn.Module): 
    def __init__(self, input_size, output_size):
        super (LinearRegression, self).__init__() 
        # 定义一个线性层
        self.linear = nn.Linear(input_size, output_size)
    def forward (self, x): 
        # 前向传播时线性层
        out = self.linear(x)
        return out
# 特征数多少,输出多少个
model = LinearRegression(input_size = 13, output_size=1)
# input_size (y,x)
summary(model, input_size =(1,13),device ="cpu")
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Linear-1                 [-1, 1, 1]              14
================================================================
Total params: 14
Trainable params: 14
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.00
Params size (MB): 0.00
Estimated Total Size (MB): 0.00
----------------------------------------------------------------

优化器选择和配置

设计好了模型之后,剩下的工作就是设计loss和配置优化器。
在模型中我们定义了forward0函数内容,通过这个函数就可以得到对于输入的预测。
有了预测就可以跟真实值做比较来计算损失了。

loss.backward()故名思义,就是将损失loss 向输入侧进行反向传播,同时对于需要进行梯度计算的所有参数.
optimizer.step()是优化器对参数的值进行更新

https://zhuanlan.zhihu.com/p/445009191

criterion = nn.MSELoss()
# 优化器
# SGD:Implements stochastic gradient descent 
optimizer = torch.optim.SGD(model.parameters(), lr=1e-2)

模型训练

定义好模型以及训练方式之后,在这里需要定义要循环多少次(epoch),如何保存中间结果,如何输出准确率等内容。

num_epochs = 1000
# 循环迭代次数
for epoch in range (num_epochs): 
    # 分批量训练
    for inps, tars in loader: 
        # 输入inputs模型参数,输出结果
        outputs = model(inps)
        # 先把梯度归0
        optimizer.zero_grad()
        # 上面定义的性能度量,也就是目标函数
        # 输入模型输出结果和实际结果,进行比较
        # 计算损失函数
        loss = criterion(outputs, tars)
        # 实现参数回传
        loss.backward ()
        # 更新参数
        optimizer.step()
    if (epoch + 1) % 100 == 0:
        print(f"Epoch {epoch + 1} / {num_epochs}, Loss: {loss.detach().numpy ()}")
Epoch 100 / 1000, Loss: 98.41191101074219
Epoch 200 / 1000, Loss: 121.85580444335938
Epoch 300 / 1000, Loss: 73.39604187011719
Epoch 400 / 1000, Loss: 57.819400787353516
Epoch 500 / 1000, Loss: 53.49835205078125
Epoch 600 / 1000, Loss: 69.24689483642578
Epoch 700 / 1000, Loss: 95.05398559570312
Epoch 800 / 1000, Loss: 81.44168853759766
Epoch 900 / 1000, Loss: 66.82765197753906
Epoch 1000 / 1000, Loss: 90.36398315429688

模型评估

# d对模型进行评估
model.eval()
# test_inputs = torch.unsqueeze(torch.FloatTensor(X_test.astype (np.float32)), dim=1) 
test_inputs = torch.FloatTensor(X_test.astype (np.float32)) 
test_pred = model(test_inputs)
test_pred = test_pred.data.squeeze().numpy() 
test_pred
array([22.617939, 22.528278, 22.975534, 22.591637, 22.973684, 22.734558,
       22.664818, 22.803598, 21.78248 , 22.112335, 22.058214, 22.48905 ,
       23.186394, 22.42793 , 23.449627, 23.124287, 22.83773 , 23.167984,
       22.48751 , 22.452835, 22.920414, 22.526266, 22.65124 , 22.614809,
       22.491415, 20.686409, 22.959938, 22.749825, 23.485897, 22.797514,
       22.991114, 23.130825, 23.313112, 22.895496, 22.527882, 22.850822,
       23.23886 , 22.322727, 22.333015, 22.981808, 22.788813, 22.700089,
       22.68786 , 22.439156, 23.15319 , 23.008183, 23.040518, 23.171814,
       22.96529 , 22.998026, 22.642712, 22.624947, 22.51308 , 24.487637,
       22.76371 , 22.897873, 22.715595, 23.156818, 21.333155, 22.953001,
       23.232807, 22.928759, 22.95558 , 22.480427, 22.599974, 22.394777,
       23.47198 , 23.629253, 22.86985 , 22.578667, 22.812248, 22.771488,
       22.538357, 23.143078, 23.044415, 22.784977, 23.698738, 22.974617,
       22.582672, 22.660606, 22.959587, 23.313873, 22.227062, 23.595732,
       23.907787, 22.93917 , 22.49835 , 21.748869, 23.217987, 23.090294,
       23.018055, 23.104914, 23.00807 , 23.157112, 22.668602, 23.171522,
       22.336212, 23.167414, 22.728645, 23.035696, 22.825071, 22.899158,
       23.003893, 22.653786, 23.351963, 22.927776, 22.163712, 22.05478 ,
       22.723917, 22.766144, 22.882189, 22.625837, 23.389277, 22.421246,
       23.438246, 22.92104 , 22.88575 , 23.204567, 22.661224, 23.335712,
       22.889713, 22.90001 , 22.878353, 22.665678, 22.596249, 23.027405,
       23.062466, 23.212257, 22.936724, 23.22808 , 23.249989, 22.916468,
       23.095114, 22.491749, 22.943712, 23.59633 , 22.542927, 23.218365,
       23.411568, 22.822601, 23.2967  , 22.593838, 22.756287, 23.02127 ,
       22.610912, 22.989109, 23.203293, 23.429138, 22.56171 , 22.927872,
       22.875078, 22.96389 ], dtype=float32)
from sklearn import metrics
mse = metrics.mean_squared_error(Y_test,test_pred)
print(mse)
81.70286240634925
# Print model 's state_dict
print("Model's state_dict:")
# print(model.state_dict)
for param_tensor in model.state_dict():
    print (param_tensor, "It", model.state_dict() [param_tensor].numpy() [0])
Model's state_dict:
<bound method Module.state_dict of LinearRegression(
  (linear): Linear(in_features=13, out_features=1, bias=True)
)>
linear.weight It [-0.19711481 -0.17246322  0.50178254  0.02363779  0.9656156   3.3555963
  0.13307181  2.0316956  -0.1019813  -0.14195028  0.3510409   0.879053
  1.2751613 ]
linear.bias It 18.75117

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值