In [ ]:
%matplotlib inline

In [ ]:
from ailib.ml.datasets.regression_toy_problems import gen_1d_linear_samples

Basic 1D linear regression with Keras

Install Keras

https://keras.io/#installation

Install dependencies

Install TensorFlow backend: https://www.tensorflow.org/install/

pip install tensorflow

Insall h5py (required if you plan on saving Keras models to disk): http://docs.h5py.org/en/latest/build.html#wheels

pip install h5py

Install pydot (used by visualization utilities to plot model graphs): https://github.com/pydot/pydot#installation

pip install pydot

Install Keras

pip install keras

Import packages and check versions


In [ ]:
import tensorflow as tf
tf.__version__

In [ ]:
import keras
keras.__version__

In [ ]:
import h5py
h5py.__version__

In [ ]:
import pydot
pydot.__version__

Make the dataset


In [ ]:
df_train = gen_1d_linear_samples(n_samples=100, noise_std=1.0)

x_train = df_train.x.values
y_train = df_train.y.values

plt.plot(x_train, y_train, ".k");

In [ ]:
df_test = gen_1d_linear_samples(n_samples=100, noise_std=None)

x_test = df_test.x.values
y_test = df_test.y.values

plt.plot(x_test, y_test, ".k");

Make the regressor


In [ ]:
model = keras.models.Sequential()

model.add(keras.layers.Dense(units=1, activation='linear', input_dim=1))

model.compile(loss='mse',
              optimizer='sgd')

model.summary()

In [ ]:
hist = model.fit(x_train, y_train, epochs=200, verbose=False)

In [ ]:
plt.plot(hist.history['loss']);

In [ ]:
model.evaluate(x_test, y_test)

In [ ]:
y_predicted = model.predict(x_test)

In [ ]:
plt.plot(x_test, y_test, ".r")
plt.plot(x_test, y_predicted, ".k");

In [ ]:
weights_list = model.get_weights()
print(weights_list)

Bonnus: plot the regressor


In [ ]:
from keras.utils import plot_model

plot_model(model, show_shapes=True, to_file="model.png")