Get the intermediate layer output during training and testing

I have the following demo model:

class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.conv1 = Conv2D(32, 3, activation='relu')
        self.flatten = Flatten()
        self.d1 = Dense(128, activation='relu')
        self.d2 = Dense(10)
    def call(self, x):
        x1 = self.conv1(x)
        x2 = self.flatten(x1)
        x3 = self.d1(x2)
        return self.d2(x3)

# Create an instance of the model
model = MyModel()

During training, I want to get the output of the intermediate layer, for example self.conv1, x2.

Do you have any suggestions?

Yes, Model knows when it’s training or testing, the flag is auto set when you call train() or predict().

@Ramyar_Jahani Hi Ramyar, I want to dump the weights and output of some intermediate layers in some iterations to the local disk. Do you have any suggestions?