How to fix following error 'torch.FloatTensor' object has no attribute 'to'

username: KanZa
cooked: <p>My code is:</p>
<p>for i, (data, label) in data_gen:<br>
data = data.to(device)<br>
label = label.to(device)<br>
outputs = model_ft(data)<br>
_, preds = torch.max(outputs, 1)<br>
for t, p in zip(label.view(-1), preds.view(-1)):<br>
print(t, p)<br>
confusion_matrix[t.long(), p.long()] += 1</p>
<p><strong>Error message is</strong><br>
‘torch.FloatTensor’ object has no attribute ‘to’</p>

username: Nikronic
cooked: <p>Which line does give you this error?</p>
<p>As I understand it should be in</p>
<pre><code class="lang-python">data = data.to(device)
label = label.to(device)
</code></pre>
<p>And this error means <code>data</code> and <code>label</code> already are tensors in GPU and you cloning them again into GPU using <code>.to()</code> method.</p>

username: KanZa
cooked: <p>I trying to make confusion matrix for this code and gave me this error. Shall I remove data.to(device) ?</p>
<p>video_pred = [np.argmax(x[0]) for x in output]</p>
<pre><code>video_labels = [x[1] for x in output]

print('Accuracy {:.02f}% ({})'.format(
    float(np.sum(np.array(video_pred) == np.array(video_labels))) / len(video_pred) * 100.0,
    len(video_pred)))       

#Accuracy per Class
for i in range(num_class):

    indicies_correct = np.where(np.array(video_pred) == np.array(video_labels))
    
    class_total = np.sum(np.array(video_labels) == i)

    class_correct = np.sum(np.array(video_pred)[indicies_correct ] == i)
    
    print('Class {}: Accuracy {:.02f}%'.format(i, class_correct / class_total * 100))

    
confusion_matrix = torch.zeros(num_class, num_class)

data_gen = enumerate(data_loader)
#with torch.no_grad():

for i, (data, label) in data_gen:
    data = data
    label = label
    outputs = model_ft(data)
    _, preds = torch.max(outputs, 1)
    for t, p in zip(label.view(-1), preds.view(-1)):
        print(t, p)
        confusion_matrix[t.long(), p.long()] += 1

print(confusion_matrix)

print(confusion_matrix.diag()/confusion_matrix.sum(1))</code></pre>

username: Nikronic
cooked: <p>It would be great to test your code without using <code>to</code> method.<br>
I still do not know which line causes this problem. Actually it should not bother you to convert your tensors to GPU because you are getting your items from <code>Dataset</code> class and they are by default on CPU. Or you have some codes somewhere which overrides these rules.</p>

username: alex.veuthey
cooked: <p><a class="mention" href="/u/nikronic">@Nikronic</a> I don’t think the tensor in question is already on GPU, since it is of type <code>torch.FloatTensor</code> and not <code>torch.cuda.FloatTensor</code>.</p>
<p>It would be helpful to print the full stacktrace, so we can at least know what line throws the error!</p>

username: Nikronic
cooked: <p>Yes, you right! I just forgot about <code>cuda</code>.</p>

username: ptrblck
cooked: <p><code>.to()</code> was introduces in PyTorch <code>0.4.0</code> if I’m not mistaken.<br>
If you’re using an older version, you might want to update to the latest release.<br>
You can find the instructions <a href="https://pytorch.org/get-started/locally/" rel="nofollow noopener">here</a>.</p>

username: KanZa
cooked: <p>Yes you are right I am using pytorch 0.3.0<br>
If I update my pytorch, my current project may stop working or may create new errors. Is this so?</p>

username: alex.veuthey
cooked: <p>Yes, many things changed with 0.4.0 and 1.0.0 as well. I would create a new virtualenv, make a copy of the code (or commit everything before making changes), and just deal with the errors as they come when running your code…</p>