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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

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.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.