How to train a network using trainers in PyBrain
Last Updated :
21 Feb, 2022
In this article, we will discuss how to train a network using trainers in PyBrain.
Network: A network consists of several modules. These modules are generally connected with connections. PyBrain provides programmers with the support of neural networks. A network can be interpreted as an acyclic directed graph where each module serves the purpose of vertex/node and connections are assumed to edge.
Dataset: A Dataset is a collection of data that is passed for testing, validating, and training on networks. A dataset is more flexible and easy to use as compared to arrays. It is quite similar to a collection of named 2d-arrays. Each task in machine learning requires specific datasets.
Training a network using trainers in PyBrain:
PyBrain provides two trainers to test a network. These networks are discussed below,
1. BackpropTrainer:
This trainer is used to train parameters of the module as per the ClassificationDataSet or monitored dataset that propagates errors backward(over time).
2. TrainUntilConvergence:
TrainUntilConvergence is specifically used to train the module on the dataset unless it converges. While developing a neural network, the network is trained based on the data passed. To determine whether the network is significantly trained depends on the prediction of the test data tested on that network.
Example:
In this example, we have created two datasets type SupervisedDataSet. We are using the NAND data model that is given below:
A | B | A NAND B |
---|
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The dataset used to test is given below:
Python3
# Creating a dataset for testing
nand_train = SupervisedDataSet(2, 1)
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_train.addSample((0, 0), (1,))
nand_train.addSample((0, 1), (1,))
nand_train.addSample((1, 1), (1,))
nand_train.addSample((1, 1), (0,))
The trainer used is given below:
Python3
# Training the network with dataset nand_gate
trainer = BackpropTrainer(network, nand_gate)
# Iterate 100 times to train the network
for epoch in range(100):
trainer.train()
trainer.testOnData(dataset=nand_train, verbose = True)
Example:
Python3
# Python program to demonstrate how to train
# a network
# Importing libraries and packages
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
# Establishing a network having two inputs,
# four hidden, and one output channels
network = buildNetwork(2, 4, 1, bias=True, hiddenclass=TanhLayer)
# Creating a dataset that match with the
# network input and output sizes
nand_gate = SupervisedDataSet(2, 1)
# Creating a dataset for testing
nand_train = SupervisedDataSet(2, 1)
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_gate.addSample((0, 0), (1,))
nand_gate.addSample((0, 1), (1,))
nand_gate.addSample((1, 0), (1,))
nand_gate.addSample((1, 1), (0,))
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_train.addSample((0, 0), (1,))
nand_train.addSample((0, 1), (1,))
nand_train.addSample((1, 1), (1,))
nand_train.addSample((1, 1), (0,))
# Training the network with dataset nand_gate
trainer = BackpropTrainer(network, nand_gate)
# Iterate 10 times to train the network
for epoch in range(100):
trainer.train()
trainer.testOnData(dataset=nand_train, verbose=True)
Output:
Interpretation: As you can see in the output, the test data matches with the dataset that has been used, and hence the error is 0.021 only.
Now, let us change the data and run the program again.
Python3
# Creating a dataset for testing
nand_train = SupervisedDataSet(2, 1)
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_train.addSample((0, 0), (1,))
nand_train.addSample((0, 1), (0,))
nand_train.addSample((1, 0), (1,))
nand_train.addSample((1, 1), (0,))
Example:
Python3
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
# Establishing a network having two inputs,
# four hidden, and one output channels
network = buildNetwork(2, 4, 1, bias=True, hiddenclass=TanhLayer)
# Creating a dataset that match with the
# network input and output sizes
nand_gate = SupervisedDataSet(2, 1)
# Creating a dataset for testing
nand_train = SupervisedDataSet(2, 1)
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_gate.addSample((0, 0), (1,))
nand_gate.addSample((0, 1), (0,))
nand_gate.addSample((1, 0), (1,))
nand_gate.addSample((1, 1), (0,))
# Fit input and target values to dataset
# Parameters for nand_train truth table
nand_train.addSample((0, 0), (1,))
nand_train.addSample((0, 1), (1,))
nand_train.addSample((1, 0), (1,))
nand_train.addSample((1, 1), (0,))
# Training the network with dataset nand_gate
trainer = BackpropTrainer(network, nand_gate)
# Iterate 10 times to train the network
for epoch in range(100):
trainer.train()
trainer.testOnData(dataset=nand_train, verbose=True)
Output:
Interpretation:
As you can see in the output, the test data doesn't exactly match with the network trainer and hence the average error is 0.129 which is greater than the previous example.
Similar Reads
How To Save The Network In XML File Using PyBrain
In this article, we are going to see how to save the network in an XML file using PyBrain in Python. A network consists of several modules. These modules are generally connected with connections. PyBrain provides programmers with the support of neural networks. A network can be interpreted as an acy
2 min read
Train and Test Neural Networks Using R
Training and testing neural networks using R is a fundamental aspect of machine learning and deep learning. In this comprehensive guide, we will explore the theory and practical steps involved in building, training, and evaluating neural networks in R Programming Language. Neural networks are a clas
11 min read
Training a Neural Network using Keras API in Tensorflow
In the field of machine learning and deep learning has been significantly transformed by tools like TensorFlow and Keras. TensorFlow, developed by Google, is an open-source platform that provides a comprehensive ecosystem for machine learning. Keras, now fully integrated into TensorFlow, offers a us
3 min read
How to optimize networking using optimization algorithms in PyBrain
In this article, we are going to see how to optimize networking using optimization algorithms in PyBrain using Python. In the field of machine learning, Optimization algorithms are specifically used to reduce certain functions known as loss function/error function. By loss function, the optimization
3 min read
How To Do Train Test Split Using Sklearn In Python
In this article, let's learn how to do a train test split using Sklearn in Python. Train Test Split Using Sklearn The train_test_split() method is used to split our data into train and test sets. First, we need to divide our data into features (X) and labels (y). The dataframe gets divided into X_t
5 min read
How to import datasets using sklearn in PyBrain
In this article, we will discuss how to import datasets using sklearn in PyBrain Dataset: A Dataset is defined as the set of data that is can be used to test, validate, and train on networks. On comparing it with arrays, a dataset is considered more flexible and easy to use. A dataset resembles a 2-
2 min read
How to visualize training progress in PyTorch?
Deep learning and understanding the mechanics of learning and progress during training is vital to optimize performance while diagnosing problems such as underfitting or overfitting. The process of visualizing training progress offers valuable insights into the dynamics of learning that allow us to
9 min read
How to Install PyBrain on Windows?
PyBrain is an open-source and free-to-use Python-based Machine Learning Library. Its main purpose is to provide machine learning tasks with flexible, easy-to-use, still a very powerful algorithms. It also provides a wide range of predefined environments which is used to test and compare different ty
2 min read
Training of Convolutional Neural Network (CNN) in TensorFlow
In this article, we are going to implement and train a convolutional neural network CNN using TensorFlow a massive machine learning library. Now in this article, we are going to work on a dataset called 'rock_paper_sissors' where we need to simply classify the hand signs as rock paper or scissors.
4 min read
How to split a Dataset into Train and Test Sets using Python
One of the most important steps in preparing data for training a ML model is splitting the dataset into training and testing sets. This simply means dividing the data into two parts: one to train the machine learning model (training set), and another to evaluate how well it performs on unseen data (
3 min read