Onil90
Onil90

Reputation: 181

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!

Upvotes: 1

Views: 13756

Answers (1)

Ophir Yaniv
Ophir Yaniv

Reputation: 366

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)

Upvotes: 4

Related Questions