How to build a two layer neural network model using theano?

This recipe helps you to build a two layer neural network model using theano.

Recipe Objective - How to build a two-layer neural network model using theano?

Let's try to build a two-layer neural network model, where x1 and x2 are input neurons, b1 and b2 are biases, y1 are y2 are hidden neurons and y3 is output neuron.

For more related projects -

/projects/data-science-projects/tensorflow-projects
/projects/data-science-projects/neural-network-projects

Example -

# Importing libraries
import theano
from theano import tensor
from theano.ifelse import ifelse
import numpy as np
from random import random

# Creating variables:
x = tensor.matrix('x') #Input neurons
w1 = theano.shared(np.array([random(),random()])) #Random generation of weights
w2 = theano.shared(np.array([random(),random()]))
w3 = theano.shared(np.array([random(),random()]))
b1 = theano.shared(1.) #Bias
b2 = theano.shared(1.)
rate_of_learning = 0.01 # Learning rate

y1 = 1/(1+tensor.exp(-tensor.dot(x,w1)-b1))
y2 = 1/(1+tensor.exp(-tensor.dot(x,w2)-b1))
x2 = tensor.stack([y1,y2],axis=1)
y3 = 1/(1+tensor.exp(-tensor.dot(x2,w3)-b2))

actual = tensor.vector('actual') #Actual output
cost = -(actual*tensor.log(y3) + (1-actual)*tensor.log(1-y3)).sum()
dervw1,dervw2,dervw3,dervb1,dervb2 = tensor.grad(cost,[w1,w2,w3,b1,b2])

# Model training
model_train = theano.function( inputs = [x,actual], outputs = [y3,cost], updates = [ [w1, w1-rate_of_learning*dervw1], [w2, w2-rate_of_learning*dervw2], [w3, w3-rate_of_learning*dervw3], [b1, b1-rate_of_learning*dervb1], [b2, b2-rate_of_learning*dervb2] ] )

inputs = [ [0, 0], [0, 1], [1, 0], [1, 1] ]
outputs = [0,1,0,1]

# Iterate through all inputs and find outputs:
cost = []
for i in range(100000):
 pred, cost_iteration = model_train(inputs, outputs)
 cost.append(cost_iteration)

# Output
print('The outputs of the Neural network are => ')
for i in range(len(inputs)):
 print('The output for x1 = {} | x2 = {} => {}'.format(inputs[i][0],inputs[i][1],pred[i]))

# Ploting the flow of cost:
print('\nThe flow of cost =>')
import matplotlib.pyplot as plt
plt.plot(cost)

Output -
The outputs of the Neural network are => 
The output for x1 = 0 | x2 = 0 => 0.0019436800954405846
The output for x1 = 0 | x2 = 1 => 0.9987640029633112
The output for x1 = 1 | x2 = 0 => 0.00025952989610139815
The output for x1 = 1 | x2 = 1 => 0.9986304719904834

In this way, we can build a two-layer neural network model using theano.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

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.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

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.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.