Multilayer binary classifier#

Deeper binary classification for or comparison with Binary classification using Keras (shallow).

1raise SystemExit("Stop right there!");
An exception has occurred, use %tb to see the full traceback.

SystemExit: Stop right there!

Importing libraries and packages#

 1# System
 2import os
 3
 4# Mathematical operations and data manipulation
 5import pandas as pd
 6from sklearn.preprocessing import LabelEncoder
 7
 8# Modelling
 9from tensorflow.keras.models import Sequential
10from tensorflow.keras.layers import Dense
1os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

Set paths#

1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"

Loading dataset#

1dataset = pd.read_csv(f"{data_path}/sonar.csv")
1dataset.shape
(208, 61)
1dataset.head()
attribute_1 attribute_2 attribute_3 attribute_4 attribute_5 attribute_6 attribute_7 attribute_8 attribute_9 attribute_10 ... attribute_52 attribute_53 attribute_54 attribute_55 attribute_56 attribute_57 attribute_58 attribute_59 attribute_60 Class
0 0.0200 0.0371 0.0428 0.0207 0.0954 0.0986 0.1539 0.1601 0.3109 0.2111 ... 0.0027 0.0065 0.0159 0.0072 0.0167 0.0180 0.0084 0.0090 0.0032 Rock
1 0.0453 0.0523 0.0843 0.0689 0.1183 0.2583 0.2156 0.3481 0.3337 0.2872 ... 0.0084 0.0089 0.0048 0.0094 0.0191 0.0140 0.0049 0.0052 0.0044 Rock
2 0.0262 0.0582 0.1099 0.1083 0.0974 0.2280 0.2431 0.3771 0.5598 0.6194 ... 0.0232 0.0166 0.0095 0.0180 0.0244 0.0316 0.0164 0.0095 0.0078 Rock
3 0.0100 0.0171 0.0623 0.0205 0.0205 0.0368 0.1098 0.1276 0.0598 0.1264 ... 0.0121 0.0036 0.0150 0.0085 0.0073 0.0050 0.0044 0.0040 0.0117 Rock
4 0.0762 0.0666 0.0481 0.0394 0.0590 0.0649 0.1209 0.2467 0.3564 0.4459 ... 0.0031 0.0054 0.0105 0.0110 0.0015 0.0072 0.0048 0.0107 0.0094 Rock

5 rows × 61 columns

The data has three columns. x1 and x2 are the features, and the label column contains the labels 0 or 1.

Training of the network#

1# Split into features and labels. Convert values at the end into
2# matrix format
3x_input = dataset.iloc[:, :-1]
4y_label = dataset["Class"].values
1# One hot
2labelencoder_Y = LabelEncoder()
3y_label = labelencoder_Y.fit_transform(y_label)
4y_label = y_label.reshape([208, 1])
1# Building the Sequential model
2model = Sequential()
3model.add(Dense(300, input_dim=60, activation="relu"))
4model.add(Dense(200, activation="relu"))
5model.add(Dense(100, activation="relu"))
6model.add(Dense(1, activation="sigmoid"))
1# Providing the training parameters using the compile method
2model.compile(
3    optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]
4)
1# Inspecting the model configuration using the summary function
2model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 300)               18300     
_________________________________________________________________
dense_1 (Dense)              (None, 200)               60200     
_________________________________________________________________
dense_2 (Dense)              (None, 100)               20100     
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 101       
=================================================================
Total params: 98,701
Trainable params: 98,701
Non-trainable params: 0
_________________________________________________________________
1# Training the model by calling the fit() method
2model.fit(x_input, y_label, epochs=30)
Epoch 1/30
7/7 [==============================] - 0s 3ms/step - loss: 0.6731 - accuracy: 0.5983
Epoch 2/30
7/7 [==============================] - 0s 3ms/step - loss: 0.6113 - accuracy: 0.7962
Epoch 3/30
7/7 [==============================] - 0s 6ms/step - loss: 0.5544 - accuracy: 0.7175
Epoch 4/30
7/7 [==============================] - 0s 1ms/step - loss: 0.5103 - accuracy: 0.7520
Epoch 5/30
7/7 [==============================] - 0s 4ms/step - loss: 0.4768 - accuracy: 0.8160
Epoch 6/30
7/7 [==============================] - 0s 4ms/step - loss: 0.4557 - accuracy: 0.7600
Epoch 7/30
7/7 [==============================] - 0s 2ms/step - loss: 0.4286 - accuracy: 0.8079
Epoch 8/30
7/7 [==============================] - 0s 2ms/step - loss: 0.3512 - accuracy: 0.8432
Epoch 9/30
7/7 [==============================] - 0s 3ms/step - loss: 0.3468 - accuracy: 0.8763
Epoch 10/30
7/7 [==============================] - 0s 2ms/step - loss: 0.3317 - accuracy: 0.8637
Epoch 11/30
7/7 [==============================] - 0s 3ms/step - loss: 0.2702 - accuracy: 0.8670
Epoch 12/30
7/7 [==============================] - 0s 3ms/step - loss: 0.2931 - accuracy: 0.8706
Epoch 13/30
7/7 [==============================] - 0s 3ms/step - loss: 0.2411 - accuracy: 0.8963
Epoch 14/30
7/7 [==============================] - 0s 3ms/step - loss: 0.2334 - accuracy: 0.9089
Epoch 15/30
7/7 [==============================] - 0s 2ms/step - loss: 0.2405 - accuracy: 0.9205
Epoch 16/30
7/7 [==============================] - 0s 2ms/step - loss: 0.1576 - accuracy: 0.9672
Epoch 17/30
7/7 [==============================] - 0s 2ms/step - loss: 0.1789 - accuracy: 0.9291
Epoch 18/30
7/7 [==============================] - 0s 2ms/step - loss: 0.1515 - accuracy: 0.9647
Epoch 19/30
7/7 [==============================] - 0s 2ms/step - loss: 0.2146 - accuracy: 0.9204
Epoch 20/30
7/7 [==============================] - 0s 2ms/step - loss: 0.1790 - accuracy: 0.9120
Epoch 21/30
7/7 [==============================] - 0s 2ms/step - loss: 0.1527 - accuracy: 0.9709
Epoch 22/30
7/7 [==============================] - 0s 3ms/step - loss: 0.1009 - accuracy: 0.9874
Epoch 23/30
7/7 [==============================] - 0s 4ms/step - loss: 0.0715 - accuracy: 0.9950
Epoch 24/30
7/7 [==============================] - 0s 2ms/step - loss: 0.0791 - accuracy: 0.9846
Epoch 25/30
7/7 [==============================] - 0s 2ms/step - loss: 0.0948 - accuracy: 0.9781
Epoch 26/30
7/7 [==============================] - 0s 2ms/step - loss: 0.0619 - accuracy: 0.9976
Epoch 27/30
7/7 [==============================] - 0s 3ms/step - loss: 0.0591 - accuracy: 1.0000
Epoch 28/30
7/7 [==============================] - 0s 3ms/step - loss: 0.0612 - accuracy: 0.9866
Epoch 29/30
7/7 [==============================] - 0s 2ms/step - loss: 0.0406 - accuracy: 1.0000
Epoch 30/30
7/7 [==============================] - 0s 3ms/step - loss: 0.0469 - accuracy: 1.0000
<tensorflow.python.keras.callbacks.History at 0x7f4d14e63190>

Statistics#

1# Evaluating the model's performance
2model.evaluate(x_input, y_label)
7/7 [==============================] - 0s 8ms/step - loss: 0.0253 - accuracy: 1.0000
[0.025273527950048447, 1.0]