2

I am implementing an autoencoder using the Fashion MNIST 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?

2
  • 1
    You can use enc_outputs = classifier.encoder.predict( images ) where classifier is an instance of MNISTClassifier. Commented May 31, 2021 at 13:56
  • and if I want output from any intermediate layer like from a layer with 64 neurons then what to do Commented May 31, 2021 at 14:16

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.