Attribut Error: 'torch.dtype' object has no attribute 'type'

Traceback (most recent call last):

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 204, in <module>

main()

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 72, in main

i_tb = train(train_loader, net, optimizer, epoch, i_tb)

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 105, in train

writer.add_scalar(‘train_loss’, loss.data[0], i_tb)

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

AttributeError: ‘torch.dtype’ object has no attribute ‘type’

It’s pretty hard to help you here since I can’t reproduce your error. I would recommend you to use a debugger and look at your loss variable and figure out how to access the loss from what is likely some sort of tensor. The error message indicates that you might want to use a .item() somewhere.

1 Like

Thank you for your prompt reply, I will try your method.

Sorry, I sent the error message wrong. The correct information is like this.

Traceback (most recent call last):

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 204, in <module>

main()

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 81, in main

validate(val_loader, net, epoch, restore_transform)

File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 163, in validate

loss = np.mean(np.array(val_loss))

File "D:\Program Files\Anaconda3\envs\lei\lib\site-packages\numpy\core\fromnumeric.py", line 2957, in mean

out=out, **kwargs)

File "D:\Program Files\Anaconda3\envs\lei\lib\site-packages\numpy\core_methods.py", line 80, in _mean

ret = ret.dtype.type(ret / rcount)

AttributeError: ‘torch.dtype’ object has no attribute ‘type’

looks like you didn’t cast your val_loss properly. I assume val_loss is a torch tensor - you can try loss = torch.mean(val_loss).numpy() if you get errors related to using a gpu try loss = torch.mean(val_loss).detach().cpu().numpy()

1 Like

Traceback (most recent call last):

File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 204, in <module>

main()

File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 81, in main

validate(val_loader, net, epoch, restore_transform)

File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 163, in validate

loss = torch.mean(val_loss).numpy()

TypeError: mean(): argument ‘input’ (position 1) must be Tensor, not list

I have tried your method, but the above error.
And in my program “val_loss = []”.How can I modify?

Ok so val_loss is a list so it is not the loss you got directly from PyTorch since it would be a tensor then.

It is impossible to give you a good answer without you sharing minimal code necessary to reproduce this error. If you don’t do that we’re stuck with guessing and that’s quite frankly not satisfying for either of us.

1 Like

https://pan.baidu.com/s/1W6HwRpecqyJXGUY7NUlJCQ
Thank you, my program is in this link.

Or use this link

Thank you!

As I see it your code should work if you replace val_loss.append(net.loss.data) with val_loss.append(net.loss.data.numpy()) that way you should get a list of floats instead of a list of tensors which seems to be your issue.

1 Like

First of all, thank you, but still gave an error.

Alrighty - so you still need to get the tensor back from your GPU - Try net.loss.data.cpu().numpy() and if that doesn’t work net.loss.data.detach().cpu().numpy()

1 Like

Still reported the error in the same place.When I try net.loss.data.cpu().numpy() ,it reports AttributeError:‘builtin_function_or_method’ object has no attribute ‘numpy’ .When I try net.loss.data.detach().cpu().numpy() ,it reports AttributeError:‘builtin_function_or_method’ object has no attribute ‘cpu’ .

Thank you.

I’m only on my laptop right now so I can’t reproduce your error. What is the type of net.loss.data.cpu()?

1 Like

Thank you, I finally solved this problem. My method is loss = np.mean(val_loss) .Now I will share this joy with you, thank you again for your constant help.

1 Like

I’m glad you figured it out. Sorry that I wasn’t that helpful after all.

Hello, I also encountered the same problem as you, but I used the way that loss = np.mean(val_loss),What went wrong

What type does val_loss have?
If it’s a tensor, I would recommend to use torch.mean or if you need to use numpy, use np.mean(val_loss.numpy()).

thx very much! I used val_loss.item() to solve this problem

ptrblck via PyTorch Forums noreply@discuss.pytorch.org 于2020年3月15日周日 下午6:09写道:

Even this didn’t work