Keras Framework Training Model preservation and re-loading
Experimental data mnist The Initial training model and save
Import NumPy as NP from keras.datasets import mnist from keras.utils import np_utils from keras.models import sequential F Rom keras.layers import dense from keras.optimizers import SGD # Load data (X_train,y_train), (x_test,y_test) = Mnist.load_data () # (60000,28,28) print (' X_shape: ', X_train.shape) # (60000) print (' Y_shape: ', Y_train.shape) # (60000,28,28)--( 60000,784) X_train = X_train.reshape (x_train.shape[0],-1)/255.0 x_test = X_test.reshape (x_test.shape[0],-1)/255.0 # Change one hot format Y_train = np_utils.to_categorical (y_train,num_classes=10) y_test = np_utils.to_categorical (y_test,num_ CLASSES=10) # Create model, enter 784 neurons, output 10 neurons model = sequential ([Dense (units=10,input_dim=784,bias_initializer= ' one ', AC tivation= ' Softmax ')] # define Optimizer SGD = SGD (lr=0.2) # define optimizer, loss function, calculate accuracy rate during training model.compile (optimizer = SGD , loss = ' MSE ', metrics=[' accuracy '), # Training Model Model.fit (X_TRAIN,Y_TRAIN,BATCH_SIZE=64,EPOCHS=5) # Evaluation Model LOSS,ACC Uracy = Model.evaluate (x_test,y_test) print (' \Ntest loss ', loss) print (' accuracy ', accuracy) # Save Model Model.save (' Model.h5 ') # HDF5 file, pip install H5py
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
load the model of the first training, then train
Import NumPy as NP from keras.datasets import mnist from keras.utils import np_utils from keras.models import sequential F Rom keras.layers import dense from keras.optimizers import SGD from keras.models import Load_model # load data (x_train,y_train ), (x_test,y_test) = Mnist.load_data () # (60000,28,28) print (' X_shape: ', X_train.shape) # (60000) print (' Y_shape: ', Y_ Train.shape) # (60000,28,28), (60000,784) X_train = X_train.reshape (x_train.shape[0],-1)/255.0 x_test = X_ Test.reshape (x_test.shape[0],-1)/255.0 # change one hot format Y_train = np_utils.to_categorical (y_train,num_classes=10) y_test = Np_utils.to_categorical (y_test,num_classes=10) # Load model = Load_model (' model.h5 ') # evaluation Model Loss,accuracy = Model.evalua Te (x_test,y_test) print (' \ntest loss ', loss) print (' accuracy ', accuracy) # Training model Model.fit (x_train,y_train,batch_size=
64,epochs=2) # evaluation Model loss,accuracy = Model.evaluate (x_test,y_test) print (' \ntest loss ', loss) print (' accuracy ', accuracy) # save parameter, load parameter model.save_weights (' My_model_weights.h5 ') model.load_weights (' My_model_weights.h5 ') # Save network structure, load network structure from keras.models import Model_from_json json_string = model . To_json () model = Model_from_json (json_string) print (json_string)
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.