import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
def NLclassifier(x,y):
out = 0
if ((x**2 + 2*y**2 > 1) and (x**2 + 2*y**2 < 3)):
out = 1
if (x**2 + 2*y**2 >= 3):
out = 2
return out
lower = -2.5
upper = -lower
train_npts = 10000
data_train = np.random.uniform(lower , upper , 2 * train_npts).reshape(train_npts , 2)
outcolor = np.array(
list(
map(lambda x , y : NLclassifier(x,y) , data_train[:,0], data_train[:,1])))
# we create a dictionary to transform the classifier into a binary vector to apply softmax
classtovector = {0.0:np.array([1.0,0,0]), 1.0 : np.array([0,1.0,0]) , 2.0 :np.array([0,0,1.0])}
# our vectors are always column matrices
labels = np.array(list(map(lambda x : classtovector[x] , outcolor[:])))
labels.shape
class0 = data_train[outcolor[:]==0,:]
print(class0.shape)
class1 = data_train[outcolor[:]==1,:]
print(class1.shape)
class2 = data_train[outcolor[:]==2,:]
print(class2.shape)
print(class0.shape[0] + class1.shape[0] + class2.shape[0])
x0 = class0[:,0]
y0 = class0[:,1]
trace0 = go.Scatter(
x = x0,
y = y0,
name = 'Class 0',
mode = 'markers',
marker = dict(
size = 5,
color = 'rgba(203, 119, 111, .8)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
x1 = class1[:,0]
y1 = class1[:,1]
trace1 = go.Scatter(
x = x1,
y = y1,
name = 'Class 1',
mode = 'markers',
marker = dict(
size = 5,
color = 'rgba(209, 207 , 250, .8)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
x2 = class2[:,0]
y2 = class2[:,1]
trace2 = go.Scatter(
x = x2,
y = y2,
name = 'Class 2',
mode = 'markers',
marker = dict(
size = 5,
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
plotdata_train = [trace0,trace1,trace2]
plotly.offline.iplot(plotdata_train)
model = tf.keras.Sequential()
model.add(layers.Dense(36,activation = 'relu',kernel_regularizer = tf.keras.regularizers.l2(0.00001)))
model.add(layers.Dense(36,activation = 'relu',kernel_regularizer = tf.keras.regularizers.l2(0.00001)))
model.add(layers.Dense(3,activation = 'softmax'))
model.compile(
optimizer = tf.train.AdamOptimizer(0.001),
loss = 'categorical_crossentropy',
metrics=['accuracy']
)
model.fit(data_train,labels,epochs=10,batch_size=50)
test_npts = 5000
data_test = np.random.uniform(lower,upper,2 * test_npts).reshape(test_npts,2)
labels_test = np.array(
list(map(lambda x,y : classtovector[NLclassifier(x,y)] , data_test[:,0] , data_test[:,1])
)
)
model.evaluate(data_test,labels_test,batch_size=32)
test_result = model.predict(data_test,batch_size = 32)
test_result.shape
test_color = np.apply_along_axis(np.argmax,1,test_result)
test0 = data_test[test_color[:]==0,:]
print(test0.shape)
test1 = data_test[test_color[:]==1,:]
print(test1.shape)
test2 = data_test[test_color[:]==2,:]
print(test2.shape)
xx0 = test0[:,0]
yy0 = test0[:,1]
ttrace0 = go.Scatter(
x = xx0,
y = yy0,
name = 'Class 0',
mode = 'markers',
marker = dict(
size = 5,
color = 'rgba(203, 119, 111, .8)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
xx1 = test1[:,0]
yy1 = test1[:,1]
ttrace1 = go.Scatter(
x = xx1,
y = yy1,
name = 'Class 1',
mode = 'markers',
marker = dict(
size = 5,
color = 'rgba(209, 207 , 250, .8)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
xx2 = test2[:,0]
yy2 = test2[:,1]
ttrace2 = go.Scatter(
x = xx2,
y = yy2,
name = 'Class 2',
mode = 'markers',
marker = dict(
size = 5,
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
plotdata_test = [ttrace0,ttrace1,ttrace2]
plotly.offline.iplot(plotdata_test)