<jupyter_start><jupyter_text>Notebook for testing the Keras setupThis netbook is for testing the [Keras](https://keras.io/) setup for the ML hands-on.  Below is a set of required imports.  Run the cell, and no error messages should appear.  In particular, **Keras 2 is required**. Keras also informs which backend (Theano, Tensorflow, or CNTK) it will be using. Some warnings may appear, this should be fine.<jupyter_code>%matplotlib inline

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten, MaxPooling2D
from keras.layers.convolutional import Conv2D 
from keras.layers.recurrent import SimpleRNN, LSTM, GRU 
from keras.utils import np_utils
from keras import backend as K

from distutils.version import LooseVersion as LV
from keras import __version__

from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

from keras.datasets import mnist, fashion_mnist, imdb

from sklearn.model_selection import train_test_split

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

print('Using Keras version:', __version__, 'backend:', K.backend())
assert(LV(__version__) >= LV("2.0.0"))<jupyter_output><empty_output><jupyter_text>If the backend is Tensorflow, we can display some further information:<jupyter_code>if K.backend() == "tensorflow":
    import tensorflow as tf
    device_name = tf.test.gpu_device_name()
    if device_name == '':
        device_name = "None"
    print('Using TensorFlow version:', tf.__version__, ', GPU:', device_name)<jupyter_output><empty_output><jupyter_text>Getting started: 30 seconds to Keras(This section is adapted from https://keras.io/)The core data structure of Keras is a **model**, a way to organize layers. The main type of model is the `Sequential` model, a linear stack of layers.A model is initialized by calling `Sequential()`:<jupyter_code>model = Sequential()<jupyter_output><empty_output><jupyter_text>Stacking layers is as easy as `.add()`:<jupyter_code>model.add(Dense(units=64, input_dim=100))
model.add(Activation("relu"))
model.add(Dense(units=10))
model.add(Activation("softmax"))<jupyter_output><empty_output><jupyter_text>A summary of the model:<jupyter_code>print(model.summary())<jupyter_output><empty_output><jupyter_text>Let's draw a fancier graph of our model:*Note: This does not work in Google Colaboratory.*<jupyter_code>SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))<jupyter_output><empty_output><jupyter_text>Once your model looks good, configure its learning process with `.compile()`:<jupyter_code>model.compile(loss='categorical_crossentropy', 
              optimizer='sgd', 
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>You can now begin training your model with `.fit()`.  Let's generate some random data and use it to train the model:<jupyter_code>X_train = np.random.rand(128, 100)
Y_train = np_utils.to_categorical(np.random.randint(10, size=128))

model.fit(X_train, Y_train, epochs=5, batch_size=32, verbose=2);<jupyter_output><empty_output><jupyter_text>Evaluate your performance on test data with `.evaluate():`<jupyter_code>X_test = np.random.rand(64, 100)
Y_test = np_utils.to_categorical(np.random.randint(10, size=64))

loss, acc = model.evaluate(X_test, Y_test, batch_size=32)
print()
print('loss:', loss, 'acc:', acc)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11078286558345643, 'fraction_numerical': 0.012112259970457902, 'mean_word_length': 6.491150442477876, 'pattern_counts': {'":': 1, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 2, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '579935', 'license': 'MIT', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1108, 'n_words': 307, 'path': 'notebooks/keras-test-setup.ipynb', 'repo_name': 'CSCfi/machine-learning-scripts', 'size': '3385'}