【问题标题】:Best way to create a tic-tac-toe model with pytorch使用 pytorch 创建井字游戏模型的最佳方法
【发布时间】:2019-11-13 01:07:27
【问题描述】:

我想创建一个模型,可以根据玩家的动作预测 9x9 井字游戏获胜者。 这是我的数据集中的一个示例:

..................................x.............................................. 14 L
..............o...................x.................x............................ 67 L
..............o...............x...x.................x..............o............. 2 L

有 81 个字段可以是 Xo。左边的数字是对手的下一步(对手总是0)。而字母代表游戏的结果。我决定用0 替换所有".",用1 替换x,用2 替换oLW 用一种热编码。我用未来的步骤压缩我的位置并将其输入到模型中。这就是我遇到麻烦的地方。我的 train_x 维度是 (249561, 80, 1)。我的样本火车是

tensor([0, 0, 0, 0, 1, 2, 1, 2, 0, 0, 2, 0, 0, 0, 0, 2, 1, 0, 0, 0, 1, 2, 2, 2,
        0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 2, 0, 1, 0, 0, 0, 2, 0, 1, 0, 1, 1,
        1, 0, 2, 0, 0, 1, 2, 1, 0, 0, 1, 2, 2, 1, 1, 0, 0, 0, 0, 0, 2, 1, 0, 2,
        0, 0, 1, 0, 0, 0, 2, 0], [67])

我试过了

self.fc = nn.Sequential(
        nn.Linear(80, 4096),
        nn.ReLU(),
        nn.Dropout(p=0.5),
        nn.Linear(4096, 2048),
        nn.ReLU(),
        nn.Dropout(p=0.5),
        nn.Linear(2048, 1),
    ) 

def forward(self, x):
    logit = self.fc(x)
    return logit

当我进行训练循环时,我收到一个错误RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm 我有两个问题。我的数据处理是否正确?我应该使用什么模型?

【问题讨论】:

    标签: python machine-learning neural-network pytorch tic-tac-toe


    【解决方案1】:

    要解决非常具有描述性的 RuntimeError,您必须简单地将张量从 Long 转换为 Float:

    input_sample = input_sample.float()
    

    或者,在构建示例时,将替换从 [0, 1, 2] (Long) 更改为 [0., 1., 2.] (Float)。

    【讨论】:

    • 谢谢!另外我如何输入 (249561, 80, 1) 数组而不是 (249561, 80)?
    • @SomeoneSomething 您可以使用 .unsqueeze(...).view(...) 来解决这个问题...如果此答案有帮助,请考虑标记为答案或点赞
    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多