Pytorch 神经网络—自定义数据集上实现教程

作者:LZDCQU 时间:2022-11-30 20:05:04 

第一步、导入需要的包


import os
import scipy.io as sio
import numpy as np
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from torch.autograd import Variable

batchSize = 128 # batchsize的大小
niter = 10   # epoch的最大值

第二步、构建神经网络

Pytorch 神经网络—自定义数据集上实现教程

设神经网络为如上图所示,输入层4个神经元,两层隐含层各4个神经元,输出层一个神经。每一层网络所做的都是线性变换,即y=W×X+b;代码实现如下:


class Neuralnetwork(nn.Module):
 def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
   super(Neuralnetwork, self).__init__()
   self.layer1 = nn.Linear(in_dim, n_hidden_1)
   self.layer2 = nn.Linear(n_hidden_1, n_hidden_2)
   self.layer3 = nn.Linear(n_hidden_2, out_dim)

def forward(self, x):
   x = x.view(x.size(0), -1)
   x = self.layer1(x)
   x = self.layer2(x)
   x = self.layer3(x)
   return x

model = Neuralnetwork(1*3, 4, 4, 1)

print(model) # net architecture

Neuralnetwork(
(layer1): Linear(in_features=3, out_features=4, bias=True)
(layer2): Linear(in_features=4, out_features=4, bias=True)
(layer3): Linear(in_features=4, out_features=1, bias=True)
)

 第三步、读取数据

自定义的数据为demo_SBPFea.mat,是MATLAB保存的数据格式,其存储的内容如下:包括fea(1000*3)和sbp(1000*1)两个数组;fea为特征向量,行为样本数,列为特征宽度;sbp为标签

Pytorch 神经网络—自定义数据集上实现教程


class SBPEstimateDataset(Dataset):

def __init__(self, ext='demo'):

data = sio.loadmat(ext+'_SBPFea.mat')
   self.fea = data['fea']
   self.sbp = data['sbp']

def __len__(self):

return len(self.sbp)

def __getitem__(self, idx):

fea = self.fea[idx]
   sbp = self.sbp[idx]
   """Convert ndarrays to Tensors."""
   return {'fea': torch.from_numpy(fea).float(),
       'sbp': torch.from_numpy(sbp).float()
       }

train_dataset = SBPEstimateDataset(ext='demo')
train_loader = DataLoader(train_dataset, batch_size=batchSize, # 分批次训练
            shuffle=True, num_workers=int(8))

整个数据样本为1000,以batchSize = 128划分,分为8份,前7份为104个样本,第8份则为104个样本。在网络训练过程中,是一份数据一份数据进行训练的

第四步、模型训练


# 优化器,Adam
optimizer = optim.Adam(list(model.parameters()), lr=0.0001, betas=(0.9, 0.999),weight_decay=0.004)
scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.997)
criterion = nn.MSELoss() # loss function

if torch.cuda.is_available(): # 有GPU,则用GPU计算
  model.cuda()
  criterion.cuda()

for epoch in range(niter):
  losses = []
  ERROR_Train = []
  model.train()
  for i, data in enumerate(train_loader, 0):
    model.zero_grad()# 首先提取清零
    real_cpu, label_cpu = data['fea'], data['sbp']

if torch.cuda.is_available():# CUDA可用情况下,将Tensor 在GPU上运行
      real_cpu = real_cpu.cuda()
      label_cpu = label_cpu.cuda()

input=real_cpu
      label=label_cpu

inputv = Variable(input)
      labelv = Variable(label)

output = model(inputv)
      err = criterion(output, labelv)
      err.backward()
      optimizer.step()

losses.append(err.data[0])

error = output.data-label+ 1e-12
      ERROR_Train.extend(error)

MAE = np.average(np.abs(np.array(ERROR_Train)))
  ME = np.average(np.array(ERROR_Train))
  STD = np.std(np.array(ERROR_Train))

print('[%d/%d] Loss: %.4f MAE: %.4f Mean Error: %.4f STD: %.4f' % (
  epoch, niter, np.average(losses), MAE, ME, STD))
 

  
[0/10] Loss: 18384.6699 MAE: 135.3871 Mean Error: -135.3871 STD: 7.5580
[1/10] Loss: 17063.0215 MAE: 130.4145 Mean Error: -130.4145 STD: 7.8918
[2/10] Loss: 13689.1934 MAE: 116.6625 Mean Error: -116.6625 STD: 9.7946
[3/10] Loss: 8192.9053 MAE: 89.6611 Mean Error: -89.6611 STD: 12.9911
[4/10] Loss: 2979.1340 MAE: 52.5410 Mean Error: -52.5279 STD: 15.0930
[5/10] Loss: 599.7094 MAE: 22.2735 Mean Error: -19.9979 STD: 14.2069
[6/10] Loss: 207.2831 MAE: 11.2394 Mean Error: -4.8821 STD: 13.5528
[7/10] Loss: 189.8173 MAE: 9.8020 Mean Error: -1.2357 STD: 13.7095
[8/10] Loss: 188.3376 MAE: 9.6512 Mean Error: -0.6498 STD: 13.7075
[9/10] Loss: 186.8393 MAE: 9.6946 Mean Error: -1.0850 STD: 13.6332

来源:https://blog.csdn.net/qq_21905401/article/details/82627402

标签:Pytorch,神经网络,自定义,数据集
0
投稿

猜你喜欢

  • 微信小程序:报错(in promise) MiniProgramError

    2024-05-09 10:35:18
  • Python编程入门指南之函数

    2021-05-13 15:45:31
  • python 测试实现方法

    2023-03-24 11:34:04
  • SQL Server 1069错误(由于登录失败而无法启动服务)解决方法

    2008-09-12 17:35:00
  • js 数据存储和DOM编程

    2024-02-24 07:52:31
  • Django使用httpresponse返回用户头像实例代码

    2021-08-28 04:47:03
  • MongoDB为用户设置访问权限

    2023-07-16 06:03:52
  • vue表单自定义校验规则介绍

    2024-04-30 10:25:10
  • 关于useSSL=false和true的区别及说明

    2024-01-25 03:27:40
  • ASP正则表达式技巧

    2011-04-10 11:00:00
  • go流程控制代码详解

    2023-10-15 18:14:19
  • go语言优雅地处理error工具及技巧详解

    2023-07-16 18:56:56
  • TensorFlow tf.nn.max_pool实现池化操作方式

    2021-08-20 20:36:45
  • SqlServer 2005 T-SQL Query 学习笔记(1)

    2024-01-25 17:01:56
  • ASP.NET教程第二讲 ASP.NET学习

    2007-08-07 12:01:00
  • MySQL 那些常见的错误设计规范,你都知道吗

    2024-01-25 18:19:36
  • ASP的URLDecode函数URLEncode解码函数

    2011-02-16 11:17:00
  • 一文带你搞懂Python中的数据容器

    2021-05-25 16:09:59
  • Python生成器实现简单"生产者消费者"模型代码实例

    2022-06-30 02:36:03
  • YOLOv5车牌识别实战教程(五)字符分割与识别

    2022-04-07 07:38:25
  • asp之家 网络编程 m.aspxhome.com