In [ ]:
%matplotlib inline
In [ ]:
from ailib.ml.datasets.regression_toy_problems import gen_1d_linear_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_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");
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)
In [ ]:
from keras.utils import plot_model
plot_model(model, show_shapes=True, to_file="model.png")