Hello, I am new to Pytorch, I need to train two different networks, and their inputs are different. I need to multiply their outputs and train them simultaneously, how to do it ?
inputs of net1 works, but inputs of net2 failed. The input data of net2 is a np.array generated by a function I def, how to send it to net2
You could transform the numpy array to a tensor via:
data = torch.from_numpy(array)
Depending on the dtype
of the numpy array you might need to transform the tensor to a FloatTensor
using:
data = data.float()
data = torch.from_numpy(array)
data = data.float()
data = data.cuda()
These I have done, but when y = net2(data)
got y is NoneType
Could you post the definition of net2
?
well a strange net
class Matrix(nn.Module):
def init(self):
super(Matrix,self).init()
self.conv1 = nn.Conv2d(1024,1024,3)
self.conv2 = nn.Conv2d(1024,512,5)
self.conv3 = nn.Conv2d(512,256,5)
self.conv4 = nn.Conv2d(256,128,3)
self.conv5 = nn.Conv2d(128,64,5)
self.pool = nn.AvgPool2d(2,2)
self.fc1 = nn.Linear(64,32)
self.fc2 = nn.Linear(32,10)
def forward(self,x):
x = self.pool(F.relu(self.conv1(x)))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = F.relu(self.conv5(x))
x = x.view(-1,64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
net2 = Matrix()
and input data:
h = hadamard(1024)
h = h.reshape([1,1024,32,32])
h = torch.from_numpy(h)
h = h.float()
h = h.cuda()
y = net2(h)
And I find I have not def a optimizer of net2, is this the reason?
Your forward
method seems to be missing the return
statement, so you should add:
return x
at the end of it.
OMG!! Thx so much
so ridiculous i am, i need to update my glasses