Sorry to post here, I’m new to this and tried searching all day and can’t quite figure out what to do.
playgame() is a 600-line function that takes a bunch of arguments (valued between 0-1) as “moves” and outputs a score. I have excluded it here, but can pastebin it or something if needed.
class TwoLayerNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(438,100)
        self.fc2 = nn.Linear(100,438)
    def forward(self, x):
        x = F.sigmoid(self.fc1(x))
        x = F.sigmoid(self.fc2(x))
        return x

m = TwoLayerNet()
loss_fn = nn.MSELoss()
optimizer = optim.Adam(m.parameters(), lr=0.01)

training_epochs = 25
fitnessgoal = torch.tensor(1200, dtype=torch.float)
blanktensor = torch.zeros([1, 438])


def myloss(output, fitnessgoal):
    orders = output.tolist()[0]
    fitness = torch.tensor(playgame(*orders), dtype=torch.float)
    loss = loss_fn(fitness, fitnessgoal)  
    loss.requires_grad = True
    loss.retain_grad()
    return loss

for i in range(training_epochs):

    # forward pass
    fpass = m(blanktensor)

    # compute and print loss
    loss = myloss(fpass, fitnessgoal)
    loss.requires_grad = True
    print(i,fitness)
    print(loss.grad)

    # reset gradients
    optimizer.zero_grad()

    # backwards pass
    loss.backward()

    # step the optimizer - update the weights
    optimizer.step()

Doing it this way results in the loss remaining exactly the same each epoch. It seems that loss.backward() and optimizer.step() aren’t actually doing anything. loss.grad results in None, which seems to confirm this.
As far as I can gather this is because it can’t back propagate through the playgame() function. I imagine I’m probably severing things when I convert the output from a tensor to a regular list. But playgame() can’t use a tensor as argument, and even if it could, would that even work as a loss function?
I’m trying to train the network to refine a 438 length list of floats (between 0 and 1) that result in the highest possible score when passed through playgame(). Is it possible to do stuff like this with Pytorch?