# Output trained parameters of Keras model

If I am training on a GRU model, is there a way I can output the learnt parameters so that when I train next time with more data, I can initialize with those learnt parameters as a starting point?

There is a way to do this. See the documentation! It is very good.

I assume you mean that you build a network using Keras (which contains recurrent GRU layers) and would like to save the model after some training, then restart from the same point e.g. with new data or just to push the model further.

### Example

Assuming you want to analyse some images of shape (150, 150, 3):

from keras.models import Model, load_model
from keras.layers import Input, GRU, Dense

my_model = Model()

my_input = Input(shape=(150, 150, 3))    # using Tensorflow's dims: 'channels last'
gru_layer - GRU()(input)
output = Dense(32)(gru_layer)

# tell the Model class which layers are the start and end of the model
model = Model(inputs=my_input, outputs=my_output)

# save it!
my_model.save('my_model.h5')

# some time later.... a new python session

# continue using the model


### Explanation

At the point the model is built, we can compile and train it the usual way e.g. my_model.fit(...)

Once training is finished, we can save the model two different ways:

1. just the model weights, using my_model.save_weights('my_weights.h5')
2. the whole model and its meta data, using my_model.save('my_model.h5')

In either case, you will need the library h5py (see the documentation)

Option 2 preserves more information from the model. Quoting the documentation, it saves:

• the architecture of the model, allowing to re-create the model
• the weights of the model
• the training configuration (loss, optimizer)
• the state of the optimizer, allowing to resume training exactly where you left off.

To continue on the with model where you ended and saved, it is as simple as:

my_model = keras.models.load_model('my_models.h5')


Now you can train it further or introspect the model and so on.