<jupyter_start><jupyter_text>A Simple Catcher CNN DemoWe first need to import the entire X library by adding the super folder and then importing the right keras libraries<jupyter_code>import os, sys
sys.path.append(os.path.join('..'))
import keras.backend as K
K.set_image_dim_ordering('th') # needs to be set since it defaults to tensorflow now
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Flatten
from keras.optimizers import SGD

from x.environment import Catcher
from x.models import KerasModel
from x.memory import ExperienceReplay
from x.agent import DiscreteAgent<jupyter_output>Using TensorFlow backend.<jupyter_text>Setup GameHere we setup the game and training settings<jupyter_code>num_actions = 3
nb_filters, nb_rows, nb_cols = 32, 3, 3
grid_x, grid_y = 11, 11
epoch = 100
batch = 50
memory_len = 500
gamma = 0.9
epsilon = 0.1<jupyter_output><empty_output><jupyter_text>Catcher ModelHere is the model we train for catching<jupyter_code># keras model
keras_model = Sequential()
keras_model.add(Convolution2D(nb_filters, nb_rows, nb_cols, input_shape=(1, grid_x, grid_y), activation='relu', subsample=(2, 2)))
keras_model.add(Convolution2D(nb_filters, nb_rows, nb_cols, activation='relu'))
keras_model.add(Convolution2D(num_actions, nb_rows, nb_cols))
keras_model.add(MaxPooling2D(keras_model.output_shape[-2:]))
keras_model.add(Flatten())

# X wrapper for Keras
model = KerasModel(keras_model)

# Memory
M = ExperienceReplay(memory_length=memory_len)

# Agent
A = DiscreteAgent(model, M)
# SGD optimizer + MSE cost + MAX policy = Q-learning as we know it
A.compile(optimizer=SGD(lr=0.2), loss="mse", policy_rule="max")

# To run an experiment, the Agent needs an Enviroment to iteract with
catcher = Catcher(grid_size=grid_x, output_shape=(1, grid_x, grid_y))
A.learn(catcher, epoch=epoch, batch_size=batch)<jupyter_output>/Users/mader/anaconda/lib/python3.5/site-packages/keras/engine/topology.py:368: UserWarning: The `regularizers` property of layers/models is deprecated. Regularization losses are now managed via the `losses` layer/model property.
  warnings.warn('The `regularizers` property of '<jupyter_text>Test the agent Here we follow the learned policy<jupyter_code>out_dir = 'rl_dir'
if not os.path.exists(out_dir): os.mkdir(out_dir)
A.play(catcher, epoch=100, visualize={'filepath': os.path.join(out_dir, 'demo.gif'), 'n_frames': 270, 'gray': True})<jupyter_output>Free play started!
100/100 [==============================] - 1s - loss: 0.0000e+00 - rewards: -0.2200     
Making gif!
See your gif at rl_dir/demo.gif<jupyter_text>Show PlayingHere we show the playing in the notebook using HTML rendering of the animated GIF<jupyter_code>from IPython.display import HTML
import base64
with open(os.path.join(out_dir, 'demo.gif'), 'rb') as in_file:
    data_str = base64.b64encode(in_file.read()).decode("ascii").replace("\n", "")
data_uri = "data:image/png;base64,{0}".format(data_str)
HTML("""<img src="{0}" width = "256px" height = "256px" />""".format(data_uri))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12359188928226585, 'fraction_numerical': 0.025748310267138717, 'mean_word_length': 6.636363636363637, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '59182', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1077, 'n_words': 281, 'path': 'examples/catcher_cnn.ipynb', 'repo_name': 'EderSantana/X', 'size': '3107'}