Search code examples
pythonmachine-learningneural-networkpytorchtic-tac-toe

Best way to create a tic-tac-toe model with pytorch


I want to create a model that can predict a 9x9 tic-tac-toe winner based on player moves. Here is an example from my dataset:

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

There are 81 fields that can be X or o. Number on the left is the next step of the opponent(opponent is always 0). And letter represents the outcome of the game. I decided to replace all "." with 0, x with 1 and o with 2. L or W with one hot encoding. I compress my positions with future step and feed it into the model. That's where i have trouble. My train_x dimension is (249561, 80, 1). My sample train is

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])

I tried this

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

When i am going through training loop I get an error RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm I have two questions. Am i doing the data processing right and what model should I use?


Solution

  • To solve you RuntimeError, which is very descriptive, you must simply cast your tensor from Long to Float:

    input_sample = input_sample.float()
    

    Or, when building the samples, change the replacement from [0, 1, 2] (Long) to [0., 1., 2.] (Float).