from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
= make_moons() X, y
0], X[:, 1], c= y) plt.scatter(X[:,
from keras.models import Sequential
from sklearn.metrics import accuracy_score
import os
from keras.layers import Dense, Activation
from keras.utils import to_categorical
= Sequential([
model_simple 1, input_shape=(2,)),
Dense('relu'),
Activation(2),
Dense('softmax'),
Activation(
])
= Sequential([
model_complex 6, input_shape=(2,)),
Dense('relu'),
Activation(4),
Dense('relu'),
Activation(3),
Dense('relu'),
Activation(2),
Dense('softmax'),
Activation(
])
= Sequential([
model_complex_2 10, input_shape=(2,)),
Dense('relu'),
Activation(8, ),
Dense('relu'),
Activation(8),
Dense('relu'),
Activation(2),
Dense('softmax'),
Activation( ])
compile(optimizer='adam',
model_simple.='categorical_crossentropy',
loss=['accuracy'])
metrics
compile(optimizer='adam',
model_complex.='categorical_crossentropy',
loss=['accuracy'])
metricscompile(optimizer='adam',
model_complex_2.='categorical_crossentropy',
loss=['accuracy']) metrics
def make_plot(X, y, model, dataset, model_type, noise, n_iter=80,cmap='PRGn'):
=200
hif dataset=="moon":
= make_moons(noise=noise)
X, y if dataset=="iris":
= load_iris()['data'][:, :2], load_iris()['target']
X, y print(X.shape, y.shape)
= to_categorical(y)
y_binary
= np.meshgrid(np.linspace(X[:, 0].min()-0.2, X[:, 0].max()+0.2, h),
xx, yy 1].min()-0.2, X[:, 1].max()+0.2, h))
np.linspace(X[:, = np.c_[xx.ravel(), yy.ravel()]
XX
for i in range(n_iter):
=1, verbose=0)
model.fit(X, y_binary, epochs= np.argmax(model.predict(XX), axis=1).reshape(xx.shape)
Z = np.argmax(model.predict(X), axis=1)
y_hat = accuracy_score(y, y_hat)
train_accuracy = plt.contourf(xx, yy, Z, h , cmap=cmap, alpha=0.4)
contours "Iteration: "+str(i)+"\n Accuracy:"+str(train_accuracy))
plt.title(
plt.colorbar()0], X[:, 1], c= y, cmap=cmap)
plt.scatter(X[:, if not os.path.exists(f"/Users/nipun/Desktop/animation-keras/{dataset}/{model_type}/{noise}/"):
f"/Users/nipun/Desktop/animation-keras/{dataset}/{model_type}/{noise}/")
os.makedirs(f"/Users/nipun/Desktop/animation-keras/{dataset}/{model_type}/{noise}/{i:03}.png")
plt.savefig(
plt.clf()
"moon", "simple", None) make_plot(X, y, model_simple,
(100, 2) (100,)
<Figure size 432x288 with 0 Axes>
!convert -delay 20 -loop 0 /Users/nipun/Desktop/animation-keras/moon/simple/None/*.png moon-simple-none.gif
"moon", "complex", None, 500) make_plot(X, y, model_complex,
(100, 2) (100,)
<Figure size 432x288 with 0 Axes>
!convert -delay 20 -loop 0 /Users/nipun/Desktop/animation-keras/moon/complex/None/*.png moon-complex-none.gif
"moon", "complex", 0.3, 700) make_plot(X, y, model_complex_2,
(100, 2) (100,)
<Figure size 432x288 with 0 Axes>
!convert -delay 20 -loop 0 /Users/nipun/Desktop/animation-keras/moon/complex/0.3/*.png moon-complex-03.gif
= Sequential([
model_simple_2 1, input_shape=(2,)),
Dense('relu'),
Activation(3),
Dense('softmax'),
Activation(
])
compile(optimizer='adam',
model_simple_2.='categorical_crossentropy',
loss=['accuracy']) metrics
"iris", "simple", None, 500) make_plot(X, y, model_simple_2,
(150, 2) (150,)
<Figure size 432x288 with 0 Axes>
!convert -delay 20 -loop 0 /Users/nipun/Desktop/animation-keras/iris/simple/None/*.png iris-simple.gif
= Sequential([
model_complex_iris 12, input_shape=(2,)),
Dense('relu'),
Activation(6),
Dense('relu'),
Activation(4),
Dense('relu'),
Activation(3),
Dense('softmax'),
Activation(
])
compile(optimizer='adam',
model_complex_iris.='categorical_crossentropy',
loss=['accuracy']) metrics
"iris", "complex", None, 500) make_plot(X, y, model_complex_iris,
(150, 2) (150,)
<Figure size 432x288 with 0 Axes>
!convert -delay 20 -loop 0 /Users/nipun/Desktop/animation-keras/iris/complex/None/*.png iris-complex.gif