Pytorch error: type error

I am facing the following error :slight_smile:
TypeError: only integer tensors of a single element can be converted to an index

Traceback (most recent call last):
File “Main_mine.py”, line 377, in
best_model, [losses_train, losses_valid, losses_epochs_train, losses_epochs_valid] = Train_Model(grud, train_dataloader, valid_dataloader , lr, epochs,patience)
File “Main_mine.py”, line 206, in Train_Model
outputs = model(inputs)
File “/home/alaa/.local/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 532, in call
result = self.forward(*input, **kwargs)
File “/media/alaa/New Drive2/PHD/PHD3-Missing Values/Codes/archive(1)/run/GRU-D_my_version (copy)/LSTMD_mine.py”, line 168, in forward
, torch.squeeze(Delta[:,i:i+1,:]))
File “/media/alaa/New Drive2/PHD/PHD3-Missing Values/Codes/archive(1)/run/GRU-D_my_version (copy)/LSTMD_mine.py”, line 133, in step
h = (delta_h) * (h)

TypeError: only integer tensors of a single element can be converted to an index

here is a piece of code

def step(self, x, x_last_obsv, x_mean, h, ct, mask, delta):

    batch_size = x.shape[0]
    dim_size = x.shape[1]
    
    delta_x = torch.exp(-torch.max(self.zeros_x, self.gamma_x_l(delta)))
    delta_h = torch.exp(-torch.max(self.zeros_h, self.gamma_h_l(delta)))
    
    x = mask * x + (1 - mask) * (delta_x * x_last_obsv + (1 - delta_x) * x_mean)
    h = (delta_h) * (h)
    
    combined = torch.cat((x, h, mask), 1)
    i = F.sigmoid(self.il(combined))
    f = F.sigmoid(self.fl(combined))
    o = F.sigmoid(self.ol(combined))
    c = F.tanh(self.cl(combined))
    ct = f*ct + i*c
    h = o * (torch.tanh(ct))
    
    return h,ct

Looking at the traceback, I believe the error is associated with torch.squeeze(Delta[:,i:i+1,:])). However, you haven’t shared the code. It could also be due to your version of PyTorch. As some users have reported the same error previously. Have a look at https://github.com/chrisdxie/uois/issues/2

I would suggest you to share the piece of code associated with slicing the Delta tensor.

def forward(self, input):
batch_size = input.size(0)
type_size = input.size(1)
step_size = input.size(2)
spatial_size = input.size(3)

    Hidden_State = self.initHidden(batch_size)
    X = torch.squeeze(input[:,0,:,:])
    X_last_obsv = torch.squeeze(input[:,1,:,:])
    Mask = torch.squeeze(input[:,2,:,:])
    Delta = torch.squeeze(input[:,3,:,:])
    
    hidden = None
    for i in range(step_size):
        Hidden_State = self.step(torch.squeeze(X[:,i:i+1,:])\
                                 , torch.squeeze(X_last_obsv[:,i:i+1,:])\
                                 , torch.squeeze(self.X_mean[:,i:i+1,:])\
                                 , Hidden_State\
                                 , torch.squeeze(Mask[:,i:i+1,:])\
                                 , torch.squeeze(Delta[:,i:i+1,:]))
        
                                  
        if hidden is None:
            hidden = Hidden_State.unsqueeze(1)
        else:
            hidden = torch.cat((hidden, Hidden_State.unsqueeze(1)), 1)
       
            
    if self.output_last:
        prediction = self.output(hidden[:,-1,:])
        return prediction
    else:
        hidden = hidden.view(-1, self.hidden_size)
        prediction = self.output(hidden)
        return prediction

I have tried the solution in the link but nothing solved