当前位置:网站首页>(6) Pytorch deep learning: loading datasets
(6) Pytorch deep learning: loading datasets
2022-07-21 03:33:00 【Kkh_ eight thousand six hundred and eighty-six】
PyTorch Load data set
1、Dataset: Data sets ( Support data indexing );
2、DataLoader: Load data set ;
3、Batch: Load all the data ;( advantage : Can maximize the use of vector computing optimization to improve the speed of Computing ; shortcoming )
4、 Stochastic gradient descent : A sample data ;( advantage : Get a good randomness , Overcome saddle point problem ; shortcoming : The trained model is better than others , But because there is only one sample at a time , Can't use CPU/GPU Parallel ability , Optimization time is too long )
5、 Code :
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader # Dataset: abstract class , Can't instantiate
from torch.utils.data import DataLoader
#####################1 Get ready 、 Load data set #################################
# DiabetesDataset Inherit Dataset The basic functions of the secondary class
class DiabetesDataset(Dataset):
def __init__(self, filepath): # initialization , According to the path (filepath) Or load simple data and tags
xy = np.loadtxt(filepath, delimiter=',', dtype=np.float32)
self.len = xy.shape[0]
self.x_data = torch.from_numpy(xy[ : , : -1]) # All rows in the column after rent
self.y_data = torch.from_numpy(xy[ : , [-1]]) # All rows in the last column
# In order to be in DiabetesDataset After instantiation Indexes dataset[index] Extract the data
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
# Quantity of data
def __len__(self):
return self.len
dataset = DiabetesDataset(“diabetes.csv.gz”) # Here diabetes.csv.gz No,
train_loader = DataLoader(dataset = dataset,
batch_size= = 32,
shuffle = True,
num_workers = 2)
#########################2 Use class design model ###############################
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8,6)
self.linear1 = torch.nn.Linear(6,4)
self.linear1 = torch.nn.Linear(4,1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
###################3 Build loss function 、 Optimizer ###############################
criterion = torch.nn.BCELoss(size_average=False) # BCE Loss
optimizer = torch.optim.SGD(model.parameters(), lr=0.1) # Parameter optimization
#####################4 Cycle training #########################
for epoch in range(100):
for i, data in enumerate(train_loader, 0):
# Prepare the data
inputs, labels = data
# Forward propagation
y_pred = model(inputs)
loss = criterion(y_pred, labels)
print(epoch, i, loss.item())
# Back propagation
optimizer.zero_grad()
loss.backward()
# Update weights
optimizer.step()
边栏推荐
猜你喜欢
Li Hongyi 2020 machine learning notes -- P10 classification
Li Hongyi machine learning 2020---p12 brief introduction of DL & p15 why DL
Li Hongyi 2020 machine learning -- P11 logistic progression
[deep learning] instantiate a simple convolutional neural network using MNIST data set
Redis persistence
(4) Pyqt5 series tutorials: use pychart to design the internal logic of pyqt5 in the serial port assistant parameter options (I)
(2) Pytorch deep learning: gradient descent
The role of 'defer' and 'async' attributes on the < srcipt> tag
MIMO-OFDM Wireless Communication Technology and matlab implementation (2) - Outdoor channel model under SISO
数仓技术实现
随机推荐
xshell安装完,启动报错:由于找不到 mfc110.dll,无法继续执行代码。重新安装程序可能会解决此问题
(六)PyTorch深度学习:Logistic回归(多层多维特征输入)
MIMO-OFDM Wireless Communication Technology and matlab implementation (2) - Outdoor channel model under SISO
李宏毅2020机器学习笔记---P10 Classification
SQL processing data synchronization group analysis
Netcat 简单的小工具模拟客户端/服务端
Select all on the current page of Ali vector Gallery
[deep learning] convolutional neural network maximum pool operation
The insertion order of single chain storage structure
MySQL is migrated to Dameng through DTS
mysql 通过dts迁移至达梦
Redis主从复制
JDBC 和 ODBC 的区别
任务调度:常见类型和工具
Swift中struct与class的区别
【深度学习】卷积神经网络最大池化运算
Data warehouse OLAP OLTP modeling method
Basic overview of data warehouse
innobackupex参数说明
如何使用 IDEA 打 jar 包