{:check ["true"]}

Index

2 Keras Binary Classifier

Binary Classification With Keras

In this notebook, we will demonstrate the most economical way to build a Keras model to perform binary classification.

In [1]:
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as pl

Let's first load the data.

In [2]:
data = np.loadtxt('./linear_classifier_data.csv',
                 delimiter=',',
                 skiprows=1)
x_data = data[:, [0,1]]
y_data = data[:, -1].astype(int)

Building Keras Model

Instead of having a separate activation, we can compactly specify the activation function as part of the dense layer.

In [3]:
model = Sequential([
    Input(shape=(2,)),
    Dense(1, activation='sigmoid'),
])

Now, we can compile the model with the appropriate loss function and optimizer.

Note, we can use string shortnames instead of actual loss function objects.

In [4]:
model.compile(
    loss = 'binary_crossentropy',
    optimizer = SGD(learning_rate=1e-3),
    metrics = ['acc'],
)

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 3         
=================================================================
Total params: 3
Trainable params: 3
Non-trainable params: 0
_________________________________________________________________

Training

In [34]:
model.fit(x_data, y_data, epochs=10)
Epoch 1/10
63/63 [==============================] - 0s 7ms/step - loss: 0.1000 - acc: 0.9860
Epoch 2/10
63/63 [==============================] - 0s 6ms/step - loss: 0.0985 - acc: 0.9860
Epoch 3/10
63/63 [==============================] - 0s 7ms/step - loss: 0.0970 - acc: 0.9860
Epoch 4/10
63/63 [==============================] - 1s 8ms/step - loss: 0.0956 - acc: 0.9865
Epoch 5/10
63/63 [==============================] - 1s 10ms/step - loss: 0.0942 - acc: 0.9865: 0s - loss: 0.0991 - acc
Epoch 6/10
63/63 [==============================] - 1s 8ms/step - loss: 0.0929 - acc: 0.9870
Epoch 7/10
63/63 [==============================] - 0s 6ms/step - loss: 0.0916 - acc: 0.9875
Epoch 8/10
63/63 [==============================] - 0s 7ms/step - loss: 0.0903 - acc: 0.9875
Epoch 9/10
63/63 [==============================] - 0s 6ms/step - loss: 0.0891 - acc: 0.9890
Epoch 10/10
63/63 [==============================] - 0s 4ms/step - loss: 0.0879 - acc: 0.9900
Out[34]:
<tensorflow.python.keras.callbacks.History at 0x7f93d43022e0>

Evaluation

We can plot the classification separation boundary.

In [35]:
xs = np.linspace(-10, 10, 100)
ys = np.linspace(-10, 10, 100)
xx, yy = np.meshgrid(xs, ys)
input = np.concatenate([
    xx.reshape((100, 100, 1)),
    yy.reshape((100, 100, 1)),
], axis=-1).reshape(-1, 2)
z = model.predict(input).reshape(100, 100)
In [36]:
pl.contourf(xx, yy, z);
pl.scatter(x_data[y_data == 0, 0], x_data[y_data==0, 1], color='red')
pl.scatter(x_data[y_data == 1, 0], x_data[y_data==1, 1], color='blue')
Out[36]:
<matplotlib.collections.PathCollection at 0x7f93d41ac8b0>
In [ ]: