-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrian_toy.py
49 lines (39 loc) · 1.19 KB
/
trian_toy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
'''
import tensorflow as tf
from dagmm_eager import DAGMM
import numpy as np
from sklearn.datasets import make_blobs
from sklearn import preprocessing
import matplotlib as mlp
mlp.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Build toy dataset
data, _ = make_blobs(n_samples=2000, n_features=2, centers=5, random_state=123)
data = preprocessing.scale(data)
data = data.astype('float32')
data[300] = [0, 2]
data[500] = [-2, -2]
plt.scatter(data[:,0], data[:,1], marker='.')
plt.savefig('images/data.png')
plt.close()
# Build model
model = DAGMM(2)
model.fit(data, 128, 4000, 100)
pred = model.predict(data)
plt.plot(pred, 'o-')
plt.plot([300,500], [pred[300], pred[500]], 'ro-')
plt.savefig('images/pred.png')
plt.close()
# Draw the decision boundary
xx,yy = np.meshgrid(np.arange(-2,2,0.01), np.arange(-2,2,0.01))
data = np.c_[xx.ravel(), yy.ravel()]
data = data.astype('float32')
pred = model.predict(data).reshape(xx.shape)
mean = np.mean(pred)
pred[pred>mean*3] = mean*3
plt.contourf(xx, yy, pred, 10, alpha = 0.6, cmap = plt.cm.hot)
C = plt.contour(xx, yy, pred, 10, colors = 'black')
plt.clabel(C, inline = True, fontsize = 10)
plt.savefig('images/boundary.png')