by Yehonathan SharvitMar 15 2019

Simple Keras Challenge

This notebook runs on a python environment that contains Keras, Tensorflow and Numpy. It describes a small Machine Learning challenge with a suggested solution to the challenge.

Your mission is to come with a better solution to the challenge and to share it with your colleagues, customers or friends either privately or publicly.

Currently, the notebook is in read-only mode. In order to change the content of a cell, all you need to do is to click on the Remix button on the right of the top bar. Once you are satisfied with your solution, share it by clicking on the Publish button that will appears once you remix the notebook.

1. Setup

There is almost nothing to setup as the "Keras Installation" environment already contains the required python packages.

The only thing we need to do is to import the required dependencies in a Python code cell:

import platform, keras, tensorflow, numpy as np
print("Python version: %s.\nNumpy version: %s.\nTensorflow version: %s.\nKeras version: %s." % 
      (platform.python_version(), tensorflow.__version__, keras.__version__, np.__version__))

2. The Challenge

The challenge is to approximate a polynomial of degree 2 with a Neural Network:

f(x)=x2+x+1f(x) = x^2 + x + 1

Let's generate x_train, an array of values between 0 and 10 and y_train, an array for the targets.:

x_train = np.arange(0, 10, .001)
x_train.shape = (len(x_train), 1)
y_train = np.array([[x[0]**2 + x[0]**2 - x[0] - 1] for x in x_train])

Now, we are going to build a Neural Network with Keras for this simple polynomial of degree 2.

3. The Model

We create our model using Keras Sequential model API: It is made of a 1 hidden layer with 20 neurons with relu activation, optimized with Adam.

from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers

model = Sequential()
model.add(Dense(20, activation='relu', input_dim=1))
model.add(Dense(1, activation='linear'))

model.compile(loss='mean_squared_error', optimizer=optimizers.Adam(lr=0.05))

Now, we train our model:

model.fit(x_train, y_train, epochs=100)

4. Model Evaluation

Let's check how our model performs:

model_evaluation = model.evaluate(x_train, y_train)
print('The model evaluation is: ' + '%.2e' % model_evaluation)

Well, a mean squared error of 0.043 is a good start.

Can you come up with a better model?

All you need to do is to click on the "Remix" button on the right of the top bar. Now the notebook becomes your own and you can author it. Once you are satisfied with your model, share it with privately with your colleagues or publicly. It is a simple as clicking on the "Publish" button.

Happy notebooking!