How does pytorch extract the results after each convolution?

I want to visualize the reult after each convolution layer and full connected layer?
Can anybody show me how to achieve this?please help me…:ok_man:
For this example :
#coding=utf8
import torch
from torch import nn
import torch.nn.functional as F
from torch import optim
from numpy import pi, sin, linspace
import numpy as np
import torch.utils.data as Data

#1
torch.manual_seed(1)
learning_rate=1e-2
num_epoches=10

#2

t = torch.linspace(0, 1, 1024*256)
modes = torch.sin(2 * pi * 5 * t)
label=torch.ones(256).long()
modes.requires_grad_(True)
X=modes.view(256,1,1024)

#3
class CNN_1D(nn.Module):
def init(self,in_dim,n_class):
super(CNN_1D,self).init()
self.conv1=nn.Sequential(
nn.Conv1d(in_dim,512,3,padding=3),
nn.LeakyReLU(),
nn.MaxPool1d(2,2),
nn.Conv1d(512,128,3,padding=3),
nn.LeakyReLU(),
nn.MaxPool1d(2,2)
)
self.fc=nn.Sequential(
nn.Linear(128*259,640),
nn.LeakyReLU(),
nn.Linear(640,160),
nn.Dropout(),
nn.Linear(160,n_class)
)
def forward(self,x):
out =self.conv1(x)
out =out.view(x.size(0),-1)
out =self.fc(out)
return out

net =CNN_1D(1,2)
print(net)

criterion =nn.CrossEntropyLoss()
optimzer =optim.Adam(net.parameters())

#4
for epoch in range(num_epoches):
out =net(X)
loss =criterion(out,label)
_,pred=torch.max(out,1)
num_correct = (pred==label).sum().item()
running_acc = num_correct/X.size(0)
optimzer.zero_grad()
loss.backward()
optimzer.step()
print(“Trian:{} epoch ;Loss:{:.6%} ; Acc:{:.6%}”.format(epoch+1,loss.item(),running_acc))
print("\n")

I think this would be a job for forward hooks. This thread might be of interest to you. Here are the docs on how to use forward hooks.

In essence how forward hooks work:

  1. Create a hooking function which has inputs function(module, input, output)
  2. Register the forward hook to some module (Note: all pytorch fuctions and layers subclass the Module class)
  3. When the forward function is called on the module which has a forward hook registered the function you registered will be caled with the parameters: (module, module_input, module_output)
  4. Within the function you can intercept the module_otuput and do whatever visualizations you like on the model_output values. Note: The hooking functions should not modify the output, so I suggest you copy the data to some other tensor if you want to process the data in some way before visualizing