What is TanhLayer in PyBrain

This recipe explains what is TanhLayer in PyBrain

Recipe Objective - What is TanhLayer in PyBrain?

Pybrain provides mainly two layers: TanhLayer and SoftmaxLayer. TanhLayer implements the tanh squashing function.

Explore the Real-World Applications of Recommender Systems

For more related projects -

https://www.projectpro.io/projects/data-science-projects/neural-network-projects
https://www.projectpro.io/projects/data-science-projects/keras-deep-learning-projects

Let's try to build a network using TanhLayer -

# Importing libraries
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

# Create a network with two inputs, two hidden, and one output
network = buildNetwork(2, 2, 1, bias=True, hiddenclass=TanhLayer)

# Create a dataset that matches network input and output sizes:
and_gate = SupervisedDataSet(2, 1)

# Create a dataset to be used for testing.
and_testing_data = SupervisedDataSet(2, 1)

# Add input and target values to dataset values for AND truth table
and_gate.addSample((0, 0), (0,))
and_gate.addSample((0, 1), (0,))
and_gate.addSample((1, 0), (0,))
and_gate.addSample((1, 1), (1,))

# Add input and target values to dataset values for AND truth table
and_testing_data.addSample((0, 0), (0,))
and_testing_data.addSample((0, 1), (0,))
and_testing_data.addSample((1, 0), (0,))
and_testing_data.addSample((1, 1), (1,))

#Training the network with dataset and_gate.
backprop_trainer = BackpropTrainer(network, and_gate)

# 5000 iteration on training data.
for iteration in range(5000):
   backprop_trainer.train()

# Testing data
backprop_trainer.testOnData(dataset=and_testing_data, verbose = True)

Output -
Testing on data:
('out:    ', '[-0.001]')
('correct:', '[0     ]')
error:  0.00000020
('out:    ', '[0.001 ]')
('correct:', '[0     ]')
error:  0.00000035
('out:    ', '[0     ]')
('correct:', '[0     ]')
error:  0.00000012
('out:    ', '[1     ]')
('correct:', '[1     ]')
error:  0.00000010
('All errors:', [1.954198645657772e-07, 3.4636537042714615e-07, 1.1671764649484939e-07, 1.0268408027397674e-07])
('Average error:', 1.9029674044043737e-07)
('Max error:', 3.4636537042714615e-07, 'Median error:', 1.954198645657772e-07)
1.9029674044043737e-07

In this way, we can use TanhLayer to build a network in pybrain.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.