1. 程式人生 > >mxnet-保存模型參數

mxnet-保存模型參數

pos war init rec .sh 模型 time urn uil

#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Fri Aug 10 16:13:29 2018 @author: myhaspl """ import mxnet as mx import mxnet.ndarray as nd from mxnet import nd, autograd, gluon from mxnet.gluon.data.vision import datasets, transforms from time import time def build_lenet(net): with net.name_scope(): net.add(gluon.nn.Conv2D(channels=6,kernel_size=5,activation="relu"), gluon.nn.MaxPool2D(pool_size=2, strides=2), gluon.nn.Conv2D(channels=16, kernel_size=3, activation="relu"), gluon.nn.MaxPool2D(pool_size=2, strides=2), gluon.nn.Flatten(), gluon.nn.Dense(120, activation="relu"), gluon.nn.Dense(84, activation="relu"), gluon.nn.Dense(10)) return net mnist_train = datasets.FashionMNIST(train=True) X, y = mnist_train[0] print (‘X shape: ‘, X.shape, ‘X dtype‘, X.dtype, ‘y:‘, y,‘Y dtype‘, y.dtype) #x:(height, width, channel) #y:numpy.scalar,標簽 text_labels = [ ‘t-shirt‘, ‘trouser‘, ‘pullover‘, ‘dress‘, ‘coat‘, ‘sandal‘, ‘shirt‘, ‘sneaker‘, ‘bag‘, ‘ankle boot‘ ] #轉換圖像為(channel, height, weight)格式,並且為floating數據類型,通過transforms.ToTensor。 #另外,normalize所有像素值 使用 transforms.Normalize平均值0.13和標準差0.31. transformer = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(0.13, 0.31)]) #只轉換第一個元素,圖像部分。第二個元素為標簽。 mnist_train = mnist_train.transform_first(transformer) #加載批次數據 batch_size = 200 train_data = gluon.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True) #讀取本批數據 i=1 for data, label in train_data: print i print data,label break#沒有這一行,會以每批次200個數據來讀取。 mnist_valid = gluon.data.vision.FashionMNIST(train=False) valid_data = gluon.data.DataLoader(mnist_valid.transform_first(transformer),batch_size=batch_size) #定義網絡 net = build_lenet(gluon.nn.Sequential()) net.initialize(init=mx.init.Xavier()) print net #輸出softmax與誤差 softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss() #定義訓練器 trainer = gluon.Trainer(net.collect_params(), ‘sgd‘, {‘learning_rate‘: 0.1}) def acc(output, label): # output: (batch, num_output) float32 ndarray # label: (batch, ) int32 ndarray return (output.argmax(axis=1) == label.astype(‘float32‘)).mean().asscalar() for epoch in range(10): train_loss, train_acc, valid_acc = 0., 0., 0. tic = time() for data, label in train_data: # 前向與反饋 with autograd.record(): output = net(data) loss = softmax_cross_entropy(output, label) loss.backward() # 換一批樣本數據,更新參數 trainer.step(batch_size) # 計算訓練誤差和正確率 train_loss += loss.mean().asscalar() train_acc += acc(output, label) print "." #測試正確率 for data, label in valid_data: predict_data=net(data) valid_acc += acc(predict_data, label) print("Epoch %d: Loss: %.3f, Train acc %.3f, Test acc %.3f, \ Time %.1f sec" % ( epoch, train_loss/len(train_data), train_acc/len(train_data), valid_acc/len(valid_data), time()-tic)) #保存模型參數,非模型結構 file_name = "net.params" net.save_parameters(file_name)

mxnet-保存模型參數