Search code examples
tensorflowkerasdeep-learningtensorflow2.0autoencoder

How to extract the hidden vector (the output of the ReLU after the third encoder layer) as the image representation


I am implementing an autoencoder using the Fashion Mnsit dataset. The code for the encoder-

class MNISTClassifier(Model):
    def __init__(self):
        super(MNISTClassifier, self).__init__()
        self.encoder = Sequential([
            layers.Dense(128, activation = "relu"),
            layers.Dense(64, activation = "relu"),
            layers.Dense(32, activation = "relu")
        ])
        
        self.decoder = Sequential([
            layers.Dense(64, activation = "relu"), 
            layers.Dense(128, activation= "relu"),
            layers.Dense(784, activation= "relu")
        ])
        
    def call(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded
    
autoencoder = MNISTClassifier()

now I want to train an SVM classifier on the image representations extracted from the above autoencoder mean Once the above fully-connected autoencoder is trained, for each image, I want to extract the 32- dimensional hidden vector (the output of the ReLU after the third encoder layer) as the image representation and then train a linear SVM classifier on the training images of fashion mnist based on the 32- dimensional features.

How to extract the output 32- dimensional hidden vector??

Thanks in Advance!!!!!!!!!!!!


Solution

  • I recommend to use Functional API in order to define multiple outputs of your model because of a more clear code. However, you can do this with Sequential model by getting the output of any layer you want and add to your model's output.

    Print your model.summary() and check your layers to find which layer you want to branch. You can access each layer's output by it's index with model.layers[index].output .

    Then you can create a multi-output model of the layers you want, like this:

     third_layer = model.layers[2]
     last_layer = model.layers[-1]
     my_model = Model(inputs=model.input, outputs=(third_layer.output, last_layer.output))
    

    Then, you can access the outputs of both of layers you have defined:

    third_layer_predict, last_layer_predict = my_model.predict(X_test)