Saving The Specific Layer From Within A Sequential Keras Model
I am building an auto-encoder and training the model so the targeted output is the same as the input. I am using a sequential Keras model. When I use model.predict I would like it
Solution 1:
After training, create a new model (model2) from your trained model (model) ending in your desired layer.
You can do so either with layer name:
(In model.summary(), your dense's layer 'name' with 256 neurons is dense_5)
from keras.models import Model
model2= Model(model.input,model.get_layer('dense_5').output)
Or with layer order:
(your dense layer with 256 neurons is fifth in model.summary())
from keras.models import Model
model2= Model(model.input,model.layers[4].output)
Then you can use predict
preds=model2.predict(x)
Solution 2:
layer.get_weights()
returns the weights of a layer as a numpy array which can then be saved, for example with np.save
.
To set the weights from a numpy array, layer.set_weights(weights)
can be used.
You can access your layer either by name (model.get_layer(LAYER_NAME)
or by its number (model.layers[LAYER_INDEX]
).
Post a Comment for "Saving The Specific Layer From Within A Sequential Keras Model"