Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Neural Network with scikit-learn | Neural Network from Scratch
Introduction to Neural Networks

book
Neural Network with scikit-learn

Working with neural networks can be quite tricky, especially if you're trying to build them from scratch. Instead of manually coding algorithms and formulas, you can use ready-made tools such as the sklearn library.

Benefits of Using sklearn

  1. Ease of use: you don't have to dive deep into the details of each algorithm. You can simply use ready-made methods and classes;

  2. Optimization: the sklearn library is optimized for performance, which can reduce the training time of your model;

  3. Extensive documentation: sklearn provides extensive documentation with usage examples, which can greatly speed up the learning process;

  4. Compatibility: sklearn integrates well with other popular Python libraries such as numpy, pandas and matplotlib.

Perceptron in sklearn

To create the same model as in this section, you can use the MLPClassifier class from the sklearn library. Its key parameters are as follows:

  • max_iter: defines the maximum number of epochs for training;
  • hidden_layer_sizes: specifies the number of neurons in each hidden layer as a tuple;
  • learning_rate_init: sets the learning rate for weight updates.

For example, with a single line of code, you can create a perceptron with two hidden layers of 10 neurons each, using at most 100 epochs for training and a learning rate of 0.5:

from sklearn.neural_network import MLPClassifier

model = MLPClassifier(max_iter=100, hidden_layer_sizes=(10,10), learning_rate_init=0.5)

As with our implementation, training the model simply involves calling the fit() method:

model.fit(X_train, y_train)

To get the predicted labels (e.g., on the test set), all you have to do is call the predict() method:

y_pred = model.predict(X_test)
Task

Swipe to start coding

Your goal is to create, train, and evaluate a perceptron with the same structure as the one you previously implemented, but using the sklearn library:

  1. Initialize a perceptron with 100 training epochs, two hidden layers of 6 neurons each, and a learning rate of 0.01 (set the parameters in this exact order).
  2. Train the model on the training data.
  3. Obtain predictions on the test set.
  4. Compute the accuracy of the model on the test set.

Solution

from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
import warnings
# Ignore warnings
warnings.filterwarnings("ignore")
import os
os.system('wget https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f9fc718f-c98b-470d-ba78-d84ef16ba45f/section_2/perceptron.py 2>/dev/null')
from perceptron import X, y

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)
# 1. Initialize a perceptron
model = MLPClassifier(max_iter=100, hidden_layer_sizes=(6, 6), learning_rate_init=0.01, random_state=10)
# 2. Train the model
model.fit(X_train, y_train)
# 3. Obtain predictions on the test set
y_pred = model.predict(X_test)
# 4. Compute the accuracy on the test set
score = accuracy_score(y_test, y_pred)
print(f'Accuracy: {score:.3f}')
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 13
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
import warnings
# Ignore warnings
warnings.filterwarnings("ignore")
import os
os.system('wget https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f9fc718f-c98b-470d-ba78-d84ef16ba45f/section_2/perceptron.py 2>/dev/null')
from perceptron import X, y

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)
# 1. Initialize a perceptron
model = ___(___=___, ___=___, ___=___, random_state=21)
# 2. Train the model
___
# 3. Obtain predictions on the test set
y_pred = ___
# 4. Compute the accuracy on the test set
score = ___
print(f'Accuracy: {score:.3f}')
toggle bottom row
some-alt