Multiclass Segregator Neural Network

Working of Multiclass Segregator Neural Network is shown in this blog.
ML
Coding
Author

Kishan Ved

Published

August 9, 2023

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

Generating data

from sklearn.datasets import make_blobs
centers = [[-5, 2], [-2, -2], [1, 2], [5, -2]]
X,y= make_blobs(n_samples=2000, centers=centers, cluster_std=1.0,random_state=30)

from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X,y)

Creating the neural network

model = Sequential([
    Dense(units=25,activation='relu'),
    Dense(units=15,activation='relu'),
    Dense(units=4,activation='linear')
])
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              optimizer=tf.optimizers.Adam(0.001),)
model.fit(Xtrain,ytrain,epochs=10)
Epoch 1/10
47/47 [==============================] - 1s 2ms/step - loss: 0.9830
Epoch 2/10
47/47 [==============================] - 0s 2ms/step - loss: 0.6116
Epoch 3/10
47/47 [==============================] - 0s 2ms/step - loss: 0.3040
Epoch 4/10
47/47 [==============================] - 0s 3ms/step - loss: 0.1548
Epoch 5/10
47/47 [==============================] - 0s 4ms/step - loss: 0.0988
Epoch 6/10
47/47 [==============================] - 0s 4ms/step - loss: 0.0727
Epoch 7/10
47/47 [==============================] - 0s 4ms/step - loss: 0.0612
Epoch 8/10
47/47 [==============================] - 0s 4ms/step - loss: 0.0549
Epoch 9/10
47/47 [==============================] - 0s 4ms/step - loss: 0.0507
Epoch 10/10
47/47 [==============================] - 0s 3ms/step - loss: 0.0474
<keras.callbacks.History at 0x7da779c31a50>

Predictions of our neural network

z_pred = model.predict(Xtest)
16/16 [==============================] - 0s 1ms/step
z_pred
array([[-3.993621  , -3.7787347 , -0.25490975, -0.07915557],
       [ 3.1840923 , -1.6633781 , -3.830906  , -7.8026066 ],
       [-2.4112265 , -6.316679  ,  4.8291907 , -4.6041875 ],
       ...,
       [-4.908304  ,  2.9423723 , -3.8089552 , -3.944756  ],
       [-3.3867204 , -5.7413936 ,  3.1279325 , -2.993482  ],
       [ 3.0492527 , -2.0576117 , -3.2582407 , -6.9038754 ]],
      dtype=float32)

Array of arrays containing probability of category = index i

prob_pred = tf.nn.softmax(z_pred).numpy()
prob_pred
array([[1.05925733e-02, 1.31318374e-02, 4.45351750e-01, 5.30923843e-01],
       [9.91312504e-01, 7.78002478e-03, 8.90503230e-04, 1.67782946e-05],
       [7.16431008e-04, 1.44230735e-05, 9.99189198e-01, 7.99435875e-05],
       ...,
       [3.88486253e-04, 9.97427046e-01, 1.16631761e-03, 1.01821462e-03],
       [1.47593522e-03, 1.40102551e-04, 9.96196985e-01, 2.18699919e-03],
       [9.92137134e-01, 6.00742921e-03, 1.80826557e-03, 4.72044776e-05]],
      dtype=float32)

Predicting the category

We dont need to use probabilities here, we can simply take the index of the maximum z value in the z_pred array as the values of z_pred and prob_pred increase or decrease together.

import numpy as np
pred = np.argmax(z_pred,axis=1) # Max of z_pred is the category
pred
array([3, 0, 2, 0, 0, 3, 2, 3, 1, 3, 1, 2, 1, 2, 3, 2, 3, 2, 3, 3, 1, 0,
       1, 0, 1, 3, 2, 3, 2, 1, 0, 1, 0, 2, 2, 2, 0, 2, 2, 1, 3, 1, 0, 1,
       0, 1, 2, 3, 3, 0, 0, 1, 2, 3, 2, 2, 3, 0, 0, 3, 1, 1, 1, 3, 3, 2,
       0, 3, 3, 1, 0, 1, 3, 2, 1, 3, 1, 2, 2, 2, 0, 3, 1, 1, 0, 0, 2, 2,
       2, 2, 3, 0, 0, 1, 2, 0, 0, 2, 1, 3, 2, 3, 1, 2, 2, 1, 2, 1, 0, 0,
       1, 3, 1, 0, 2, 3, 0, 1, 2, 1, 1, 0, 0, 0, 1, 0, 1, 3, 1, 2, 3, 2,
       2, 0, 3, 0, 1, 3, 2, 0, 0, 0, 2, 3, 2, 1, 2, 2, 1, 2, 3, 2, 3, 0,
       1, 0, 1, 2, 2, 0, 3, 1, 3, 2, 3, 1, 1, 0, 2, 3, 3, 0, 2, 0, 0, 1,
       3, 2, 0, 1, 2, 0, 0, 1, 2, 0, 0, 2, 0, 1, 3, 0, 2, 1, 1, 3, 0, 2,
       3, 1, 0, 2, 0, 0, 2, 3, 3, 1, 3, 0, 0, 1, 3, 3, 3, 2, 1, 0, 2, 0,
       0, 2, 0, 1, 1, 2, 2, 1, 1, 3, 0, 2, 3, 1, 0, 0, 2, 2, 2, 1, 2, 0,
       0, 1, 3, 3, 2, 1, 1, 3, 1, 0, 3, 0, 3, 1, 0, 3, 3, 1, 3, 0, 3, 2,
       3, 3, 0, 2, 1, 3, 3, 0, 1, 3, 0, 3, 0, 3, 3, 2, 2, 3, 3, 1, 2, 0,
       1, 1, 1, 3, 2, 1, 1, 2, 1, 3, 2, 1, 1, 1, 2, 0, 1, 2, 1, 1, 1, 3,
       1, 0, 3, 2, 2, 1, 1, 0, 0, 2, 3, 0, 2, 1, 0, 3, 3, 1, 1, 3, 2, 3,
       2, 1, 1, 1, 0, 1, 1, 3, 1, 2, 1, 3, 0, 3, 3, 2, 2, 0, 3, 2, 3, 3,
       1, 0, 2, 1, 0, 2, 0, 0, 2, 1, 0, 1, 3, 3, 1, 1, 0, 2, 0, 0, 0, 0,
       1, 2, 0, 2, 1, 2, 1, 1, 0, 1, 0, 3, 3, 2, 2, 2, 0, 1, 3, 0, 2, 2,
       2, 1, 0, 3, 1, 0, 3, 0, 2, 3, 1, 2, 1, 0, 1, 0, 1, 0, 3, 0, 2, 1,
       1, 1, 0, 1, 1, 0, 3, 1, 0, 2, 0, 0, 0, 0, 3, 1, 2, 2, 2, 1, 0, 3,
       3, 1, 2, 3, 0, 0, 3, 3, 1, 1, 1, 3, 3, 3, 2, 2, 3, 2, 0, 2, 3, 1,
       3, 3, 2, 2, 0, 2, 2, 1, 2, 1, 1, 1, 2, 0, 1, 2, 0, 3, 0, 3, 2, 0,
       1, 0, 2, 1, 1, 2, 1, 3, 0, 0, 2, 0, 0, 1, 2, 0])

Evaluating our model’s accuracy

from sklearn.metrics import accuracy_score
accuracy_score(pred,ytest) * 100
98.8