In [1]:
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)
In [16]:
def NLclassifier(x,y):
    
    def teste1(a,b): return a**2 * b - 3
    def teste2(a,b): return -a**2 + 6*a*b + 3*b**2 + 5
    
    out = 0
    
    if (teste1(x,y) > 0 and teste2(x,y) < 0):
        out = 1

    if(teste2(x,y)>0):
        out = 2
        
    return out
In [17]:
lower = -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])))
In [18]:
# 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[:])))
In [19]:
labels.shape
Out[19]:
(10000, 3)
In [20]:
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])
(2020, 2)
(1594, 2)
(6386, 2)
10000
In [21]:
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)
In [22]:
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']
)
In [23]:
model.fit(data_train,labels,epochs=10,batch_size=50)
Epoch 1/10
10000/10000 [==============================] - 1s 78us/step - loss: 0.3506 - acc: 0.9134
Epoch 2/10
10000/10000 [==============================] - 0s 34us/step - loss: 0.1352 - acc: 0.9631
Epoch 3/10
10000/10000 [==============================] - 0s 34us/step - loss: 0.0965 - acc: 0.9725
Epoch 4/10
10000/10000 [==============================] - 0s 33us/step - loss: 0.0800 - acc: 0.9743
Epoch 5/10
10000/10000 [==============================] - 0s 33us/step - loss: 0.0688 - acc: 0.9780
Epoch 6/10
10000/10000 [==============================] - 0s 34us/step - loss: 0.0634 - acc: 0.9775
Epoch 7/10
10000/10000 [==============================] - 0s 34us/step - loss: 0.0563 - acc: 0.9811
Epoch 8/10
10000/10000 [==============================] - 0s 32us/step - loss: 0.0509 - acc: 0.9827
Epoch 9/10
10000/10000 [==============================] - 0s 31us/step - loss: 0.0483 - acc: 0.9834
Epoch 10/10
10000/10000 [==============================] - 0s 33us/step - loss: 0.0466 - acc: 0.9825
Out[23]:
<tensorflow.python.keras.callbacks.History at 0xb3c5f3828>
In [24]:
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])
    )
)
In [25]:
model.evaluate(data_test,labels_test,batch_size=32)
5000/5000 [==============================] - 0s 43us/step
Out[25]:
[0.04267390879392624, 0.9858]
In [26]:
test_result = model.predict(data_test,batch_size = 32)
test_result.shape
Out[26]:
(5000, 3)
In [27]:
test_color = np.apply_along_axis(np.argmax,1,test_result)
In [28]:
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)
(936, 2)
(809, 2)
(3255, 2)
In [29]:
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)
In [ ]: