Keras Framework Training Model Preservation and onboarding continuation training

Source: Internet
Author: User
Tags keras

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.