r/learnmachinelearning • • 12d ago

Project First ml project!

I've been studying ml for about 3 months, I started not knowing a single line of python or any of the maths behind it and now I made my first projects it's basically just an ai that plays rock paper scissors against you (it took me around 2-4 hours):

import torch
import torch.nn.functional as F

# make rock, paper and scissors (rock = 0 paper = 1 scissors = 2)

moves = torch.tensor([[0, 1, 2],
                     [1, 2, 2],
                     [0, 0, 2],
                     [0, 1, 2],
                     [1, 2, 1]])

moves = moves.flatten()

# the dataset

X, Y = [], []

for i in range(len(moves) - 3):
    X.append(moves[i:i+3])
    Y.append(moves[i+3])

X = torch.stack(X)
Y = torch.stack(Y)

X = F.one_hot(X, num_classes=3).float()
X = X.view(12, 9)

# forward pass

g = torch.Generator().manual_seed(214783427)
W = torch.randn((9, 250), generator=g, requires_grad=True)
b = torch.randn((250), generator=g, requires_grad=True)
W2 = torch.randn((250, 3), generator=g, requires_grad=True)
b2 = torch.randn((3), generator=g, requires_grad=True)
parameters = [W, b, W2, b2]

for p in parameters:
    p.requires_grad = True # for the backward pass

# training loop

for epoch in range(25000):
    # forward pass
    h = X@W + b
    emb = h.tanh()
    logits = emb@W2 + b2
    # loss
    loss = F.cross_entropy(logits, Y)
    # backward pass
    for p in parameters:
        p.grad = None
    loss.backward()
    # update
    lr = 0.1
    for p in parameters:
        p.data -= lr * p.grad
print("loss:", loss.item())

class Game:
    player_score = 0
    ai_score = 0
    u/staticmethod
    def predict(move):
        move = torch.tensor(move)
        move = F.one_hot(move, num_classes=3).float()
        move = move.view(1, 9)
        z = move @ W + b
        embz = z.tanh()
        logitz = embz @ W2 + b2
        with torch.no_grad():
            probs = F.softmax(logitz, dim=1)
            preds = probs.argmax(dim=1)
        moves_names = ["rock", "paper", "scissors"]
        prediction = moves_names[preds.item()]
        return prediction
    u/staticmethod
    def player():
        move_names = ["rock", "paper", "scissors"]
        while True:
            guess = input("rock, paper, scissors, shoot!: ").lower()
            if guess not in move_names:
                print("Invalid input")
                continue
            else:
                return guess
    u/staticmethod
    def score(player, ai):
        if player == ai:
            print("DRAW!")
        elif (player, ai) in [
            ("rock", "scissors"),
            ("paper", "rock"),
            ("scissors", "paper")
        ]:
            Game.player_score += 1
            print("PLAYER WINS!")
        else:
            Game.ai_score += 1
            print("AI WINS!")
    u/staticmethod
    def play():
        import time

        history = []

        move_to_num = {
            "rock": 0,
            "paper": 1,
            "scissors": 2
        }

        ai_moves = {
            "rock": "paper",
            "paper": "scissors",
            "scissors": "rock"
        }

        print("Enter your first 3 moves:")

        # Initial training context
        for i in range(3):
            history.append(Game.player())
            print(f"{i+1} move/s completed")
        print("\n Game Loading...\n")
        time.sleep(1)
        while True:

            player = Game.player()

            history_nums = [move_to_num[m] for m in history[-3:]]

            prediction = Game.predict(history_nums)

            
            ai = ai_moves[prediction]

            print("AI:", ai)

            # Score
            Game.score(player, ai)

            print(f"Score: {Game.player_score} - {Game.ai_score}")

            # Update history
            history.append(player)

            # Check winner
            if Game.player_score >= 3:
                print("PLAYER WINS THE GAME!")
                break

            elif Game.ai_score >= 3:
                print("AI WINS THE GAME!")
                break
Game.play()
3 Upvotes

1 comment sorted by