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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

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.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

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