Hi guys,
I’m new to PyTorch and usally write my networks in tensorflow. I have some questions on to correctly do stuff in PyTorch.
Suppose I have a two-layer network called A(x):
class A(nn.Module):
def __init__(self):
super(A, self).__init__()
self.fc1 = nn.Linear(100, 100)
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
return x
Now I need outputs from fc1 and fc2 before applying relu. What is the ‘PyTorch’ way of achieving this? I was thinking of writing something like this:
def hidden_outputs(self, x):
outs = {}
x = self.fc1(x)
outs['fc1'] = x
...
return outs
and then calling A.hidden_outputs(x)
from another script. Also, is it okay to write any function in addition to forward in the class? Can I for example write:
def apply_softmax(self, x):
x = self.forward(x)
x = F.softmax(x)
return x
and use the function above to calculate gradients etc. in another script?
...
net = A()
x = data
out = A.softmax(x)
out.backward()
x_grad = x.grad
...