Introduction to Neural Networks With Scikit-Learn

0
13

Introduction

Neural Networks (NNs) are the most commonly used tool in Machine Learning (ML). By the end of this article, you will be familiar with the theoretical concepts of a neural network, and a simple implementation with Python’s Scikit-Learn.

Neural Networks in Theory

In Supervised Learning ML techniques, at first, the model must be trained on some data. Neural networks traditionally follow Supervised Learning, and the network improves its accuracy over iterations/epochs. A neural network is a statistical tool to interpret a set of features in the input data and it tries to either classify the input (Classification) or predict the output based on a continuous input (Regression).

Data Pre-processing

It consists of algorithms, such as normalization, to make input data suitable for training. Normalization is done to ensure that the data input to a network is within a specified range. A typical normalization formula for numerical data is given below:

x_normalized = (x_input – mean(x)) / (max(x) – min(x))

The formula above changes the values of all inputs x from R to [0,1]. There are various preprocessing techniques which are used with the aim of applying a linear transformation, or detecting and minimizing outliers and reducing the size of the input space.

Logistic Regression and Perceptron

In a nutshell, a Logistic Regression is a Classifier, where every input is a feature set and an output are an N-dimensional vector (for N classes). Every output value is in [0,1], indicating the probability of an input belonging to the corresponding class. A Logistic Regression model is a one-layered neural network. A perceptron is the same as Logistic Regression, but with a different activation function, producing an output in {-1, 1}.

When multiple perceptrons are used in layers, it essentially forms a neural network.

Bias and Variance

In a network, Bias can be considered as the DC component of a neuron: The output of the neuron is shifted by B. This can be understood clearly in the next section.

In terms of training, the bias and variance define the dataset: If a dataset has too many samples for an output, the network becomes biased toward it. Variance is the error produced due to the variety of samples associated with an output, and high variance implies that the algorithm models the randomness of the data and leads to overfitting. Thus, you will be staring at a trade-off between bias and variance: Bias-Variance Tradeoff

Weights

Every node in the neural network has a weight W associated with it. If the input to a neuron is X, then the output it produces is WX+B (B=bias)

A sample network is shown in the figure below. Each circle represents a neuron which has a weight W associated with it.

Neural Networks

[Image Source]

Activation Function

An activation function is what separates a neural network from a vanilla linear regression model. It defines a function, which enables the neuron to convert an input to an output value. The most common activation functions for hidden layers of a neural network are:

  1. Sigmoid: f(x) = 1/1+e^(-x). [Range: (0,1); slow convergence; non-zero mean-value]
  2. Tanh: f(x)=1-e^(-2x) / 1+e^(-2x). [Range: (-1,1); quick convergence; zero mean-value]
  3. ReLu: f(x)=0 (if x<0), x (if x>=0). [Range: R+; ReLu can lead to dead neurons]
  4. Leaky ReLu: f(x)=-ax (if x<0), ax (if x>=0). [Range: R; Used to avoid dead neurons]

In the output layer of the neural network, the softmax activation function is used for classification and the linear activation function is used for regression.

Training a network

Undoubtedly, the most important part of a neural network is the dataset. When building a model, you must have three partitions in your dataset:

  1. Training Data: For any implementation, the first thing required will be the training data. This is the data that your model iterates over continuously and updates itself to improve its accuracy.
  2. Validation Data: You should use some samples from your dataset which are NOT seen in the training data, this gives you an estimate of the model’s skill in terms of tuning the hyperparameters. The validation dataset is important in order to avoid overfitting the network.
  3. Test Data: The test data contains all samples that the model is likely to see in its usage, it can contain samples present in training data and validation data. This data is used to find the accuracy/predictive capabilities of the model.

You may either partition the dataset randomly (NumPy is helpful in such cases) or have the dataset split before you load the training, validation, and test data in your code. It is always good to use samples from the training and validation data in your test data.

Cost Function

The cost function of a network is used in the update process after every iteration. There is a variety of cost functions available such as Distance functions, Mean-Square-Error (MSE), Bernoulli log-likelihood, Exponential cost, etc. The cost function is application-dependent.

A Simple Implementation in Scikit-Learn

 

Conclusion

In this article, we built a simple Neural Network in sklearn for classification on the iris dataset. Depending on the development of a model, you should be tuning its hyperparameters and carefully choose an appropriate evaluation metric.

You should also consider reading about the Keras and Tensorflow Python libraries, which are two of the most commonly used libraries for developing neural networks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here