I resume the problem written on stackexchange
I am new in Pytorch and I’m trying to find a solution to the following problem via Pytorch: I’m playing the game Rock paper scissors
We are two players
J1 Rock - J2 Rock : 0$
J1 Rock - J2 Paper : -1$
J1 Rock - J2 Scissors : 2$
J1 Paper - J2 Scissors : -1$
And the opposite. In brief, if I win with Rock, I win 2$ instead of 1$
I am trying to make play 2 machines in this game to find out what will be the optimal distribution of choices between Rock/Paper/Scissors. Here is my code in Pytorch
import torch
import torch.nn as nn
import torch.optim as optim
class Modele(nn.Module):
def __init__(self):
super(Modele, self).__init__()
self.fc1 = nn.Linear(1, 5)
self.fc2 = nn.Linear(5, 3)
def forward(self,x):
x = torch.sigmoid(self.fc1(x))
x = self.fc2(x)
x = nn.Softmax(dim=1)(x)
return x
model1 = Modele()
model2 = Modele()
def discrete_loss(x,y):
M = torch.tensor([
[0,-1,2],
[1,0,-1],
[-2,1,0]
],dtype=torch.float32,requires_grad=False)
loss =torch.einsum('ix,iy,xy->i',x,y,M)
return loss.mean().float()
optimizer1 = optim.SGD(model1.parameters(), lr=0.001)
optimizer2 = optim.SGD(model2.parameters(), lr=0.001)
n = 5
for i in range(10) :
optimizer1.zero_grad()
optimizer2.zero_grad()
x = torch.rand(n,1,requires_grad=True)
y = torch.rand(n,1, requires_grad=True)
x = model1(x)
y = model2(y)
loss1 = -discrete_loss(x,y)
loss1.retain_grad()
loss1.backward()
print(loss1.grad)
optimizer1.step()
optimizer1.zero_grad()
optimizer2.zero_grad()
loss2 = discrete_loss(x,y)
loss2.retain_grad()
loss2.backward()
print(loss2.grad)
optimizer2.step()
Compared to the stackexchange link, I changed the loss to make it a little smoother. Nevertheless my code bug completely, I have the impression of not knowing the good practices in a basic pytorch model. If you could test the code and tell me my errors