上一篇,我们学习了TensorFlow的基本使用方法,这一次我们尝试使用昇思框架。
下载:进入官网下载地址 ,选择适合自己的版本,根据命令安装即可。建议在anaconda中使用pip安装。
import os
from matplotlib import pyplot as plt
import numpy as np
import mindspore as ms
import mindspore. context as context
import mindspore. dataset as ds
import mindspore. dataset. transforms. c_transforms as C
import mindspore. dataset. vision. c_transforms as CV
from mindspore. nn. metrics import Accuracy
from mindspore import nn
from mindspore. train import Model
from mindspore. train. callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
数据集相关API库:mindspore.dataset
构建神经网络层相关API库:mindspore.nn
训练模型相关API库: mindspore.train
context. set_context( mode= context. GRAPH_MODE, device_target= 'CPU' )
mode: 设置为context.PYNATIVE_MODE
是动态图模式,context.GRAPH_MODE
是静态图模式。
设置数据集和训练集路径,并使用MnistDataset()函数读取
DATA_DIR_TRAIN = "./datasets/MNIST_Data/train"
DATA_DIR_TEST = "./datasets/MNIST_Data/test"
ds_train = ds. MnistDataset( DATA_DIR_TRAIN)
ds_test = ds. MnistDataset( DATA_DIR_TEST)
print ( '训练数据集数量:' , ds_train. get_dataset_size( ) )
print ( '测试数据集数量:' , ds_test. get_dataset_size( ) )
image= ds_train. create_dict_iterator( ) . __next__( )
print ( '图像长/宽/通道数:' , image[ 'image' ] . shape)
print ( '一张图像的标签样式:' , image[ 'label' ] )
def create_dataset ( training= True , batch_size= 128 , resize= ( 28 , 28 ) ,
rescale= 1 / 255 , shift= 0 , buffer_size= 64 ) :
ds = ms. dataset. MnistDataset( DATA_DIR_TRAIN if training else DATA_DIR_TEST)
resize_op = CV. Resize( resize)
rescale_op = CV. Rescale( rescale, shift)
hwc2chw_op = CV. HWC2CHW( )
ds = ds. map ( input_columns= "image" , operations= [ rescale_op, resize_op, hwc2chw_op] )
ds = ds. map ( input_columns= "label" , operations= C. TypeCast( ms. int32) )
ds = ds. shuffle( buffer_size= buffer_size)
ds = ds. batch( batch_size, drop_remainder= True )
return ds
ds = create_dataset( training= False )
data = ds. create_dict_iterator( ) . __next__( )
images = data[ 'image' ] . asnumpy( )
labels = data[ 'label' ] . asnumpy( )
plt. figure( figsize= ( 15 , 5 ) )
for i in range ( 1 , 11 ) :
plt. subplot( 2 , 5 , i)
plt. imshow( np. squeeze( images[ i] ) )
plt. title( 'Number: %s' % labels[ i] )
plt. xticks( [ ] )
plt. show( )
接下来创建模型,模型定义方法使用类的定义,调用nn中的函数进行网络层构建。类中需要定义__init__
和construct
方法。这里先展示从mindspore官网找到的定义方法:
class ForwardNN ( nn. Cell) :
def __init__ ( self) :
super ( ForwardNN, self) . __init__( )
self. flatten = nn. Flatten( )
self. fc1 = nn. Dense( 784 , 512 , activation= 'relu' )
self. fc2 = nn. Dense( 512 , 128 , activation= 'relu' )
self. fc3 = nn. Dense( 128 , 10 , activation= None )
def construct ( self, input_x) :
output = self. flatten( input_x)
output = self. fc1( output)
output = self. fc2( output)
output = self. fc3( output)
return output
lr = 0.001
num_epoch = 10
momentum = 0.9
net = ForwardNN( )
loss = nn. loss. SoftmaxCrossEntropyWithLogits( sparse= True , reduction= 'mean' )
metrics= { "Accuracy" : Accuracy( ) }
opt = nn. Adam( net. trainable_params( ) , lr)
model = Model( net, loss, opt, metrics)
config_ck = CheckpointConfig( save_checkpoint_steps= 1875 , keep_checkpoint_max= 10 )
ckpoint_cb = ModelCheckpoint( prefix= "checkpoint_net" , directory = "./ckpt" , config= config_ck)
ds_eval = create_dataset( False , batch_size= 32 )
ds_train = create_dataset( batch_size= 32 )
loss_cb = LossMonitor( per_print_times= 1875 )
time_cb = TimeMonitor( data_size= ds_train. get_dataset_size( ) )
print ( "============== Starting Training ==============" )
model. train( num_epoch, ds_train, callbacks= [ ckpoint_cb, loss_cb, time_cb ] , dataset_sink_mode= False )
metrics= model. eval ( ds_eval)
print ( metrics)
在使用上,MindSpore和TensorFlow以及Pytorch均无太大区别,主要在于不同库函数的区别上。因此,建议学会使用一种框架,后面在需要用到其他框架的时候可以很快上手。在此之后,我们会练习使用TensorFlow或者MindSpore进行经典网络的搭建,请重点放在经典网络的结构特点上而不是框架本身的语法上。