Search code examples
tensorflowinputkerasmodeltraining-data

Take output of intermediate layer as input for model training


Usually we feed a model for training with external data. But I would like to use tensor coming from intermediate layer of the same model as an input for next batch. I believe that this can be acheived by using manual loop for training. This time, I prefer to use fit_generator() from Keras (v2.2.4). I create a mode using Functional API.

Any help are appreciated. Thanks.


Solution

  • This is how I solve my problem.

    model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
    model.metrics_tensors =+ [self.model.get_layer('your_intermediate_layer').output]   # This line is to access the output of a layer during training (what I want)
    

    Then train like this:

    loss_out, ...., your_intermediate_layer_out = model.train_on_batch(X, y)
    

    your_intermediate_layer_out is a numpy array I am looking for during model's training.