Issue
The pytorch program needs to return the result of the "Rock, Paper, Scissors" game. Input is given as one-hot tensors: [ [1,0,0], [0,1,0] ] ([1,0,0]-rock, [0,1,0] - scissors) output must be: [1, 0] (first player win, second player lose). What's wrong with this code?
import torch
from torch import nn
import torch.utils.data as data
torch.manual_seed(42)
input = [[1, 0, 0], [0, 1, 0]], [[1, 0, 0], [0, 0, 1]], [[0, 1, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 0]]
input = torch.tensor(input, dtype=torch.float32)
result = [[1, 0], [0, 1], [1, 1], [1, 0]]
result = torch.tensor(result, dtype=torch.float32)
class LinearRegression(nn.Module):
def __init__(self, num_inputs, num_outputs):
super().__init__()
self.linear = nn.Linear(num_inputs, num_outputs)
self.act_fn = nn.Sigmoid()
def forward(self, x):
x = self.linear(x)
x = self.act_fn(x)
return x
model = LinearRegression(num_inputs=3, num_outputs=2)
print(model)
# Training loop
for name, param in model.named_parameters():
print(f"Parameter {name}, shape {param.shape}")
for x in input:
print(model(x))
print(model(input))
print('####################')
model.train()
optimizer = torch.optim.SGD(model.parameters(), lr=0.3)
lossfunc = nn.MSELoss()
# Training loop
for _ in range(1000):
res = model(input)
res = res.squeeze(dim=1)
loss = lossfunc(res, result)
# print(loss)
## Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(model(input))
Solution
you have the following problem: You want your model to get as input the rock/paper/scissors of player1 and the rock/paper/scissors of player2. Since you do one-hot encoding you want to input 6 values into your neural network. 3 for player1 and 3 for player2. But you are trying to
Hey, a dense/feed-forward neural networl can only take ONE vector as input. But you are trying to pass two vectors of size three [[1, 0, 0], [0, 1, 0]]
. What you can/should do instead is to concat both vectors. That means the first 3 values is the one-hot encoded result of player1 and the last 3 values are the one-hot encoded result of player2. Like this: [1, 0, 0, 0, 1, 0]
.
Therefore simply change the input data to this:
input = [[1, 0, 0, 0, 1, 0]], [[1, 0, 0, 0, 0, 1]], [[0, 1, 0, 0, 1, 0]], [[0, 0, 1, 1, 0, 0]]
and the model to this:
model = LinearRegression(num_inputs=6, num_outputs=2)
And it works!
Answered By - Theodor Peifer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.