This lesson is being piloted (Beta version)

Neural Network

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How to use Neural Network in Machine Learning model

Objectives
  • Learn how to use ANN in ML model

8 Neural Network

8.1 The Neural Network of a brain

image

8.2 Neural Network in Machine Learning:

image

8.3 Formulation of Neural Network:

image

Here:

In which:

Activation functions:

image

8.4 Multi-Layer Perceptron (MLP)

Multi-layer Perceptron (MLP) is a supervised learning algorithm. Given a set of features X = x1, x2, ... xm, and target y, MLP can learn a non-linear function approximator for either classification or regression.

Between the input and the output layer, there can be one or more non-linear layers, called hidden layers. Figure below shows a one hidden layer MLP with scalar output.

image

image

The advantages of Multi-layer Perceptron:

The disadvantages of Multi-layer Perceptron:

8.5 Type of Neural Network Multi-Layer Perceptron in sklearn

Similar to previous Machine Learning model, there are 2 main types of MLP in sklearn, depending on the model output:

8.6 Implementation with Classification problem

Here we use Breast Cancer Wisconsine data for Classification problem

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer()
X = data.data
y = data.target

scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

The Class MLPClassifier implements a multi-layer perceptron (MLP) algorithm that trains using Backpropagation. There are lots of parameters in MLPClassifier:

More information can be found here

from sklearn.neural_network import MLPClassifier
model_NN = MLPClassifier(hidden_layer_sizes = (50,20),solver='lbfgs',activation='relu',random_state=123).fit(X_train_scaled, y_train)
model_NN.score(X_test_scaled,y_test)

8.7 Implementation with Regression problem

Here we use california housing data from Regression espisode:

import pandas as pd
import numpy as np

from sklearn.datasets import fetch_california_housing

data = fetch_california_housing()

# Predictors/Input:
X = pd.DataFrame(data.data,columns=data.feature_names)

# Predictand/output:
y = pd.DataFrame(data.target,columns=data.target_names)

Fit MLPRegressor model

from sklearn.neural_network import MLPRegressor
model_NN = MLPRegressor(hidden_layer_sizes = (10,5),solver='lbfgs',activation='tanh',max_iter=1000).fit(X_train,y_train)
model_NN.score(X_test,y_test)

8.8 Tips on using MLP

8.9. Notes

Key Points

  • ANN