# Package imports
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model
import matplotlib
import theano
from keras.utils.np_utils import to_categorical
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
import pydot
# old pydot will not work with python3, must use one
# that works with python3 such as pydot2 or pydot
from keras.models import Sequential, Graph
# Display plots inline and change default figure size
# Generate a dataset and plot it
np.random.seed(0)
X, y = sklearn.datasets.make_moons(200, noise=0.20)
plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)
from keras.regularizers import l2
model = Sequential()
model.add(Dense(3, input_dim=2, init='uniform'))
model.add(Activation('tanh'))
model.add(Dense(2, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adagrad')
# model.compile(loss='mean_squared_error', optimizer='sgd')
history = model.fit(X, to_categorical(y), nb_epoch=5000)
def plot_decision_boundary(pred_func):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
print(Z.shape, xx.shape)
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
plot_decision_boundary(model.predict_classes)
plt.show()