class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
#self.fc1 = nn.Linear(16 * 5 * 5, 120) # 5*5 from image dimension
self.fc1 = nn.Linear(13456, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 1)
def forward(self, x):
# Max pooling over a (2, 2) window
print('x', str(x.size()))
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
print('x', str(x.size()))
#ts.show(x,ncols=4)
# If the size is a square, you can specify with a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = torch.flatten(x, 1) # flatten all dimensions except the batch dimension
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
activation = {}
def get_activation(name):
def hook(model, input, output):
activation[name] = output.detach()
return hook
#model = Net()
model = Net()
model.fc2.register_forward_hook(get_activation(‘conv1’))
x = torch.randn(1,6)
output = model(x)
print(activation[‘conv1’])
for the above code ,since i want conv1 features gave x = torch.randn(1,6)
but got this error RuntimeError: Expected 4-dimensional input for 4-dimensional weight [6, 1, 5, 5], but got 2-dimensional input of size [1, 6] instead