user3043636
user3043636

Reputation: 579

Print/Save autoencoder generated features in Keras

I have this autoencoder:

input_dim = Input(shape=(10,))
encoded1 = Dense(30, activation = 'relu')(input_dim)
encoded2 = Dense(20, activation = 'relu')(encoded1)
encoded3 = Dense(10, activation = 'relu')(encoded2)
encoded4 = Dense(6, activation = 'relu')(encoded3)
decoded1 = Dense(10, activation = 'relu')(encoded4)
decoded2 = Dense(20, activation = 'relu')(decoded1)
decoded3 = Dense(30, activation = 'relu')(decoded2)
decoded4 = Dense(ncol, activation = 'sigmoid')(decoded3)
autoencoder = Model(input = input_dim, output = decoded4) 
autoencoder.compile(-...)
autoencoder.fit(...)

Now I would like print or save the features generate in encoded4. Basically, starting from a huge dataset I would like to extract the features generated by autoencoder, after the training part, to obtain a restricted representation of my dataset.

Could you help me?

Upvotes: 5

Views: 1196

Answers (2)

user3043636
user3043636

Reputation: 579

So, basically, by creating an encoder like this:

 encoder = Model (input_dim,encoded4)
 encoded_input=Input(shape=(6,))

and then using:

 encoded_data=encoder.predict(data)

where the data within the predict function is the dataset, the output generate by

 print encoded_data

is the restricted representation of my dataset.

It is right?

Thanks

Upvotes: 3

Nassim Ben
Nassim Ben

Reputation: 11543

You can do it by creating the "encoder" model:

encoder = Model(input = input_dim, output = encoded4)

This will use the same layers instances that you trained with the autoencoder and should be producing the feature if you use it in "inference mode" like encoder.predict()

I hope this helps :)

Upvotes: 4

Related Questions