-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_connectfour.py
58 lines (45 loc) · 1.74 KB
/
model_connectfour.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
from model import *
import numpy as np
import keras.layers as Kl
import keras.models as Km
from keras import optimizers
class ConnectFourModel(Model):
def __init__(self, tag):
super().__init__(tag)
pass
def create_model(self):
print('new model')
model = Km.Sequential()
model.add(Kl.Conv2D(20, (5, 5), padding='same', input_shape=(7, 7, 1)))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(20, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(20, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(30, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(30, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(30, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Conv2D(30, (4, 4), padding='same'))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Flatten(input_shape=(7, 7, 1)))
model.add(Kl.Dense(49))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Dense(7))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Dense(7))
model.add(Kl.LeakyReLU(alpha=0.3))
model.add(Kl.Dense(1, activation='linear'))
opt = optimizers.adam()
model.compile(optimizer=opt, loss='mean_squared_error', metrics=['accuracy'])
model.summary()
return model
def state_to_tensor(self, state, move):
vec = np.zeros((1, 7))
if move != -1:
vec[0, move] = 1
tensor = np.append(vec, state, axis=0)
tensor = tensor.reshape((1, 7, 7, 1))
return tensor