【发布时间】:2021-09-29 09:46:37
【问题描述】:
我看过一些解释。
但我明白我认为出了什么问题,但我的错误发生在不知不觉中。例如,发生错误的 sn-p 是 outputs = model(**inputs) 行。
# INPUTS
# Pulling out the inputs in the form of dictionary
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2],
}
# OUTPUTS
# '**' Unpacking the dictionary stright into the input
outputs = model(**inputs)
loss = outputs[0]
loss_train_total += loss.item()
loss.backward() # backpropagation
我得到的错误:
File "c:\Users\#####\BERT.py", line 193, in <module>
outputs = model(**inputs)
ValueError: Target size (torch.Size([32])) must be the same as input size (torch.Size([32, 3]))
现在我知道 32 是批量大小,但不确定这个目标在哪里。我知道这是标签,但我该如何解决它以使其运行?
谢谢。
编辑
我尝试了一种像这样对标签进行热编码的方法:
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(train_y.reshape(-1, 1))
train_y = enc.transform(train_y.reshape(-1, 1)).toarray()
enc.fit(test_y.reshape(-1, 1))
test_y = enc.transform(test_y.reshape(-1, 1)).toarray()
输出
[[1. 0. 0.]
[0. 0. 1.]
[0. 0. 1.]
...
[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]]
但我仍然收到相同的错误消息
【问题讨论】:
标签: python machine-learning neural-network bert-language-model