【发布时间】:2022-01-11 11:48:49
【问题描述】:
我正在制作一个机器学习模型来计算不同角色组合的游戏胜率。 我使用损失函数在最后一行出错。我认为这是因为输入是单热向量。 模型的输出与目标数据不兼容。因为目标数据只是布尔值,无论输赢。请给我建议以解决这个问题。如何让一键输入与非一键兼容?
'''for example, when the number of character is 4 and eahc team member is 2.
x_data is [ [[0,0,1,0], [0,1,0,0], [1,0,0,0,],[0,1,0,0]], [game2]...]
team A1, temaA2, temaB1 teamB2
'''
y_data = [[0], [0], [0], [1], [1], [1]] # team blue win: 1, lose : 0
x_train = torch.FloatTensor(x_data)
y_train = torch.FloatTensor(y_data)
class BinaryClassifier(nn.Module):
def __init__(self):
super(BinaryClassifier, self).__init__()
self.layer1 = nn.Sequential(
nn.Linear(in_features=num_characters, out_features=10, bias=True),
nn.ReLU(),
)
self.layer2 = nn.Sequential(
nn.Linear(in_features=10, out_features=1, bias=True),
nn.Sigmoid(),
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
return torch.sigmoid(x)
model = BinaryClassifier()
optimizer = optim.SGD(model.parameters(), lr=1)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
hypothesis = model(x_train)
cost = nn.BCELoss(hypothesis, y_train)
# RuntimeError: bool value of Tensor with more than one value is ambiguous
【问题讨论】:
标签: python machine-learning neural-network pytorch one-hot-encoding