Hi everyone, I am new user on pytorch. Currently i’m trying to implement an LSTM and GRU model. The LSTM training is doing well, but when i switch into GRU training im facing some issues.
Any advice?

AttributeError                             Traceback (most recent call last)
 in 
---- > 1  train ( modelGRU , train_list , val_list , path , seq , epoch , batch_size , criterionGRU , optimizerGRU , model_type = “GRU” )
 in train (model, train_list, val_list, path, seq, epoch, batch_size, criterion, optimizer, model_type)
52  # print(x.size(),hidden[0].size() )
53              if model_type ==  “GRU” :
—> 54                  hidden = hidden . data
55              else :
56                  hidden = tuple ( [ e . data for e in hidden ] )
AttributeError :‘tuple’ object has no attribute’data’
class GRUNet(nn.Module):
    def __init__(self,in_size,h_size,n_layers,fc_out,out_size,dropout=0.5):
        super(GRUNet, self).__init__()   
        self.gru = nn.GRU(input_size=in_size,hidden_size=h_size,num_layers=n_layers,dropout=dropout,bias=False)
        self.fc = nn.Linear(in_features=h_size,out_features=fc_out,bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.out = nn.Linear(in_features=fc_out,out_features=out_size,bias=False)
        self.tanh = nn.Tanh()        
    def forward(self, x, hidden):
        out, hidden = self.gru(x, hidden)
        x = self.fc(x)
        x = self.relu(x)
        x = self.out(x)
        x = self.tanh(x)
        return x, hidden

class MyLstm(nn.Module):
    def __init__(self,in_size,h_size,n_layers,fc_out,out_size,dropout=0.5):
        super(MyLstm, self).__init__()
        self.lstm = nn.LSTM(input_size=in_size,hidden_size=h_size,num_layers=n_layers,dropout=dropout,bias=False)
        self.fc = nn.Linear(in_features=h_size,out_features=fc_out,bias=False)
        self.relu = nn.ReLU(inplace=True)
        self.out = nn.Linear(in_features=fc_out,out_features=out_size,bias=False)
        self.tanh = nn.Tanh()
    def forward(self,x,hidden):
        x, hidden = self.lstm(x,hidden)
#         x = x[-1:]
        x = self.fc(x)
        x = self.relu(x)
        x = self.out(x)
        x = self.tanh(x)
        return x, hidden

def train(model,train_list,val_list,path,seq,epoch,batch_size,criterion,optimizer,model_type):
    for e in range(epoch):
        train_data = load_data(train_list,batch_size)
        a_loss = 0
        a_size = 0
        model.train()
        for x,y in train_data:
            x,y = x.to(device),y.to(device)
            bs = x.size()[1]
            h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
            h2 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
            hidden = (h1,h2)
#             hidden = (hidden[0].detach(),hidden[1].detach())
#             print(x.size(),hidden[0].size())
            if model_type == "GRU":
                hidden = hidden.data
            else:
                hidden = tuple([e.data for e in hidden])
            model.zero_grad()
            pred,hidden = model(x,hidden)
            loss = criterion(pred,y)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(),5)
            optimizer.step()
            a_loss += loss.detach()
            a_size += bs
#         print(e,a_loss/a_size*1e+6)
        model.eval()
        with torch.no_grad():
            val_data = load_data(val_list,batch_size)
            b_loss = 0
            b_size = 0
            for x,y in val_data:
                x,y = x.to(device),y.to(device)
                bs = x.size()[1]
                h1 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
                h2 = torch.zeros((n_layers,bs,h_size)).to("cuda:0")
                hidden = (h1,h2)
                pred,hidden = model(x,hidden)
                loss = criterion(pred,y)
                b_loss += loss.detach()
                b_size += bs
        print("epoch: {} - train_loss: {} - val_loss: {}".format(e+1,float(a_loss.item()/a_size*1e+6),b_loss.item()/b_size*1e+6))

train(modelGRU,train_list,val_list,path,seq,epoch,batch_size,criterionGRU,optimizerGRU,model_type="GRU")```

Thank you