Skip to content

Instantly share code, notes, and snippets.

@moratorium08
Created July 4, 2017 14:06
Show Gist options
  • Select an option

  • Save moratorium08/f08ef6befaba69605f57bb95401ae91a to your computer and use it in GitHub Desktop.

Select an option

Save moratorium08/f08ef6befaba69605f57bb95401ae91a to your computer and use it in GitHub Desktop.
# coding:utf-8
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[3.989, 3.432],
[6.517, 5.741],
[5.629, 3.828],
[3.267, 5.239],
[4.021, 4.520],
[4.063, 3.424],
[4.408, 5.199],
[3.369, 4.476],
[5.608, 5.525],
[4.860, 5.262],
[3.295, 8.655],
[0.871, 8.646],
[0.513, 1.231],
[-0.147, 4.628],
[10.120, 6.647],
[7.619, 8.668],
[7.261, 9.535],
[2.376, 9.043],
[9.482, 8.523],
[9.198, 9.767]
])
target = np.array([0 for i in range(10)] + [1 for i in range(10)])
model = Sequential()
model.add(Dense(80, input_shape=(2,), activation='relu'))
model.add(Dense(80, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy'])
model.fit(data, target, batch_size=1, nb_epoch = 120, verbose=1)
gridx = np.linspace(-2,12,100)
gridy = np.linspace(-2,12,100)
X, Y = np.meshgrid(gridx, gridy)
result = []
for i in range(X[0].size):
tmp = []
for j in range(X[i].size):
x = X[i][j]
y = Y[i][j]
tmp.append(model.predict(np.array([[x, y]]))[0, 0])
result.append(tmp)
Z = np.array(result)
cont = plt.contour(X, Y, Z, levels=[0.5])
cont.clabel(fmt='%1.1f', fontsize=14)
plt.hot()
plt.plot(data[:10,0], data[:10,1], "bo", label="1")
plt.plot(data[10:,0], data[10:,1], "ro", label="2")
plt.xlabel("x")
plt.ylabel("y")
plt.axis('equal')
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment