In [ ]:
%matplotlib inline
In [ ]:
from ailib.ml.datasets.regression_toy_problems import gen_1d_polynomial_samples
https://keras.io/#installation
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
pip install keras
In [ ]:
import tensorflow as tf
tf.__version__
In [ ]:
import keras
keras.__version__
In [ ]:
import h5py
h5py.__version__
In [ ]:
import pydot
pydot.__version__
In [ ]:
df_train = gen_1d_polynomial_samples(n_samples=100, noise_std=0.05)
x_train = df_train.x.values
y_train = df_train.y.values
plt.plot(x_train, y_train, ".k");
In [ ]:
df_test = gen_1d_polynomial_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");
In [ ]:
model = keras.models.Sequential()
#model.add(keras.layers.Dense(units=1000, activation='relu', input_dim=1))
#model.add(keras.layers.Dense(units=1))
#model.add(keras.layers.Dense(units=1000, activation='relu'))
#model.add(keras.layers.Dense(units=1))
model.add(keras.layers.Dense(units=5, activation='relu', input_dim=1))
model.add(keras.layers.Dense(units=1))
model.add(keras.layers.Dense(units=5, activation='relu'))
model.add(keras.layers.Dense(units=1))
model.add(keras.layers.Dense(units=5, activation='relu'))
model.add(keras.layers.Dense(units=1))
model.compile(loss='mse',
optimizer='adam')
model.summary()
In [ ]:
hist = model.fit(x_train, y_train, batch_size=100, epochs=3000, verbose=None)
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");