Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MSELoss from Pytorch

I am trying to train a neural network using Pytorch. I would like the loss function to be the MSE. I tried to use torch.nn.MSELoss, however I get an error that I do not understand.

For example the following code gives me RuntimeError: Boolean value of Tensor with more than one value is ambiguous

import torch
import torch.nn as nn

model = torch.zeros(64)
model.requires_grad = True
target = torch.ones(64)

loss = nn.MSELoss(model, target)

Any help will be very much appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Please look in Pytorch docs: https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html

You need to create an MSELoss object before calling with the target and predictions.

loss = nn.MSELoss()
input = torch.zeros(64, requires_grad=True)
target = torch.ones(64)
output = loss(input, target)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading