如何让one-hot数据兼容non-one-hot?

How to make one-hot data compatible with non one-hot?

我正在制作一个机器学习模型来计算不同角色组合的游戏胜率。 我在最后一行使用损失函数时出错。我认为这是因为输入是单热向量。 模型的输出与目标数据不兼容。因为目标数据只是布尔值,输赢。请给我建议来解决这个问题。如何让one-hot输入兼容非one-hot?

'''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

首先,你的问题与One-hot编码无关,因为你的模型输出的是一个数字,而Y_data是0-1,所以它们是兼容的。你的问题是关于实例化损失。 因此,你必须实例化损失,然后传递参数:

...
model = BinaryClassifier()
optimizer = torch.optim.SGD(model.parameters(), lr=1)
loss = nn.BCELoss()

nb_epochs = 1000
for epoch in range(nb_epochs + 1):

    hypothesis = model(x_train)
    cost = loss(hypothesis, y_train)

关于你的x_data,如果你的数据是这样的:

[[0,0,1,0], [0,1,0,0], [1,0,0,0,], [0,1,0,0],...]

self.layer1 你应该用 4.

指定 in_features

如果x_data是这样的:

[ [[0,0,1,0], [0,1,0,0], [1,0,0,0,], [0,1,0,0]], [[0,0,1,0], [0,1,0,0], [1,0,0,0,], [0,1,0,0]], ...]

并且你想使用线性层,你必须展平每个样本,因为线性层接受 1-dim 输入。

例如,以上为:

[[0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0], [0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0], ...]

in_features=16.

供您参考,对于 2 维和更多维输入,您可以使用 CNN(卷积神经网络),对于序列输入,您可以使用 RNN(递归神经网络)。

希望对您有所帮助。