【问题标题】:How to make one-hot data compatible with non one-hot?如何让一热数据兼容非一热数据?
【发布时间】: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


    【解决方案1】:

    首先,您的问题与 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

    供您参考,您可以将 CNN(卷积神经网络)用于 2 维及以上的输入,而对于系列输入,您可以使用 RNN(循环神经网络)。

    希望对您有所帮助。

    【讨论】:

    • 感谢您的建议。我错过了。但是我又遇到了另一个错误。 ValueError:目标和输入必须具有相同数量的元素。 target nelement (6) != input nelement (60)。 我认为 6 是 y_data 的数量,这是游戏的结果。而 60 是 x_data 的数量。 * 10(一场游戏中的游戏角色数(在这种情况下,蓝色:5,红色:5)。我应该将 y_data 拟合到结果数据吗?例如 [1,1,1,1,1,0,0,0,0 ,0,],表示蓝队获胜。
    • 能否请你把x_data的确切结构。
    • x_data = [ [[0,0,1,0], [0,1,0,0], [1,0,0,0,],[0,1,0,0]] ] 是游戏的准确数据。一场比赛由 4 名玩家组成(红色:2,蓝色 2:),[1,0,0,0,] 是角色的 one-hot 向量。就我而言,字符更多。
    • 我已经编辑了答案。
    猜你喜欢
    • 2016-08-03
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2016-12-26
    • 2017-02-26
    • 2021-01-16
    • 2017-05-02
    相关资源
    最近更新 更多