0% found this document useful (0 votes)
3 views5 pages

ANN Lab Short

The document contains a series of Python programs demonstrating various neural network implementations, including Perceptron for NOT, AND, and OR gates, Crab and Wine classification using MLP, and analysis of LSTM, RNN, CNN, and GRU. Each section includes code snippets, visualizations, and outputs related to the respective neural network techniques. The programs cover both classification and regression tasks, showcasing the versatility of neural networks in machine learning.

Uploaded by

sagarsagarhr868
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

ANN Lab Short

The document contains a series of Python programs demonstrating various neural network implementations, including Perceptron for NOT, AND, and OR gates, Crab and Wine classification using MLP, and analysis of LSTM, RNN, CNN, and GRU. Each section includes code snippets, visualizations, and outputs related to the respective neural network techniques. The programs cover both classification and regression tasks, showcasing the versatility of neural networks in machine learning.

Uploaded by

sagarsagarhr868
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#1. 1.

Write a program to implement Perceptron using NOT gate

import numpy as np, [Link] as plt


X=[Link]([0,1]); y=[Link]([1,0]); w,b,lr=0.0,0.0,0.1
for _ in range(10):
for xi,yi in zip(X,y):
yhat=1 if xi*w+b>=0 else 0
w+=lr*(yi-yhat)*xi; b+=lr*(yi-yhat)
print("Final Weight:",round(w,1)); print("Final Bias:",round(b,1))
x=[Link](-0.5,1.5,100); [Link](X,y,color='r',label="Target Output")
[Link](x,w*x+b,label="Activation Line"); [Link]("Perceptron NOT Gate")
[Link]("Input"); [Link]("Output"); [Link](); [Link](); [Link]()

#2. 2. Write a program to implement AND and OR gates using Perceptron.


import numpy as np
import [Link] as plt

X = [Link]([[0,0],[0,1],[1,0],[1,1]])
y_and = [Link]([0,0,0,1])
y_or = [Link]([0,1,1,1])

def train(y):
w = [Link](2); b = 0; lr = 0.1
for _ in range(20):
for xi,yi in zip(X,y):
yhat = 1 if [Link](xi,w)+b >= 0 else 0
w = w + lr*(yi-yhat)*xi
b = b + lr*(yi-yhat)
return w,b

w1,b1 = train(y_and)
w2,b2 = train(y_or)

[Link](figsize=(6,8))

[Link](2,1,1)
[Link](X[:,0],X[:,1],c=y_and)
x = [Link](-0.5,1.5)
[Link](x,(-w1[0]*x-b1)/w1[1],'k--')
[Link]("AND Gate Decision Boundary")
[Link]("Input X1"); [Link]("Input X2")

[Link](2,1,2)
[Link](X[:,0],X[:,1],c=y_or)
[Link](x,(-w2[0]*x-b2)/w2[1],'k--')
[Link]("OR Gate Decision Boundary")
[Link]("Input X1"); [Link]("Input X2")

print("AND Gate Weights:",w1,"Bias:",b1)


print("OR Gate Weights:",w2,"Bias:",b2)

[Link]()

#3. 3. Write a program to Implement Crab Classification using pattern net.


import numpy as np
import [Link] as plt
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from [Link] import classification_report

[Link](0)
X1 = [Link]([-1,-1],0.3,(60,2))
X2 = [Link]([1,1],0.3,(60,2))
X = [Link]((X1,X2))
y = [Link]([0]*60 + [1]*60)

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3)

model = MLPClassifier(hidden_layer_sizes=(5,),max_iter=1000)
[Link](X_train,y_train)
pred = [Link](X_test)

print(classification_report(y_test,pred))

[Link](X_train[:,0],X_train[:,1],c=y_train,marker='o',label="Train")
[Link](X_test[:,0],X_test[:,1],c=y_test,marker='x',label="Test")
[Link]("Crab Classification using Pattern Net")
[Link]("Scaled Claw Size")
[Link]("Scaled Shell Hardness")
[Link]()
[Link]()

#4. 4. Write a program to implement Wine Classification using Back propagation.


import numpy as np
import [Link] as plt
from [Link] import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from [Link] import classification_report, confusion_matrix
from [Link] import PCA

data = load_wine()
X_train,X_test,y_train,y_test =
train_test_split([Link],[Link],test_size=0.3)

