-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.py
72 lines (61 loc) · 2.65 KB
/
game_of_life.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
import numpy as np
import tensorflow as tf
class GameOfLife:
"""Create a network that simulates Conway's Game of Life"""
def __init__(self, show_model=False):
"""Initialize class variables"""
self.setup_model()
if show_model:
self.model.summary()
self.print_model_weights()
def setup_model(self):
"""Create the Game-of-Life model"""
const = tf.keras.initializers.constant
self.model = tf.keras.models.Sequential([
tf.keras.Input(shape=(None,None,1)),
# convolutional layer 1
tf.keras.layers.Conv2D(filters=2, kernel_size=(3,3), padding='same',
name='conv1', trainable=False, dynamic=True, activation='relu',
kernel_initializer=const([1,-1,1,-1,1,-1,1,-1,0,-1,1,-1,1,-1,1,-1,1,-1]),
bias_initializer=const([-3,3])),
# convolutional layer 2
tf.keras.layers.Conv2D(filters=1, kernel_size=(1,1), padding='same',
name='conv2', trainable=False, dynamic=True, activation='relu',
kernel_initializer=const([-1,-1]),
bias_initializer=const([1]))],
name='game_of_life')
# initialize model weights
self.predict(np.zeros((1,3,3,1)))
def predict(self, X, steps=1):
"""Compute the board state after the given number of steps"""
Y = X.copy()
for _ in range(steps):
Y = self.model(Y)
return Y.numpy()
def print_model_weights(self):
"""Display the network architecture and weights"""
layers = self.model.layers
print('\nconv1 weights:')
print(np.squeeze(layers[0].get_weights()[0]).transpose(2,0,1))
print('\nconv1 biases:')
print(' ', layers[0].get_weights()[1])
print('\nconv2 weights:')
print(np.squeeze(layers[1].get_weights()[0]))
print('\nconv2 biases:')
print(' ', layers[1].get_weights()[1])
def generate_dataset(self, num=100, steps=1, board_size=(32,32),
density=.5, random_seed=None):
"""Create a dataset consisting of multiple simulations"""
if random_seed:
np.random.seed(random_seed)
X = np.zeros(num * np.prod(board_size))
num_cells = X.size
num_alive = int(num_cells * density)
alive_cells = np.random.choice(num_cells, size=num_alive, replace=False)
X[alive_cells] = 1
X = X.reshape(num, *board_size, 1).astype(np.float32)
Y = self.predict(X, steps=steps)
return (X, Y)
if __name__ == '__main__':
life = GameOfLife(show_model=True)
x, y = life.generate_dataset(steps=2, num=1)