<jupyter_start><jupyter_text>> Installing required packages...<jupyter_code>!pip install tensorflow
!pip install tensorflowjs
!pip install numpy
!pip install matplotlib
!pip install rarfile
!pip install sklearn<jupyter_output>Requirement already satisfied: tensorflow in /usr/local/lib/python3.6/dist-packages (2.4.0)
Requirement already satisfied: protobuf>=3.9.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.12.4)
Requirement already satisfied: tensorboard~=2.4 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (2.4.0)
Requirement already satisfied: keras-preprocessing~=1.1.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.2)
Requirement already satisfied: h5py~=2.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (2.10.0)
Requirement already satisfied: numpy~=1.19.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.19.5)
Requirement already satisfied: absl-py~=0.10 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.10.0)
Requirement already satisfied: google-pasta~=0.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: termcolor~=1.1.0 in /usr/local/lib/python3.6/dist-packages (f[...]<jupyter_text><jupyter_code>from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
import random
import json
import os
import tensorflow as tf

tf.compat.v1.enable_eager_execution()<jupyter_output><empty_output><jupyter_text>> Checking if GPU device is linked.<jupyter_code>device_name = tf.test.gpu_device_name()
if device_name != "/device:GPU:0":
    raise SystemError("GPU device not found")
    
print("Found GPU at: {}".format(device_name))<jupyter_output>Found GPU at: /device:GPU:0<jupyter_text>> Connecting with google drive to import training Data<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import rarfile

with rarfile.RarFile("/content/drive/MyDrive/Datasets/gol_data.rar", "r") as zip_ref:
  zip_ref.extractall("gol_data")
data = []

with open("gol_data/gol_data_1.json", "r") as f:
    data.extend(json.load(f))
start_all, stop_all = [], []

for item in data:
  start_all.append(item["start_gen"])
  stop_all.append(item["stop_gen"])
print(len(start_all), len(stop_all))
print(len(start_all[1]), len(stop_all[0]))<jupyter_output>100000 100000
625 625<jupyter_text>> Splitting data to 66% training data and 33% testing data<jupyter_code>X_train, X_test, Y_train, Y_test = train_test_split(
    np.array(start_all, dtype="float32").reshape((-1, 25, 25, 1)), 
    np.array(stop_all, dtype="float32").reshape((-1, 25, 25, 1)), 
    test_size=0.33,
    random_state=42,
    shuffle=True)<jupyter_output><empty_output><jupyter_text>> Creating the model with one hidden layer which is a convolutional layer of 4 filters.<jupyter_code>def CreateModel():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv2D(filters=4, kernel_size=3, activation="relu", padding="same", input_shape=(25, 25, 1)))
    model.add(tf.keras.layers.Conv2D(filters=1, kernel_size=3, activation="relu", padding="same"))
    return model<jupyter_output><empty_output><jupyter_text>> Compiling the model and starting the training<jupyter_code>model_checkpoint = tf.keras.callbacks.ModelCheckpoint(
    filepath="/content/drive/My Drive/model_checkpoint_gol",
    save_weights_only=False,
    monitor='val_accuracy',
    mode='max',
    save_best_only=True)


with tf.device("/gpu:0"):
  Model = CreateModel()

  Model.compile(
      loss="bce",
      optimizer=tf.keras.optimizers.Adam(),
      metrics=["accuracy"])
  
  Model.summary()
  
  history = Model.fit(x=X_train,
                      y=Y_train,
                      epochs=200,
                      batch_size=512,
                      validation_data=(X_test, Y_test),
                      shuffle=True,
                      callbacks=[model_checkpoint])
Model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 25, 25, 4)         40        
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 25, 25, 1)         37        
=================================================================
Total params: 77
Trainable params: 77
Non-trainable params: 0
_________________________________________________________________<jupyter_text>> Loading the model.<jupyter_code>Model = tf.keras.models.load_model("/content/drive/My Drive/model_checkpoint_gol")<jupyter_output><empty_output><jupyter_text>> Model performs very well with 100% acurracy on both testing and training data.> The model learned successfully to replicate conway's game of life rules using only one hidden layer with 3x3 kernel of 4 filters which makes a total of 77 parameters to tune.<jupyter_code>Model.evaluate(X_test, Y_test)<jupyter_output>1032/1032 [==============================] - 2s 2ms/step - loss: 1.4999e-10 - accuracy: 1.0000<jupyter_text>The model stopped training after 25 epochs and reached 100 % accuracy. The remaining training epochs lowered the Loss which was not useful for this Model.<jupyter_code>loss = history.history['loss']
val_loss = history.history['val_loss']
accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
epochs = range(len(loss))

fig = plt.figure(figsize=(12,6))
gs = fig.add_gridspec(1, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])

ax1.plot(epochs, loss, 'r', label='Training')
ax1.plot(epochs, val_loss, 'b', label='Validation')
ax1.set_xlabel('Epochs', size=16)
ax1.set_ylabel('Loss', size=16)
ax1.legend()

ax2.plot(epochs, accuracy, 'r', label='Training')
ax2.plot(epochs, val_accuracy, 'b', label='Validation')
ax2.set_xlabel('Epochs', size=16)
ax2.set_ylabel('Accuracy', size=16)
ax2.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>> Printing some samples from the data to visualize model predictions<jupyter_code>for example in range(50, 55):

    img_sample = X_test[example]
    out_sample = Y_test[example]

    Model.evaluate(img_sample.reshape((-1, 25, 25, 1)), out_sample.reshape((-1, 25, 25, 1)))
    predicted = Model.predict(X_test[example].reshape((-1, 25, 25, 1)))

    fig, (ax1, ax2, ax3) = plt.subplots(1, 3)

    ax1.imshow((img_sample.reshape(25, 25)), cmap="gray")
    ax1.set_title("Start Setting")

    ax2.imshow((out_sample.reshape(25, 25)), cmap="gray")
    ax2.set_title("Stop Setting")

    ax3.imshow((predicted.reshape(25, 25) >= 0.5), cmap="gray")
    ax3.set_title("Predicted Setting")
    plt.show()<jupyter_output>1/1 [==============================] - 0s 21ms/step - loss: 0.0000e+00 - accuracy: 1.0000
{'fraction_non_alphanumeric': 0.157151179726187, 'fraction_numerical': 0.04238275560734052, 'mean_word_length': 4.935177182368194, 'pattern_counts': {'":': 1, '<': 39, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '612782', 'license': 'MIT', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2287, 'n_words': 491, 'path': 'Notebooks/GoL_Forward.ipynb', 'repo_name': 'ezzakri-anas/GOL-forward-and-reverse-models', 'size': '6866'}