The key here is the kernel_size of (3, 3) and strides of 1. Now we can take a first crack at building a Convolutional Neural Network using Keras. X_test, y_test = generate_dataset(test_size, board_shape, probability_alive) X_val, y_val = generate_dataset(val_size, board_shape, probability_alive) X_train, y_train = generate_dataset(train_size, board_shape, probability_alive) X = generate_frames(num_frames, board_shape=board_shape, prob_alive=prob_alive) Return X.reshape(X.shape, X.shape, X.shape, 1)ĭef generate_dataset(num_frames, board_shape, prob_alive): Now that we can generate data, we’ll generate training, validation and test sets.Įach element in the y_train/ y_val/ y_test arrays will represent the next Game of Life board for each board frame in X_train/ X_val/ X_test. To the right it also renders the next state of the game board using the life_step function: The following takes the integer representation of a game board from above, and renders it as an image. Print(frames.shape) # (num_frames, board_w, board_h) Plt.imshow(frame2.flatten().reshape(board_shape), cmap='gray')īoard_size = board_shape * board_shapeįrames = generate_frames(10, board_shape=board_shape, prob_alive=probability_alive) Plt.imshow(frame1.flatten().reshape(board_shape), cmap='gray') generate_frames creates num_frames random game boards with a particular shape and a predefined probability of each cell being ‘alive’, and render_frames draws image representations of two game boards side-by-side for comparison, where living cells are white and dead cells are black:ĭef generate_frames(num_frames, board_shape=(100,100), prob_alive=0.15): With the game logic in place, the next thing we’ll want is some way to randomly generate game boards (frames) and a way to visualize them. Return (live_neighbors = 3) | (X & (live_neighbors = 2)).astype(int) Live_neighbors = sum(np.roll(np.roll(X, i, 0), j, 1) Basically, it takes a game board matrix as input where 0 represents a dead cell and 1 represents a living cell, and returns a matrix of the same size but containing the state of each cell on the next iteration of the game. Luckily there are plenty of implementations available online, such as the following from. The first thing to do is define a function that takes a game board as input and returns the next state. It’s probably overkill for Conway’s Game, but it’s still pretty interesting to see how it performs. Why do this? Mostly for fun, and to learn a little bit about convolutional neural networks. The rules continue to be applied repeatedly to create further generations. Each generation is a pure function of the preceding one. The first generation is created by applying the above rules simultaneously to every cell in the seed births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick. The initial pattern constitutes the seed of the system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |