11. Model Loading

1 import numpy as np
2 from keras.datasets import mnist
3 from keras.utils import np_utils
4 from keras.models import Sequential
5 from keras.layers import Dense
6 from keras.optimizers import SGD
7 from keras.models import load_model
 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 y_train = np_utils.to_categorical(y_train,num_classes=10)
12 y_test = np_utils.to_categorical(y_test,num_classes=10)
13 
14 # 载入模型
15 model = load_model('model.h5')
16 
17 # 评估模型
18 loss,accuracy = model.evaluate(x_test,y_test)
19 
20 print('\ntest loss',loss)
21 print('accuracy',accuracy)

# Training model 
model.fit (x_train, y_train, the batch_size = 64, epochs = 2 ) 

# assessment model 
Loss, Accuracy = model.evaluate (x_test, android.permission.FACTOR.) 

Print ( ' \ NTEST Loss ' , Loss)
 Print ( ' Accuracy ' , accuracy)

# Save parameters, loading parameters 
model.save_weights ( ' my_model_weights.h5 ' ) 
model.load_weights ( ' my_model_weights.h5 ' )
 # save network structure, the network structure loaded 
from keras.models Import model_from_json 
json_string = model.to_json () 
Model = model_from_json (json_string)
print(json_string)

Guess you like

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