Error while training with datasets using neural network

29 Views Asked by At

I'm facing a problem while making a project. I have to train my program with a dataset file in order to predict after. It is concerning chess game. This is

RES: 1/2-1/2
CHECKMATE: False
FEN: 8/8/8/8/3K2kP/r5N1/1R6/8 b - - 0 62
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . K . . k P
r . . . . . N .
. R . . . . . .
. . . . . . . .

a content exemple. RES and checkmate are the expected and the board the input.

I'm making an algo based on neural network training. This is the high parts of my code.

def network_creator(sizeof_input, sizeof_output, sizeof_hidden):
    weight_one = [[random.uniform(0, 1) for i in range(sizeof_hidden)] for j in range(sizeof_input)]
    weight_two = [[random.uniform(0, 1) for i in range(sizeof_output)] for j in range(sizeof_hidden)]
    return weight_one, weight_two
def file_parser(file):
    chess_states = []
    with open(file, 'r') as file:
        lines = file.readlines()
        i = 0
        while i < len(lines):
            line = lines[i]
            if line.startswith("RES:"):
                res = line.strip().split(' ')[1]
                checkmate = lines[i + 1].strip().split(' ')[1]
                fen = lines[i + 2].strip().split(' ')[1]
                chess = []
                i += 3
                while i < len(lines) and not lines[i].startswith("\n"):
                    chess.append(lines[i].strip())
                    i += 1
                chess_states.append({
                    "RES": res,
                    "Checkmate": checkmate,
                    "FEN": fen,
                    "CHESS_Board": chess
                })
            else:
                i += 1
    return chess_states

def board_parser(chess_ine):
    my_dict = {'.': 0, 'k': 1, 'r': 2, 'p': 3, 'K': 4, 'b': 5, 'B': 10, 'R': 6, 'Q': 7, 'q': 8, 'P': 9, 'N': 11, 'n': 12}

    new_value = [my_dict[c] for c in chess_ine.strip() if c in my_dict]
    return new_value

def res_parser(res):
    if res == "1-0":
        return 1.0
    elif res == "0-1":
        return 0.1
    elif res == "1/2-1/2":
        return 0.5

def extract_data(chess):
    X_train = []
    Y_train = []
    for one in chess:
        # print(one["CHESS_Board"])
        inputs = board_parser("".join(one["CHESS_Board"]))
        # print(inputs)
        expected = (res_parser(one["RES"]), one["Checkmate"])
        # print(expected)
        X_train.append(inputs)
        Y_train.append(expected)

    return X_train, Y_train

def forward(X, weight_one, weight_two):

    layer_hidden_input = [sum(x * w for x, w in zip(X, weight_one[i])) for i in range(len(weight_one))]
    layer_hidden_output = [activation_function(x) for x in layer_hidden_input]

    layer_output_input = [sum(h * w for h, w in zip(layer_hidden_output, weight_two[i])) for i in range(len(weight_two))]
    layer_output_output = [activation_function(x) for x in layer_output_input]

    return layer_hidden_output, layer_output_output

def backward(X, y, layer_hidden_output, layer_output_output, weight_two):
    error_out = [t - p for t, p in zip(y, layer_output_output)]
    alpha_calcul = [e * activation_function_der(p) for e, p in zip(error_out, layer_output_output)]

    error_hidden = [sum(alpha_calcul[j] * weight_two[i][j] for j in range(len(alpha_calcul))) for i in range(len(layer_hidden_output))]
    alpha_calcul_hidden = [e * activation_function_der(h) for e, h in zip(error_hidden, layer_hidden_output)]

    return alpha_calcul_hidden, alpha_calcul

def update(X, alpha_calcul_hidden, alpha_calcul, layer_hidden_output, weight_one, weight_two, lr):
    for i in range(len(weight_two)):
        for j in range(len(weight_two[i])):
            weight_two[i][j] += layer_hidden_output[i] * alpha_calcul[j] *lr

    for i in range(len(weight_one)):
        for j in range(len(weight_one[i])):
            weight_one[i][j] += X[i] * alpha_calcul_hidden * lr

