torch.nn.MSELoss()
时间: 2023-09-29 08:02:43 浏览: 209
`torch.nn.MSELoss()` is a class in PyTorch that implements the mean squared error loss function. It is used for regression tasks, where the goal is to minimize the difference between the predicted and actual target values.
The mean squared error loss is calculated as the average of the squared differences between the predicted and actual target values. It is expressed as:
```
MSE = 1/n * ∑(y_pred - y_actual)^2
```
where `n` is the number of samples, `y_pred` is the predicted target value and `y_actual` is the actual target value.
The `torch.nn.MSELoss()` class takes in two arguments - `size_average` and `reduce`. `size_average` specifies whether to average the loss over the batch or not, while `reduce` specifies whether to reduce the loss over the batch or not. By default, both arguments are set to `True`.
Example usage:
```python
import torch.nn as nn
mse_loss = nn.MSELoss()
output = model(input)
loss = mse_loss(output, target)
loss.backward()
```
阅读全文