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


(Yellow Champagne) #1

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’


(Fabian ) #2

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.


(Yellow Champagne) #3

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


(Yellow Champagne) #4

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’


(Fabian ) #5

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()


(Yellow Champagne) #7

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?


(Fabian ) #8

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.


(Yellow Champagne) #10

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


(Yellow Champagne) #11

Or use this link


(Fabian ) #12

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.


(Yellow Champagne) #13

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


(Fabian ) #14

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()


(Yellow Champagne) #16

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.


(Fabian ) #17

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


(Yellow Champagne) #19

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.


(Fabian ) #20

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