Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save the encoded output in Keras

I want to save the encoded part just before decoder stage in autoencoder model of Keras with Tensorflow backend.

For instance;

encoding_dim = 210
input_img = Input(shape=(5184,))

encoded = Dense(2592, activation='relu')(input_img)
encoded1 = Dense(encoding_dim, activation='relu')(encoded)

decoded = Dense(encoding_dim, activation='relu')(encoded1)
decoded = Dense(5184, activation='sigmoid')(decoded)

I want to save the encoded1 as csv file after the autoencoder training. Suppose the output of Dense will be (nb_samples, output_dim).

Thank you

like image 776
Nufa Avatar asked Feb 27 '17 12:02

Nufa


1 Answers

Try:

autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded1)

autoencoder.compile(loss=my_loss, optimizer=my_optimizer)
autoencoder.fit(my_data, my_data, .. rest of fit params)

numpy.savetxt("encoded1.csv", encoder.predict(x), delimiter=",")

Moreover - I don't know what kind of data do you have but I advise you to use linear activation is a last layer and mse loss function.

like image 94
Marcin Możejko Avatar answered Oct 08 '22 01:10

Marcin Możejko