-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt_2.py
135 lines (111 loc) · 4.07 KB
/
ttt_2.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
class TicTacToeNeuralNet:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# Weights between input and hidden layers
self.w_ih = np.random.randn(self.input_size, self.hidden_size) / np.sqrt(self.input_size)
# Weights between hidden and output layers
self.w_ho = np.random.randn(self.hidden_size, self.output_size) / np.sqrt(self.hidden_size)
# Bias for hidden and output layers
self.b_h = np.zeros((1, hidden_size))
self.b_o = np.zeros((1, output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
self.h = self.sigmoid(np.dot(X, self.w_ih) + self.b_h)
self.o = self.sigmoid(np.dot(self.h, self.w_ho) + self.b_o)
return self.o
def backward(self, X, y, o):
self.o_error = y - o
self.o_delta = self.o_error * self.sigmoid_derivative(o)
self.h_error = self.o_delta.dot(self.w_ho.T)
self.h_delta = self.h_error * self.sigmoid_derivative(self.h)
self.w_ho += self.h.T.dot(self.o_delta)
self.w_ih += X.T.dot(self.h_delta)
self.b_h += np.sum(self.h_delta, axis=0, keepdims=True)
self.b_o += np.sum(self.o_delta, axis=0, keepdims=True)
def train(self, X, y, epochs=1000, learning_rate=0.1):
for _ in range(epochs):
o = self.forward(X)
self.backward(X, y, o)
def print_board(board):
for row in [board[i:i+3] for i in range(0, 9, 3)]:
print('|' + '|'.join([' X ' if x == 1 else ' O ' if x == -1 else ' ' for x in row]) + '|')
print('-' * 11)
def play_game(net, board):
print("\nBoard Before AI's Move:")
print_board(board)
# Convert board state to input for the network
X = np.array(board).reshape(1, 9)
# Get network's decision
decision = net.forward(X)
# Convert to move (0-8)
move = np.argmax(decision)
# Update board if move is valid
if board[move] == 0:
board[move] = 1 # Player's move (1 for simplicity)
print("\nBoard After AI's Move:")
print_board(board)
return board
# Example training data (as provided earlier)
X_train = np.array([
[1,-1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1],
[-1,1,-1,0,0,0,0,0,0],
[0,0,0,0,0,-1,0,1,0],
[0,0,1,0,0,0,0,0,0],
[0,0,0,0,1,-1,0,0,0],
[0,0,0,0,0,0,0,1,-1],
[-1,0,1,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0],
[-1,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,1,0,0],
[0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,-1,1,0,0],
[-1,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0],
[1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,-1,0,1],
[0,0,0,0,0,1,0,0,-1],
])
y_train = np.array([
[0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
])
# Initialize and train the neural network
net = TicTacToeNeuralNet(input_size=9, hidden_size=6, output_size=9)
net.train(X_train, y_train)
# Example usage to see the network's output for a given input
example_input = X_train[0].reshape(1, -1)
prediction = net.forward(example_input)
print("Prediction for the first example:", prediction)
# Example usage:
board = [0] * 9 # Initialize empty board
net = TicTacToeNeuralNet(input_size=9, hidden_size=6, output_size=9)
# Assuming net has been trained as per previous instructions
net.train(X_train, y_train)
board = play_game(net, board)