I have been trying to save the weights of my neural network model so that I could use a few of its layers for another neural network model to be trained on another dataset.
model = Sequential() model.add(tf.keras.layers.Dense(100, input_shape=(X_train_orig_sm.shape))) model.add(tf.keras.layers.Activation('relu')) model.add(tf.keras.layers.Dropout(0.2)) model.add(tf.keras.layers.Dense(10)) model.add(tf.keras.layers.Activation('relu')) model.add(tf.keras.layers.Dropout(0.2)) model.add(tf.keras.layers.Dense(1)) model.add(tf.keras.layers.Activation('sigmoid')) model.summary() # need sparse otherwise shape is wrong. check why model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Fitting the data to the model') batch_size = 20 epochs = 10 history = model.fit(X_train_orig_sm, Y_train_orig_sm, batch_size=batch_size, epochs=epochs, verbose=1, validation_split=0.2) print('Evaluating the test data on the model')
How I saved the weights of neural network:
How I try to load the weights of neural network:
While trying to load the model, I get the following error:
AttributeError: 'NoneType' object has no attribute 'layers'
I dont seem to understand why the layers of the neural network are not recognised even though the pre-trained neural network is trained before the model was saved. Any advice, solution or direction will be highly appreciated. Thank you.
When you call
model.save_weights("dnn_model.h5"), you only save the "weights" of the model. You do not save the actual structure of the model. That’s why you cannot access the layers etc.
To save the actual model, you can call the below.
# save model.save('dnn_model') # save as pb model.save('dnn_model.h5') # save as HDF5 # load dnn_model = tf.keras.models.load_model('dnn_model') # load as pb dnn_model = tf.keras.models.load_model('dnn_model.h5') # load as HDF5
Note: You do not need to add an extension to the name to save as pb.
Answered By – NanoBit