split neural network in two nets preserving weights in python

Viewed 239

In keras I would like to use the model with the initial layers of the structure for a given trained neuralnet with the weights I got for the training process.

Going to the case: Lets imagine we have a dataset df, after spliting into train, dev and test we train a neural network, for this example an autoencoder.

A real piece of code illustrating this concept, without providing data(i didn't consider it necessary):

from keras.models import Model
from keras.layers import Activation, Dense, Dropout, Input

# Define input layer
input_data = Input(shape=(train.shape[1],), name='Input')

# Define encoding layer
encoded = Dense(encoding_dim, activation='relu')(input_data)

# Define decoding layer
decoded = Dense(train.shape[1], activation='sigmoid')(encoded)

# Create the autoencoder model
autoencoder = Model(input_data, decoded, name='Simple AutoEncoder')

#Compile the autoencoder model
autoencoder.compile(optimizer='rmsprop',
                    loss='binary_crossentropy')

autoencoder.fit(train, train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(dev_x, dev_x), verbose=0)

After compile and fit the model we have a neural network with their weights that we got from fitting process.

How could I use only the encoder part of this net by preserving the weight I got?

2 Answers

I believe something along this line should do the trick:

#...all the code from above, including training...

# Define the encoder model
encoder = Model(input_data, encoded, name='Encoder')

The encoder model can be treated as a fully-fledged Keras model (you can save/load/fit/evaluate/predict).

By training an Autoencoder, the encoder neuralnet part would be created with the encoded object that contains the trained weights of the autoencoder.

# Getting the trained weights of the first layer(dense layer of encoder)
weights_ae = autoencoder.layers[1].get_weights()[0]

# The previous code of the example...

# Creating the encoder model
encoder = Model(input_data, encoded, name='Encoder')

# Getting the weights of the encoder model
weights_e = encoder.layers[1].get_weights()[0]

So, finally it would be confirmed that by creating the model encoder would have the weights ("trainied experience") from the autoencoder.

Related