主要组成模块
数据读取
使用自带数据集 导入自己收集的数据,然后转换为合适的Dataset
Dataset DataLoader 按需求生成特定数据集 生成便于训练和测试的加载数据(实际用于模型的数据)
from torchvision import transforms, datasets
image_size = 28
data_transform = transforms. Compose( [
transforms. ToPILImage( ) ,
transforms. Resize( image_size) ,
transforms. ToTensor( ) ,
] )
train_data = datasets. FashionMNIST( root= './' , train= True , download= True , transform= data_transform)
test_data = datasets. FashionMNIST( root= './' , train= False , download= True , transform= data_transform)
class FMDataset ( Dataset) :
def __init__ ( self, df, transform= None ) :
self. df = df
self. transform = transform
self. images = df. iloc[ : , 1 : ] . values. astype( np. uint8)
self. labels = df. iloc[ : , 0 ] . values
def __len__ ( self) :
return len ( self. images)
def __getitem__ ( self, idx) :
image = self. images[ idx] . reshape( 28 , 28 , 1 )
label = int ( self. labels[ idx] )
if self. transform is not None :
image = self. transform( image)
else :
image = torch. tensor( image/ 255. , dtype= torch. float )
label = torch. tensor( label, dtype= torch. long )
return image, label
train_df = pd. read_csv( './data/images/FashionMNIST/fashion-mnist_train.csv' )
test_df = pd. read_csv( "./data/images/FashionMNIST/fashion-mnist_test.csv" )
train_data = FMDataset( train_df, data_transform)
test_data = FMDataset( test_df, data_transform)
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
模型设计
基本定义方法
Sequential ModuleList/ModuleDict 直接搭建网络,定义顺序即为模型连接顺序 List/Dict中元素顺序并不代表其网络中的真实位置顺序,需要forward函数指定各个层的连接顺序 模型中间无法加入外部输入 模型中间需要之前层的信息
import torch. nn as nn
import collections
class Net ( nn. Module) :
def __init__ ( self) :
super ( Net, self) . __init__( )
self. conv = nn. Sequential(
nn. Conv2d( 1 , 32 , 5 ) ,
nn. ReLU( ) ,
nn. MaxPool2d( 2 , stride= 2 ) ,
nn. Dropout( 0.3 ) ,
nn. Conv2d( 32 , 64 , 5 ) ,
nn. ReLU( ) ,
nn. MaxPool2d( 2 , stride= 2 ) ,
nn. Dropout( 0.3 )
)
self. fc = nn. Sequential( collections. OrderedDict( [
( 'fc1' , nn. Linear( 64 * 4 * 4 , 512 ) ) ,
( 'relu1' , nn. ReLU( ) ) ,
( 'fc2' , nn. Linear( 512 , 10 ) )
] )
)
def forward ( self, x) :
x = self. conv( x)
x = x. view( - 1 , 64 * 4 * 4 )
x = self. fc( x)
return x
model = Net( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
通过nn.ModuleList()/nn.ModuleDict()
class model ( nn. Module) :
def __init__ ( self) :
super ( ) . __init__( )
self. modulelist = nn. ModuleList( [ nn. Linear( 784 , 256 ) , nn. ReLU( ) , nn. Linear( 256 , 10 ) ] )
def forward ( self, x) :
for layer in self. modulelist:
x = layer( x)
return x
class model ( nn. Module) :
def __init__ ( self) :
super ( ) . __init__( )
self. moduledict = nn. ModuleDict( {
'linear' : nn. Linear( 784 , 256 ) ,
'act' : nn. ReLU( ) ,
'output' : nn. Linear( 256 , 10 )
} )
def forward ( self, x) :
for layer in self. moduledict:
x = layer( x)
return x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
常见损失函数及原理 常见损失函数(分类与回归) CTC损失函数详解
常见损失函数 代码 功能 二分类交叉熵损失函数 .nn.BCELoss 计算二分类任务时的交叉熵 交叉熵损失函数 .nn.CrossEntropyLoss 计算交叉熵 L1损失函数 nn.L1Loss 计算输出y和真实标签之间的差值绝对值 MSE损失函数 nn.MSELoss 计算输出y和真实标签之差的平方 smooth L1 nn.SmoothL1Loss L1的平滑输出,其功能是减轻离群点带来的影响 目标泊松分布的负对数似然损失 nn.PoissonNLLLoss 泊松分布的负对数似然损失函数 Kl散度 nn.KLDIvLoss 计算相对熵 MarginRanKingLoss nn.MarginRanKingLoss 计算两向量之间的相似度,用于排序任务 多标签边界损失函数 nn.MultiLabelMarginLoss 用于多标签分类问题计算损失函数 二分类损失函数 nn.SoftMarginLoss 计算二分类的logistic损失 多分类的折页损失 nn.MultiMarginLoss 计算多分类的折页损失 三元组损失 nn.TripletMarginLoss 计算三元组损失 HingEmbeddingLoss nn.HingEmbeddingLoss 对输出的结果做Hing损失计算 余弦相似度 nn.CosineEmbeddingLoss 对两个向量做余弦相似度 CTC损失函数 nn.CTCLoss 用于解决时序类数据分类
m = nn. Sigmoid( )
loss = nn. BCELoss( )
input = torch. randn( 3 , requires_grad= True )
target = torch. empty( 3 ) . random_( 2 )
output = loss( m( input ) , target)
output. backward( )
loss = nn. CrossEntropyLoss( )
input = torch. randn( 3 , 5 , requires_grad= True )
target = torch. empty( 3 , dtype= torch. long ) . random_( 5 )
output = loss( input , target)
output. backward( )
loss = nn. L1Loss( )
input = torch. randn( 3 , 5 , requires_grad= True )
target = torch. randn( 3 , 5 )
output = loss( input , target)
output. backward( )
loss = nn. MSELoss( )
input = torch. randn( 3 , 5 , requires_grad= True )
target = torch. randn( 3 , 5 )
output = loss( input , target)
output. backward( )
loss = nn. SmoothL1Loss( )
input = torch. randn( 3 , 5 , requires_grad= True )
target = torch. randn( 3 , 5 )
output = loss( input , target)
output. backward( )
loss = nn. PoissonNLLLoss( )
log_input = torch. randn( 5 , 2 , requires_grad= True )
target = torch. randn( 5 , 2 )
output = loss( log_input, target)
output. backward( )
inputs = torch. tensor( [ [ 0.5 , 0.3 , 0.2 ] , [ 0.2 , 0.3 , 0.5 ] ] )
target = torch. tensor( [ [ 0.9 , 0.05 , 0.05 ] , [ 0.1 , 0.7 , 0.2 ] ] , dtype= torch. float )
loss = nn. KLDivLoss( )
output = loss( inputs, target)
loss = nn. MarginRankingLoss( )
input1 = torch. randn( 3 , requires_grad= True )
input2 = torch. randn( 3 , requires_grad= True )
target = torch. randn( 3 ) . sign( )
output = loss( input1, input2, target)
output. backward( )
loss = nn. MultiLabelMarginLoss( )
x = torch. FloatTensor( [ [ 0.9 , 0.2 , 0.4 , 0.8 ] ] )
y = torch. LongTensor( [ [ 3 , 0 , - 1 , 1 ] ] )
output = loss( x, y)
inputs = torch. tensor( [ [ 0.3 , 0.7 ] , [ 0.5 , 0.5 ] ] )
target = torch. tensor( [ [ - 1 , 1 ] , [ 1 , - 1 ] ] , dtype= torch. float )
loss_f = nn. SoftMarginLoss( )
output = loss_f( inputs, target)
inputs = torch. tensor( [ [ 0.3 , 0.7 ] , [ 0.5 , 0.5 ] ] )
target = torch. tensor( [ 0 , 1 ] , dtype= torch. long )
loss_f = nn. MultiMarginLoss( )
output = loss_f( inputs, target)
triplet_loss = nn. TripletMarginLoss( margin= 1.0 , p= 2 )
anchor = torch. randn( 100 , 128 , requires_grad= True )
positive = torch. randn( 100 , 128 , requires_grad= True )
negative = torch. randn( 100 , 128 , requires_grad= True )
output = triplet_loss( anchor, positive, negative)
output. backward( )
loss_f = nn. HingeEmbeddingLoss( )
inputs = torch. tensor( [ [ 1. , 0.8 , 0.5 ] ] )
target = torch. tensor( [ [ 1 , 1 , - 1 ] ] )
output = loss_f( inputs, target)
loss_f = nn. CosineEmbeddingLoss( )
inputs_1 = torch. tensor( [ [ 0.3 , 0.5 , 0.7 ] , [ 0.3 , 0.5 , 0.7 ] ] )
inputs_2 = torch. tensor( [ [ 0.1 , 0.3 , 0.5 ] , [ 0.1 , 0.3 , 0.5 ] ] )
target = torch. tensor( [ [ 1 , - 1 ] ] , dtype= torch. float )
output = loss_f( inputs_1, inputs_2, target)
T = 50
C = 20
N = 16
S = 30
S_min = 10
input = torch. randn( T, N, C) . log_softmax( 2 ) . detach( ) . requires_grad_( )
target = torch. randint( low= 1 , high= C, size= ( N, S) , dtype= torch. long )
input_lengths = torch. full( size= ( N, ) , fill_value= T, dtype= torch. long )
target_lengths = torch. randint( low= S_min, high= S, size= ( N, ) , dtype= torch. long )
ctc_loss = nn. CTCLoss( )
loss = ctc_loss( input , target, input_lengths, target_lengths)
loss. backward( )
T = 50
C = 20
N = 16
input = torch. randn( T, N, C) . log_softmax( 2 ) . detach( ) . requires_grad_( )
input_lengths = torch. full( size= ( N, ) , fill_value= T, dtype= torch. long )
target_lengths = torch. randint( low= 1 , high= T, size= ( N, ) , dtype= torch. long )
target = torch. randint( low= 1 , high= C, size= ( sum ( target_lengths) , ) , dtype= torch. long )
ctc_loss = nn. CTCLoss( )
loss = ctc_loss( input , target, input_lengths, target_lengths)
loss. backward( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
优化器
6种优化器 梯度下降法及优化算法 ASGD 所有优化算法均继承于Optimizer
Optimizer
class Optimizer ( object ) :
def __init__ ( self, params, defaults) :
self. defaults = defaults
self. state = defaultdict( dict )
self. param_groups = [ ]
import os
import torch
weight = torch. randn( ( 2 , 2 ) , requires_grad= True )
weight. grad = torch. ones( ( 2 , 2 ) )
optimizer = torch. optim. SGD( [ weight] , lr= 0.1 , momentum= 0.9 )
optimizer. step( )
optimizer. zero_grad( )
print ( "optimizer.params_group is \n{}" . format ( optimizer. param_groups) )
weight2 = torch. randn( ( 3 , 3 ) , requires_grad= True )
optimizer. add_param_group( { "params" : weight2, 'lr' : 0.0001 , 'nesterov' : True } )
opt_state_dict = optimizer. state_dict( )
for _ in range ( 50 ) :
optimizer. step( )
print ( "state_dict after step:\n" , optimizer. state_dict( ) )
torch. save( optimizer. state_dict( ) , os. path. join( r".\", " optimizer_state_dict. pkl") )
state_dict = torch. load( r".\optimizer_state_dict.pkl" )
optimizer. load_state_dict( state_dict)
print ( "\n{}" . format ( optimizer. defaults) )
print ( "\n{}" . format ( optimizer. state) )
print ( "\n{}" . format ( optimizer. param_groups) )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
训练和评估
def train ( epoch) :
model. train( )
train_loss = 0
for data, label in train_loader:
optimizer. zero_grad( )
output = model( data)
loss = criterion( label, output)
loss. backward( )
optimizer. step( )
train_loss += loss. item( ) * data. size( 0 )
train_loss = train_loss/ len ( train_loader. dataset)
print ( 'Epoch: {} \tTraining Loss: {:.6f}' . format ( epoch, train_loss) )
def val ( epoch) :
model. eval ( )
val_loss = 0
gt_labels = [ ]
pred_labels = [ ]
with torch. no_grad( ) :
for data, label in test_loader:
output = model( data)
preds = torch. argmax( output, 1 )
gt_labels. append( label. cpu( ) . data. numpy( ) )
pred_labels. append( preds. cpu( ) . data. numpy( ) )
loss = criterion( output, label)
val_loss += loss. item( ) * data. size( 0 )
val_loss = val_loss/ len ( test_loader. dataset)
gt_labels, pred_labels = np. concatenate( gt_labels) , np. concatenate( pred_labels)
acc = np. sum ( gt_labels== pred_labels) / len ( pred_labels)
print ( 'Epoch: {} \tValidation Loss: {:.6f}, Accuracy: {:6f}' . format ( epoch, val_loss, acc) )
for epoch in range ( 1 , epochs+ 1 ) :
train( epoch)
val( epoch)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
参考
模型设计 pytorch中文文档
实战练习项目
FashionMNIST