PyTorch ValueError:目标和输入必须具有相同数量的元素 [英] PyTorch ValueError: Target and input must have the same number of elements

查看:1833
本文介绍了PyTorch ValueError:目标和输入必须具有相同数量的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PyTorch的新手,但是我试图了解计算损失函数时torch.nn.BCELoss()中目标和输入大小的作用.

import torch
import torch.nn as nn
from torch.autograd import Variable

time_steps = 15
batch_size = 3
embeddings_size = 100
num_classes = 2

model = nn.LSTM(embeddings_size, num_classes)
input_seq = Variable(torch.randn(time_steps, batch_size, embeddings_size))
lstm_out, _ = model(input_seq)
last_out = lstm_out[-1]
print(last_out)

loss = nn.BCELoss()
target = Variable(torch.LongTensor(batch_size).random_(0, num_classes))
print(target)
err = loss(last_out.long(), target)
err.backward()

我收到以下错误:

Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 767
"Please ensure they have the same size.".format(target.size(), input.size()))

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 770, in binary_cross_entropy
"!= input nelement ({})".format(target.nelement(), input.nelement()))
ValueError: Target and input must have the same number of elements. target nelement (3) != input nelement (6)

此错误肯定是由于last_out(大小为3x2)和目标(大小为3)的大小不同造成的.所以我的问题是如何将last_out转换为目标(大小为3,仅包含0和1s)以计算损失函数?

解决方案

nn.BCELoss()的想法是实现以下公式:

ot都是任意(但相同!)大小的张量,而i只是索引两个张量的每个元素以计算上述总和.

通常,在分类设置中使用nn.BCELoss():oi将是尺寸为N x D的矩阵. N将是您的数据集或小批量中观察值的数量.如果仅尝试对单个属性进行分类,则D将为1;如果尝试对多个属性进行分类,则D将大于1.目标矩阵t将仅保存0和1,因为对于每个属性,只有两个类(这是二进制交叉熵损失中的二进制文件的来源). o将保留您将每个观测值的每个属性分配给类别1的概率.

现在在上面的设置中,尚不清楚您正在考虑多少个类以及为您提供多少个属性.如果target的形状仅建议一个属性,则应仅输出一个数量,即model处于1类的概率.如果有两个属性,则您的targets不完整!如果有多个类,则应使用torch.nn.CrossEntropyLoss而不是torch.nn.BCELoss().

顺便说一句,出于数值稳定性的考虑,通常希望在某些输出上使用torch.nn.BCEWithLogitsLoss代替nn.Sigmoid(),然后使用torch.nn.BCEWithLogitsLoss.

I am kinda new to PyTorch, but I am trying to understand how the sizes of target and input work in torch.nn.BCELoss() when computing loss function.

import torch
import torch.nn as nn
from torch.autograd import Variable

time_steps = 15
batch_size = 3
embeddings_size = 100
num_classes = 2

model = nn.LSTM(embeddings_size, num_classes)
input_seq = Variable(torch.randn(time_steps, batch_size, embeddings_size))
lstm_out, _ = model(input_seq)
last_out = lstm_out[-1]
print(last_out)

loss = nn.BCELoss()
target = Variable(torch.LongTensor(batch_size).random_(0, num_classes))
print(target)
err = loss(last_out.long(), target)
err.backward()

I received the following error:

Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 767
"Please ensure they have the same size.".format(target.size(), input.size()))

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/nn/functional.py", line 770, in binary_cross_entropy
"!= input nelement ({})".format(target.nelement(), input.nelement()))
ValueError: Target and input must have the same number of elements. target nelement (3) != input nelement (6)

This error definitely comes from the different sizes of last_out (size 3x2) and target (size 3). So my question is how can I convert last_out into something like target (of size 3 and containing just 0s and 1s) to compute loss function?

解决方案

The idea of nn.BCELoss() is to implement the following formula:

Both o and tare tensors of arbitrary (but same!) size and i simply indexes each element of the two tensor to compute the sum above.

Typically, nn.BCELoss() is used in a classification setting: o and i will be matrices of dimensions N x D. N will be the number of observations in your dataset or minibatch. D will be 1 if you are only trying to classify a single property, and larger than 1 if you are trying to classify multiple properties. t, the target matrix, will only hold 0 and 1, as for every property, there are only two classes (that's where the binary in binary cross entropy loss comes from). o will hold the probability with which you assign every property of every observation to class 1.

Now in your setting above, it is not clear how many classes you are considering and how many properties there are for you. If there is only one property as suggested by the shape of your target, you should only output a single quantity, namely the probability of being in class 1 from your model. If there are two properties, your targets are incomplete! If there are multiple classes, you should work with torch.nn.CrossEntropyLossinstead of torch.nn.BCELoss().

As an aside, often it is desirable for the sake of numerical stability to use torch.nn.BCEWithLogitsLossinstead of torch.nn.BCELoss() following nn.Sigmoid() on some outputs.

这篇关于PyTorch ValueError:目标和输入必须具有相同数量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