1. Concepts & Definitions
1.1. Regression versus Classification
1.3. Parameter versus Hyperparameter
1.4. Training, Validation, and Test
2. Problem & Solution
2.1. Gaussian Mixture x K-means on HS6 Weight
2.2. Evaluation of classification method using ROC curve
2.3. Comparing logistic regression, neural network, and ensemble
2.4. Fruits or not, split or encode and scale first?
Neurons in neural network were inspired by neurons in the human brain. Here is a diagram of the anatomy of a brain neuron [1]:
This brain neuron become the basic building block of Artificial Neural Networks: the artificial neuron. The first mathematical model dates back to Warren McCulloch and Walter Pitts, who proposed it in 1942, hence at the very beginning of the electronic computer age during World War II. The MCP neuron depicted in Fig. 4 is a basic ingredient of all ANNs discussed in this course. It is built on very simple general rules, inspired neatly by the biological neuron [2]:
The signal enters the nucleus via dendrites from other neurons.
The synaptic connection for each dendrite may have a different (and adjustable) strength (weight).
In the nucleus, the signal from all the dendrites is combined (summed up) into.
If the combined signal is stronger than a given threshold, then the neuron fires along the axon, in the opposite case it remains still.
In the simplest realization, the strength of the fired signal has two possible levels: on or off, i.e. 1 or 0. No intermediate values are needed.
Axon terminal connects to dendrites of other neurons.
Here is a diagram of the functionality of a neuron in a neural net [1]:
Let’s walk through this diagram step-by-step.
As you can see, neurons in a deep learning model are capable of having synapses that connect to more than one neuron in the preceding layer. Each synapse has an associated weight, which impacts the preceding neuron’s importance in the overall neural network.
Weights are a very important topic in the field of deep learning because adjusting a model’s weights is the primary way through which deep learning models are trained. You’ll see this in practice later on when we build our first neural networks from scratch.
Once a neuron receives its inputs from the neurons in the preceding layer of the model, it adds up each signal multiplied by its corresponding weight and passes them on to an activation function, like this [1]:
Translating this into a mathematical prescription, one assigns to the input cells the numbers x1, x2, ..., xn (input data point). The strength of the synaptic connections is controlled with the weights wi. Then the combined signal is defined as the weighted sum [2]:
The signal becomes an argument of the activation function, which, in the simplest case, takes the form of the step function:
When the combined signal s is larger than the bias (threshold), the nucleus fires. i.e. the signal passed along the axon is 1. in the opposite case, the generated signal value is 0 (no firing). This is precisely what we need to mimic the biological prototype.
For more details on creating and manipulating activation functions, please see [2, 3]. An example of activation function is given in [4] and is as th next figure. Observe that it resembles to the curve provided by the Logistic Regression.
The activation function calculates the output value for the neuron. This output value is then passed on to the next layer of the neural network through another synapse as it could be seen in the next topic.
As described in [5] neurons could be combined to create a neural network as shown in the next figure:
A detailed explanation of the mathematics behind a neural network and its layers (input, hidden, and output) is given at [6, 7].
As shown by [8], a variety of neural network topologies are possible as shown in the next figure.
To just illustrate the potential of neural networks, the Feed Forward (FF) architecture will be employed.
Neural networks could be created from scratch as done in [9], but a much more simpler alternative is to use the Python Package Keras [10]. The next numerical example employs this package in a numerical example of binary classification that had been already addressed in this track.
The next link provides a Python code to read the data and could be used as an initial base to create neural networks tests:
https://colab.research.google.com/drive/1f8_LkwYJ6TwJfGo8m62jDbAAke76dcS0?usp=sharing
The previous code could be added with the following code to create and configure a neural network in which has input layer has 60 neurons, the hidden layer has 32 neurons, in both layers all neurons employ a 'relu' activation function, and the output layer has just 1 neuron with a 'sigmoid' activation function which is the most appropriate for binary classification. For a problem with multiple labels, the variable could be transformed into an array of binary values as described in [6] and in the next figure.
But, let's return the code to solve the binary classification problem.
from keras.models import Sequential
from keras.layers import Dense
from random import seed
seed(1)
model = Sequential([
Dense(32, activation='relu', input_shape=(X_train.shape[1],)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid'),
])
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy'])
Now, let's train the neural network and try to see it's accuracy in the test data.
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
import matplotlib.pyplot as plt
# Fit the model
#model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, callbacks=callbacks_list, verbose=0)
hist = model.fit(X_train, y_train,
batch_size=10, epochs=150,
verbose=0)
score = model.evaluate(X_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
accuracy: 93.94%
To extract specific predictions for each value of the test dataset the following code could be employed.
import numpy as np
yhat = model.predict(X_test)
yhat = np.hstack(yhat)
yhat
array([5.7209113e-06, 4.6386916e-02, 1.3030034e-01, 9.9999893e-01, 5.6801824e-04, 4.3865874e-01, 9.3893695e-04, 6.0202630e-05, 1.2160371e-03, 9.9935609e-01, 1.8311395e-04, 9.9999225e-01, 1.3821785e-04, 9.9996394e-01, 9.9997091e-01, 9.9999905e-01, 1.7163566e-01, 1.0659255e-03, 6.2829161e-01, 9.9999779e-01, 6.3525772e-01, 5.0400107e-05, 5.6435580e-05, 9.9949783e-01, 9.9999946e-01, 9.9999326e-01, 9.9931163e-01, 6.3111102e-03, 2.6265846e-04, 9.9999595e-01, 9.9999958e-01, 1.5029396e-02, 1.6175528e-04], dtype=float32)
It is necessary to employ a threshold to transform the returned values into 0 or 1.
threshold = 0.5
yhat = np.where(yhat > threshold, 1, 0)
yhat
array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0])
Now, it is possible to compare the results on the test dataset and the value predicted by the neural network.
y_test, yhat
(array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0]),
array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0]))
This comparison of each value in the test dataset could be done more quickly and easily using the following code.
# make class predictions with the model
predictions = (model.predict(X_test) > 0.5).astype(int)
predictions = np.hstack(predictions)
predictions
2/2 [==============================] - 0s 7ms/step
array([0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0])
The Python code with all the steps is summarized in this Google Colab (click on the link):
https://colab.research.google.com/drive/1ANZ6eRdPCr7gCGmg4tkgLyFwG_pwLymJ?usp=sharing
[1] https://www.freecodecamp.org/news/deep-learning-neural-networks-explained-in-plain-english/
[2] https://bronwojtek.github.io/neuralnets-in-raw-python/docs/mcp.html
[3] https://machinelearningmastery.com/choose-an-activation-function-for-deep-learning/
[4] https://towardsdatascience.com/first-neural-network-for-beginners-explained-with-code-4cfd37e06eaf
[5] https://medium.com/@sadafsaleem5815/neural-networks-in-10mins-simply-explained-9ec2ad9ea815
[6] https://python-course.eu/machine-learning/training-neural-network-with-python.php
[7] https://dimleve.medium.com/back-propagation-explained-9720c2d4a566
[8] https://towardsdatascience.com/the-mostly-complete-chart-of-neural-networks-explained-3fb6f2367464
[9] https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/
[10] https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
Neural Network
A numerical example