model = MLPClassifier(hidden_layer_sizes=(10,),max_iter=100)
[Link](X_train,y_train)
pred = [Link](X_test)

print("Accuracy:",[Link](X_test,y_test))
print(classification_report(y_test,pred))

loss = model.loss_curve_
[Link](loss)
[Link]("Training Loss vs Epochs")
[Link]("Epoch")
[Link]("Loss")
[Link]()

cm = confusion_matrix(y_test,pred)
[Link](cm)
[Link]("Confusion Matrix")
[Link]()
[Link]()

pca = PCA(n_components=2)
X2 = pca.fit_transform([Link])
[Link](X2[:,0],X2[:,1],c=[Link])
[Link]("Decision Boundary (Wine Classification)")
[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]()

#5. Jacobian and Hessian


import numpy as np
import [Link] as plt

# Sample input
x = [Link](-2,2,100)

# Neural network function


y = x**3 + 2*x**2

# Jacobian (first derivative)


J = 3*x**2 + 4*x

# Hessian (second derivative)


H = 6*x + 4

# Plot
[Link](x,y,label="Function")
[Link](x,J,label="Jacobian")
[Link](x,H,label="Hessian")

[Link]()
[Link]("Jacobian and Hessian in Neural Network")
[Link]()

#6. 6. Write a program to implement classification of Linearly Separable Data


with a perceptron.
import numpy as np
import [Link] as plt

X=[Link]([[1,2],[2,3],[3,3],[5,5],[6,6],[7,7]])
y=[Link]([0,0,0,1,1,1])
w=[Link](2); b=0; lr=0.1

for _ in range(20):
for xi,yi in zip(X,y):
yhat=1 if [Link](xi,w)+b>=0 else 0
w+=lr*(yi-yhat)*xi; b+=lr*(yi-yhat)

print("Final Weights:",w); print("Final Bias:",b)

[Link](X[:,0],X[:,1],c=y)
x=[Link](0,8)
[Link](x,(-w[0]*x-b)/w[1],'k--')
[Link]("Classification of Linearly Separable Data using Perceptron")
[Link]("X1"); [Link]("X2")
[Link]()
#7. 7. Write a program to analyse Long Short Term Memory
import numpy as np
import [Link] as plt
from sklearn.neural_network import MLPRegressor

t=[Link](0,50)
y=[Link](t)
X=[Link](-1,1)

model=MLPRegressor(hidden_layer_sizes=(10,),max_iter=1000)
[Link](X,y)
pred=[Link](X)

[Link](t,y,label="Actual")
[Link](t,pred,label="Predicted")
[Link]("LSTM Analysis (Sequence Prediction)")
[Link]("Time"); [Link]("Value")
[Link](); [Link]()

#8. 8. Write a program to analyse Recurrent Neural Network


import numpy as np
import [Link] as plt
from sklearn.neural_network import MLPRegressor

t = [Link](0,50)
y = [Link](t)
X = [Link](-1,1)

model = MLPRegressor(hidden_layer_sizes=(10,), max_iter=1000)


[Link](X,y)
pred = [Link](X)

[Link](t,y,label="Actual")
[Link](t,pred,label="Predicted")
[Link]("RNN Analysis (Sequence Prediction)")
[Link]("Time"); [Link]("Value")
[Link](); [Link]()

#9. 9. Write a program to analyze Convolution Neural Network


import numpy as np
import [Link] as plt

img = [Link](10,10)
kernel = [Link]([[1,0,-1],
[1,0,-1],
[1,0,-1]])
conv = [Link]((8,8))

for i in range(8):
for j in range(8):
conv[i,j] = [Link](img[i:i+3,j:j+3] * kernel)

[Link](1,2,1)
[Link](img)
[Link]("Input Image")
[Link](1,2,2)
[Link](conv)
[Link]("Feature Map (Convolution)")

[Link]()

#10. 10. Write a program to analyse Gated Recurrent Units


import numpy as np
import [Link] as plt

# Input sequence
x = [Link](1,11)

# Simulated GRU output using sigmoid activation


y = 1/(1+[Link](-x/2))

# Plot sequence vs GRU output


[Link](x,y,marker='o')
[Link]("GRU Output Analysis")
[Link]("Time Step")
[Link]("GRU Output")
[Link]()

You might also like