What is TanhLayer in PyBrain
Last Updated :
21 Feb, 2022
In this article, we will be looking at various functionality with the defined examples of the TanhLayer in PyBrain. Layers in Pybrain are functions that are used on hidden layers of the network. TanhLayer executes the tanh squashing function.
Syntax:
Import TanhLayer: from pybrain.structure import TanhLayer
Use in python code: net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)
Example 1:
In this example, we import the TanhLayer using the import command to create the network using buildNetwork() with input, hidden, and output layer. We take a hidden class as TanhLayer, Now give the sizes of input and output dataset using SupervisedDataSet(). To add sample dataset to AND table and NOR table. Then train this network using BackpropTrainer(). We have 2500 iterations and then testing starts and we can see the errors, corrections, max, errors, etc.In this case, the sample data we have taken in AND table are ((0,0), (0,)) and ((0,1),(1,)) and NOR table are ((0,0),(0,)) and (0,1),(1,))
Python
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
# two inputs, two hidden, and one output
net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)
gate_set = SupervisedDataSet(2, 1)
test_dataset = SupervisedDataSet(2, 1)
# AND truth table
gate_set.addSample((0, 0), (0,))
gate_set.addSample((0, 1), (1,))
# NOR truth table
test_dataset.addSample((0, 0), (0,))
test_dataset.addSample((0, 1), (1,))
# Train the network with net and gate_set
backpr_tr = BackpropTrainer(net, gate_set)
# 2500 iteration
for i in range(2500):
backpr_tr.train()
# Testing....
backpr_tr.testOnData(dataset=test_dataset, verbose = True)
Output:
Example 2:
Under this example, the sample data we have taken in AND table are ((0,0), (1,)) and ((0,1),(1,)) and NOR table are ((0,0),(0,)) and (0,1),(0,)) and we can see the testing output with average errors, max errors, median errors, etc.
Python
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
# two inputs, two hidden, and one output
net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer)
gate_set = SupervisedDataSet(2, 1)
test_dataset = SupervisedDataSet(2, 1)
# AND truth table
gate_set.addSample((0, 0), (1,))
gate_set.addSample((0, 1), (1,))
# NOR truth table
test_dataset.addSample((0, 0), (0,))
test_dataset.addSample((0, 1), (0,))
#Train the network with net and gate_set
backpr_tr = BackpropTrainer(net, gate_set)
# 2500 iteration
for i in range(2500):
backpr_tr.train()
# Testing....
backpr_tr.testOnData(dataset=test_dataset, verbose = True)
Output:
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Machin
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
100+ Machine Learning Projects with Source Code [2025] This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an
5 min read
K means Clustering â Introduction K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ
4 min read