File size: 2,495 Bytes
9d524af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch
import pandas as pd
import torch.nn as nn

class CSVToTensor:
    def __init__(self, file_path):
        self.data_frame = pd.read_csv(file_path)
        len_dataset = len(self.data_frame)
        self.game_tensor = torch.zeros((len_dataset, 9), dtype=torch.float32)
        self.prediction_tensor = torch.zeros((len_dataset, 9), dtype=torch.float32)
    
    def csv_to_tensor(self, pos):
        if pos >= len(self.data_frame):
            raise ValueError("Position is greater than the number of data in the dataset")
        
        data_pos = self.data_frame.iloc[pos]
        game_stat = data_pos.values[0:9]
        prediction_stat = data_pos.values[9:18]

        self.game_tensor[pos] = torch.tensor(game_stat, dtype=torch.float32)
        self.prediction_tensor[pos] = torch.tensor(prediction_stat, dtype=torch.float32)

    def tensor_to_view(self, pos):
        if pos >= len(self.data_frame):
            raise ValueError("Position is greater than the number of data in the dataset")

        if torch.equal(self.game_tensor[pos], torch.zeros(9)) or torch.equal(self.prediction_tensor[pos], torch.zeros(9)):
            raise ValueError("No tensor data found at this position")
        
        symbols = {0: ' ', 1: 'x', 2: 'o', 3: 'O'}
        board = []
        for i in range(9):
            if self.game_tensor[pos][i] == 1:
                board.append(1)
            elif self.game_tensor[pos][i] == 2:
                board.append(2)
            elif self.prediction_tensor[pos][i] == 2:
                board.append(3)
            else:
                board.append(0)
        
        print("\nCurrent Game State:")
        for i in range(0, 9, 3):
            print(f"{symbols[board[i]]} | {symbols[board[i+1]]} | {symbols[board[i+2]]}")
            if i < 6:
                print("---------")

    def print_data(self):
        print(self.data_frame)

    def create_all_tensor(self):
        for i in range(len(self.data_frame)):
            self.csv_to_tensor(i)
        return self.game_tensor, self.prediction_tensor
    
    def create_a_dataset(self):
        return torch.utils.data.TensorDataset(self.game_tensor, self.prediction_tensor)

    
if __name__ == '__main__':
    position = 0
    tensor = CSVToTensor('./Datasets/example.csv')
    tensor.print_data()
    tensor.csv_to_tensor(position)
    print(f"Input : {tensor.game_tensor[position]}")
    print(f"Output : {tensor.prediction_tensor[position]}")
    tensor.tensor_to_view(position)