def weighted_sum(expected, mine):
    res = sum((t - p) ** 2 for t, p in zip(expected, mine)) / len(expected)
    return res

def forward(X, weight_one, weight_two):

    layer_hidden_input = [sum(x * w for x, w in zip(X, weight_one[i])) for i in range(len(weight_one))]
    layer_hidden_output = [activation_function(x) for x in layer_hidden_input]

    layer_output_input = [sum(h * w for h, w in zip(layer_hidden_output, weight_two[i])) for i in range(len(weight_two))]
    layer_output_output = [activation_function(x) for x in layer_output_input]

    return layer_hidden_output, layer_output_output

def backward(X, y, layer_hidden_output, layer_output_output, weight_two):
    error_out = [t - p for t, p in zip(y, layer_output_output)]
    alpha_calcul = [e * activation_function_der(p) for e, p in zip(error_out, layer_output_output)]

    error_hidden = [sum(alpha_calcul[j] * weight_two[i][j] for j in range(len(alpha_calcul))) for i in range(len(layer_hidden_output))]
    alpha_calcul_hidden = [e * activation_function_der(h) for e, h in zip(error_hidden, layer_hidden_output)]

    return alpha_calcul_hidden, alpha_calcul

def update(X, alpha_calcul_hidden, alpha_calcul, layer_hidden_output, weight_one, weight_two, lr):
    for i in range(len(weight_two)):
        for j in range(len(weight_two[i])):
            weight_two[i][j] += layer_hidden_output[i] * alpha_calcul[j] *lr

    for i in range(len(weight_one)):
        for j in range(len(weight_one[i])):
            weight_one[i][j] += X[i] * alpha_calcul_hidden * lr

def trainer(X, y, epochs=1000, lr=0.1):
    sizeof_input = len(X[0])
    sizeof_hidden = len(X[0])
    sizeof_output = len(y[0])

    weight_one, weight_two = network_creator(sizeof_input, sizeof_output, sizeof_hidden)
    
    for epoch in range(epochs):
        layer_hidden_output, layer_output_output = forward(X, weight_one, weight_two)
        loss = weighted_sum(y, layer_output_output)
        alpha_calcul_hidden, alpha_calcul = backward(X, y, layer_hidden_output, layer_output_output)
        update(X[0], alpha_calcul_hidden, alpha_calcul, layer_hidden_output, weight_one, weight_two, lr)
        if epoch % 100 == 0:
            print(f"Epoch {epoch}, Loss: {loss}")
    return weight_one, weight_two

def main(argv):
    ac = len(argv)
    for i in range(0, ac):
        if argv[i] == "--new":
            if int(argv[i + 3]):
                sizeof_input = int(argv[i + 1])
                sizeof_hidden = int(argv[i + 2])
                sizeof_output = int(argv[i + 3])
                created_network, e = network_creator(sizeof_input, sizeof_output, sizeof_hidden)
            else:
                sizeof_input = int(argv[i + 1])
                sizeof_output = int(argv[i + 2])
                sizeof_hidden = int(math.sqrt(sizeof_input * sizeof_output))
                created_network, e = network_creator(sizeof_input, sizeof_output, sizeof_hidden)
            print(created_network)
            print(e)
        if argv[i] == "--save":
            save_file(argv[i + 1], created_network)
        if argv[i] == "--load":
            loaded_file = load_file(argv[i + 1])
            print(loaded_file)
        if argv[i] == "--train":
            fiile = file_parser(argv[i + 1])
            X_train, Y_train = extract_data(fiile)
            trained_one, trained_two = trainer(X_train, Y_train)

if __name__ == "__main__":
    exit(main(sys.argv))

This is the error i'm encountering:

layer_hidden_input = [sum(x * w for x, w in zip(X[i], weight_one[i])) for i in range(len(weight_one))]
IndexError: list index out of range
0

There are 0 best solutions below