Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have little experience in training neural networks. When I try to train the network function, I find the loss function is always 0. Then I make the following attempt:

>>> from torch import tensor                                                                                            >>> import torch.nn as nn                                                                                               >>> t1=tensor([[0.23],[0.33]])                                                                                          >>> t2=tensor([[1.0],[1.0]])                                                                                      >>> criterion = nn.CrossEntropyLoss()                                                                                   >>> criterion(t1,t2)                                                                                                    tensor(-0.)                                                                                                             >>> criterion(t1,t2).item()                                                                                             -0.0                       


>>> criterion = nn.CrossEntropyLoss().cuda()                                                                            >>> t1=t1.cuda()                                                                                                        >>> t2=t2.cuda()                                                                                                        >>> criterion(t1,t2)                                                                                                    tensor(-0., device='cuda:0')                                                                                            >>> criterion(t1,t2).item()                                                                                             -0.0 


Could you tell me why the output of criterion is always 0?

What I have tried:

t1 t2 is what the output of the NN and my label like.
Posted
Updated 19-Jul-23 2:39am
v2
Comments
Richard MacCutchan 19-Jul-23 10:05am    
Have you looked at the PyTorch documentation for CrossEntropyLoss?
Maciej Los 14-Nov-23 16:52pm    
Please, improve your question. Instead of two columns of code, use single column. BTW: can you provide complete code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900