10. Save the model

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
 1 # 载入数据
 2 (x_train,y_train),(x_test,y_test) = mnist.load_data()
 3 # (60000,28,28)
 4 print('x_shape:',x_train.shape)
 5 # (60000)
 6 print('y_shape:',y_train.shape)
 7 # (60000,28,28)->(60000,784)
 8 x_train = x_train.reshape(x_train.shape[0],-1)/255.0
 9 x_test = x_test.reshape(x_test.shape[0],-1)/255.0
10 # 换one hot格式
11= np_utils.to_categorical y_train (y_train, num_classes = 10 )
 12 is android.permission.FACTOR. = np_utils.to_categorical (android.permission.FACTOR., num_classes = 10 )
 13 is  
14  # create a model neuron input 784, the output neurons 10 
15 Model = the Sequential ([
 16          the Dense (Units = 10, input_dim = 784, bias_initializer = ' One ' , Activation = ' SoftMax ' )
 . 17      ])
 18 is  
. 19  # define the optimizer 
20 is SGD = the SGD (LR = 0.2 )
 21 is  
22 is  # define optimizer, loss function, training calculation accuracy of the process 
23 is  model.compile (
 24     optimizer = sgd,
25     loss = 'mse',
26     metrics=['accuracy'],
27 )
28 
29 # 训练模型
30 model.fit(x_train,y_train,batch_size=64,epochs=5)
31 
32 # 评估模型
33 loss,accuracy = model.evaluate(x_test,y_test)
34 
35 print('\ntest loss',loss)
36 print('accuracy',accuracy)
37 
38  # stored model 
39 model.save ( ' model.h5 ' )    # the HDF5 file, pip install h5py

Guess you like

Origin www.cnblogs.com/liuwenhua/p/11567019.html