Type erorr: pytorch

username: Hala_Hamdoun
cooked: <p>How can i solve this error?<br>
TypeError: only integer tensors of a single element can be converted to an index</p>
<p>Traceback (most recent call last):<br>
File “Main_mine.py”, line 377, in<br>
best_model, [losses_train, losses_valid, losses_epochs_train, losses_epochs_valid] = Train_Model(grud, train_dataloader, valid_dataloader , lr, epochs,patience)<br>
File “Main_mine.py”, line 206, in Train_Model<br>
outputs = model(inputs)<br>
File “/home/alaa/.local/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 532, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
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<br>
, torch.squeeze(Delta[:,i:i+1,:]))<br>
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<br>
h = (delta_h) * (h)</p>
<p>TypeError: only integer tensors of a single element can be converted to an index</p>
<p>I think the error is happened here</p>
<p>def step(self, x, x_last_obsv, x_mean, h, ct, mask, delta):</p>
<pre><code class="lang-auto">    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
</code></pre>

username: ptrblck
cooked: <p>I don’t see any indexing operation in the code snippet and the error message points to:</p>
<pre><code class="lang-python">torch.squeeze(Delta[:,i:i+1,:])
</code></pre>
<p>Could you print <code>i</code> before trying to index <code>Delta</code> with it and make sure it’s a scalar value?</p>

username: Hala_Hamdoun
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="87884">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Could you print <code>i</code> before trying t</p>
</blockquote>
</aside>
<p>sorry for late reply: the following contain torch.squeeze(Delta[:,i:i+1,:]))<br>
def forward(self, input):<br>
batch_size = input.size(0)<br>
type_size = input.size(1)<br>
step_size = input.size(2)<br>
spatial_size = input.size(3)</p>
<pre><code>    Hidden_State = self.initHidden(batch_size)
    C_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,:])\
                                 , C_State\
                                 , 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

def initHidden(self, batch_size):
    #use_gpu = torch.cuda.is_available()
   # if use_gpu:
       # Hidden_State = Variable(torch.zeros(batch_size, self.hidden_size).cuda())
       # return Hidden_State
    #else:
    Hidden_State = Variable(torch.zeros(batch_size, self.hidden_size))
    C_State = Variable(torch.zeros(batch_size, self.hidden_size))
    return Hidden_State, C_State
</code></pre>

username: ptrblck
cooked: <p>Thanks for the code snippet.<br>
Unfortunately, it’s not executable, so that I would ask you to check the <code>i</code> values and make sure they are scalar values.</p>

username: ptrblck
cooked: <p>Then this error shouldn’t be raised as seen in this code snippet:</p>
<pre><code class="lang-python">Delta = torch.randn(10, 10, 10)

i = 1
out = Delta[:, i:i+1, :] # works

i = torch.tensor([0, 1])
out = Delta[:, i:i+1, :] # your error
&gt; TypeError: only integer tensors of a single element can be converted to an index
</code></pre>
<p>Feel free to post an executable code snippet to reproduce this issue, so that we could have a look.</p>