Save the output matrix of every layer of autoencoder

Using PyTorch, I am able to create an autoencoder like the one given below. How do I save the output matrix to a .csv file after every layer?

class Autoencoder(nn.Module):
    def __init__(self, ):
        super(Autoencoder, self).__init__()
        self.fc1 = nn.Linear(10000, 5000)
        self.fc2 = nn.Linear(5000, 2000)
        self.fc3 = nn.Linear(2000, 500)
        self.fc4 = nn.Linear(500, 100)
        self.fc5 = nn.Linear(100, 500)
        self.fc6 = nn.Linear(500, 2000)
        self.fc7 = nn.Linear(2000, 5000)
        self.fc8 = nn.Linear(5000, 10000)
        self.relu = nn.Relu()

    def forward(self, x):
        x = self.relu(self.fc1(x))
        x = self.relu(self.fc2(x))
        x = self.relu(self.fc3(x))
        x = self.relu(self.fc4(x))
        x = self.relu(self.fc5(x))
        x = self.relu(self.fc6(x))
        x = self.relu(self.fc7(x))
        x = self.relu(self.fc8(x))
        return x
1 Like

I think what you are looking for are the forward hooks (callbacks). Their use has been demonstrated in the last lesson of part 1, the CAM notebook.

Can you please send a link?