How to do loss.backward for a model that contains Multiple independent models

Hi, thank for giving attention to my question.
A multimodel class has three independent model(GRU1, GRU2, GRU3) and all these outputs are concatenated then the last single neural network was attached to an ensemble. the code is like below.
But there might be some problems. Although the value of loss was calculated well. At the stage of loss.backward, it raised an error. Is there any solution to backprop multi models? I think torch cannot track tensor to the start point. Where should I fix the code? Is there any example like this model?
Thanks for spending you time to read my question.

class MultiModels (torch.nn.Module):
  def __init__(self,batchsize,hidden_size,hidden_n,dropout=0.5):
    self.batchsize = batchsize
    self.hidden_size = hidden_size
    self.hidden_n = hidden_n
    self.dropout = dropout
    # GRU(feature 수,output_size,num_layer)
    self.GRU1 = torch.nn.GRU(3,hidden_size,hidden_n,batch_first=True)
    self.GRU2 = torch.nn.GRU(2,hidden_size,hidden_n,batch_first=True)
    self.GRU3 = torch.nn.GRU(2,hidden_size,hidden_n,batch_first=True)
    self.ensemble_layer1 = torch.nn.Linear(3*5,7)
    self.ensemble_layer2 = torch.nn.Linear(7,1)
def forward(self,input,hidden):
    out1,hidden1 = self.GRU1(input,hidden.clone())
    out1 = out1.float()
    out2,hidden2 = self.GRU2(input[:,:,[0,1]],hidden.clone())
    out2 = out2.float()
    out3,hidden3 = self.GRU3(input[:,:,[1,2]],hidden.clone())
    out3 = out3.float()
    total_out =[:,-1,:],out2[:,-1,:],out3[:,-1,:]),1)
    out = self.ensemble_layer1(total_out)
    out = self.ensemble_layer2(out)
    out = out.view(self.batchsize,-1)
    return out

def init_hidden(self):
    hidden = torch.Tensor(torch.zeros(self.hidden_n,self.batchsize,self.hidden_size)).cuda()
    return hidden

model = MultiModels(16,5,2)
loss_f = BCEwithLogitLoss()
optim = Adam(model.parameters(),lr=0.001)

 ------training session------
 hidden = model.init_hidden()
 pred = model(input,hidden)
 loss = loss_f(pred,label)
 loss.backward()  # Error!

Could you paste the error?

oh I missed the explanation of an error. I added it. thank u!

You haven’t call model’s forward function in any place of your code. You should do

model = MultiModels(16,5,2)
output = model(your_input)

and then using output to compute the loss.

you are right. I added full code. thank you
but the same error also occur…

This error is pretty rare since I have googled it and got nothing.
Could you remove the three float() in forward and try again? The data type in the whole neural network should be the same, at least I think so.

strangely It works on only CPU
not on GPU

You should keep everything the same type in your neural net, from input to output, and also the weights. You could use torch.set_default_tensor_type('torch.FloatTensor') to set a default tensor type. If you want to train with GPU, you should make sure everything is torch.cuda.FloatTensor type.

Thank you I didn’t know torch.set_default_tensor_type.
I will try it