当前位置:网站首页>How does the pytorch project run?
How does the pytorch project run?
2022-07-03 03:46:00 【Program Yuanke】
PyTorch Basic training process : Use PyTorch Establishing a deep learning model generally has the following steps :
- Basic configuration
- Data read in
- model building
- Loss function and optimizer
- Training
- assessment
In the following notes , First, I will briefly introduce the basic content of each step , Then match the case , Use FashionMNIST Data sets , adopt PyTorch The code implements every step , Finally, a classifier is built and trained .
pytorch How to run the project ?
Basic configuration
In the basic configuration, you generally need to complete the following three configurations :
Import Python Common bag
- Quickly realize the functions we need through common packages
GPU To configure
- If subsequent calculations require GPU, Here is the preset
Set the super parameters uniformly
- Put together , Convenient for subsequent debugging
# Import Python Common bag
import os
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset
# To configure GPU
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
# Configure other super parameters
batch_size = 256 # A training batch Size
num_workers = 1 # Working at the same time cpu Threads
lr = 1e-4 # Learning rate
epochs = 20 # Training epochs Count
Be careful , In the following code , We will not use GPU Calculation .
Data read in
Data reading will use PyTorch Two classes of :
Dataset
- Define the format of the data , distinguish samples Corresponding to them labels
DataLoader
- take Dataset become iterable, Convenient for later use iterative Read in different batches of data
First , We use Dataset To build subclasses , In this subclass , We need to define three functions :
__init__
- Definition samples And the corresponding labels, At the same time, external parameters are passed into the class .
__len__
- Return data set samples Count
__getitem__
- adopt index Read one by one samples The elements in the collection , Finally, return the data required for training and verification ( If necessary, the data can be transformation operation )
Now let's look at an example . because FashionMNIST The data set is stored in csv In file , We need to do something about it first transformation.
# Set data transformation
from torchvision import transforms
image_size = 28
# Package a series of transformations you want to make to the data , So that it can be directly used when reading data later
data_transform = transforms.Compose([
transforms.ToPILImage(), # convert a tensor or ndarray to PIL image(python image library)
transforms.Resize(image_size), # resize imput image into given size
transforms.ToTensor() # convert a PIL image or ndarray into tensor
])
First put these csv The table data in is converted to Python Recognable PILImage Format , Then put their size Change it to the size we need , Finally, all the data will be transformed into what the model can recognize tensor Format .
Then we can start to create Dataset Subclass
# Data reading method 2 : Read in csv Formatted data , Building on its own Dataset class
class FMDataset(Dataset):
# __init__ Is that 1. Pass in external parameters (transform) 2. Define samples,labels
def __init__(self, df, transform=None):
self.df = df # Data sets
self.transform = transform # transformation
self.image = df.iloc[:,1:].values.astype(np.uint8) # Define what is in the dataset training set
self.label = df.iloc[:,0].values # Define what is in the dataset testing set
#__len__ Record and return dataset Medium samples Count
def __len__(self):
return len(self.image)
# __getitem__ Returns the specified index Specific to sample. Here include the image and his label
def __getitem__(self, idx):
image = self.image[idx].reshape(28,28,1) # Will be sample Save it in a variable and set its shape In order to input the model later
label = int(self.label[idx]) # Will be sample Of label Save in variable
# Determine whether to transform operation
if self.transform is not None:
image = self.transform(image)
else:
image = torch.tensor(image/255, dtype=torch.float) # If you don't transform We have to turn the data into tensor In the form of , here /255 In order to put every piece of data pixel The size is controlled in 0-1 Between , Meet the requirements of the model
label = torch.tensor(label, dtype=torch.long)
return image, label
# Import data
import pandas
import io
# Be careful , What we use here is colab, Other environments may need to be fine tuned
train_df = pd.read_csv(io.BytesIO(uploaded['fashion-mnist_train.csv']))
test_df = pd.read_csv(io.BytesIO(uploaded['fashion-mnist_test.csv']))
train_data = FMDataset(train_df, data_transform)
test_data = FMDataset(test_df, data_transform)
In the building Dataset after , We can use DataLoader Class will come Dataset become iterable
# structure DataLoader class , The purpose is to load data during later training and testing .DataLoader It's a iterable! use next(iter()) To go back to the next batch
# Property introduction
# Dataset: Previously defined Dataset
# Batch_size:# sample to load in each batch
# shuffle: according to index load data Do you want to disturb the order every time load None of them are the same
# num_workers: load Number of data processes , The higher the number , The faster the speed.
# drop_last: When load To the last batch,batch Quantity of is not satisfied batch_size when ,drop Drop this batch
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=num_workers, drop_last=True)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=num_workers)
adopt next(iter(train_loader)) We can get one batch The data of . With the following code and visualization, we can more intuitively feel how we can pass DataLoader get data .
# Visual view sample
import matplotlib.pyplot as plt
image, label = next(iter(train_loader)) # The output here is train_loader One of them batch.train_loader It's a iterable object, adopt iter() obtain iterator, use next() You can visit this iterator Next element of
print(image.shape, label.shape)
plt.imshow(image[0][0], cmap='gray') # Show this batch No 0 Pictures . Behind the [0] How to understand ???
model building
By inheritance PyTorchnn Module Module Class to define the model we want . When constructing the model , We are generally right about Module Overloads two functions of :
__init__
- Create model parameters , And define forward calculation
forward
Model initialization
Loss function and optimizer
Training
assessment
Share some of my artificial intelligence learning materials for free , For a long time , Very comprehensive . Including some AI Common framework actual combat video 、 Image recognition 、OpenCV、NLQ、 machine learning 、pytorch、 Computer vision 、 Videos such as deep learning and neural network 、 Courseware source code 、 Famous essence resources at home and abroad 、AI Hot papers 、 Industry reports, etc .
For better systematic learning AI, I recommend that you collect one .
Here are some screenshots , Free download method is attached at the end of the article .
One 、 AI must read books
Two 、 Free video on artificial intelligence courses and projects
3、 ... and 、 Collection of papers on artificial intelligence
Four 、 AI Industry Report
Learn Artificial Intelligence well , Read more , Do more , practice , If you want to improve your level , We must learn to settle down and learn slowly and systematically , Only in the end can we gain something .
Click on the business card below , Scan the code and download the information for free .
边栏推荐
- 900w+ data, from 17s to 300ms, how to operate
- IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
- Docker install and start MySQL service
- C language hashtable/hashset library summary
- redis在服务器linux下的启动的相关命令(安装和配置)
- [learning notes] seckill - seckill project - (11) project summary
- Commands related to the startup of redis under Linux server (installation and configuration)
- The difference between static web pages and dynamic web pages & the difference between Web1.0 and Web2.0 & the difference between get and post
- VS克隆时显示403错误
- Web session management security issues
猜你喜欢
Web会话管理安全问题
js中#号的作用
Téléchargement et installation du client Filezilla
学会pytorch能干什么?
递归:一维链表和数组
[MySQL] the difference between left join, right join and join
Section 26 detailed explanation and demonstration of IPSec virtual private network configuration experiment - simulation experiment based on packettracer8.0
Introduction to mongodb
Hi3536c v100r001c02spc040 cross compiler installation
Ffmpeg download and installation tutorial and introduction
随机推荐
[mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
Message queue addition failure
Basic operations of mongodb [add, delete, modify, query]
Mongodb replication set [master-slave replication]
Use of sigaction
C # webrequest post mode, based on "basic auth" password authentication mode, uploads files and submits other data using multipart / form data mode
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(一)
Introduction to mongodb
UMI route interception (simple and rough)
umi 路由拦截(简单粗暴)
2020-01-01t00:00:00.000000z date format conversion
VS克隆时显示403错误
在 .NET 6 项目中使用 Startup.cs
8.8.2-PointersOnC-20220214
Tidal characteristics of the Bohai Sea and the Yellow Sea
递归:深度优先搜索
C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
[learning notes] seckill - seckill project - (11) project summary
Filter