Credentials checking.
CODE:
age=int(input("ENTER AGE:"))
mem=input("Are you a library member(yes/no):")
if age>=18 :
if mem=='yes':
print("Access granted")
else:
print("Access denied")
Single node neuron with Abs error
CODE:
import numpy as np
input_x=[Link]([0,1,2,3,4])
output_u=[Link]([0,6,12,18,24])
w=3
tolerance=0
max_w=6
while w<=max_w:
predicted_u=w*input_x
error=[Link](predicted_u-output_u)
print(f"-> testing weight w={w}")
print(f"Predicted output :{predicted_u}")
print(f"Target output: {output_u}")
print(f"Error:")
if [Link](error<=tolerance):
print(f" Converged at w={w}-prediction matches target output exactly")
break
w+=1
if not [Link](error<=tolerance):
print("no exact match found")
else:
print("final predicted output:",predicted_u)
Single node neuron with MSE error
import numpy as np
input_x=[Link]([0,1,2,3,4])
output_u=[Link]([0,6,12,18,24])
w=3
max_w=6
mse_threshold=0.01
def m_s_e(pred,actual):
return [Link]((pred-actual)**2)
while w<=max_w:
predicted_u=w*input_x
mse=m_s_e(predicted_u,output_u)
print(f"-> testing weight w={w}")
print(f"Predicted output :{predicted_u}")
print(f"Target output: {output_u}")
print(f"MSE: {mse:.4f}\n")
if [Link](mse<=mse_threshold):
print(f" Converged at w={w}-prediction matches target output exactly")
break
w+=1
if not [Link](mse<=mse_threshold):
print("no exact match found")
else:
print("final predicted output:",predicted_u)
Generate random integers X and Y and create Plots
CODE:
Scatter plot:
import numpy as np
import [Link] as plt
X=[Link](0,101,size=10)
Y=[Link](0,101,size=10)
[Link](X,Y,color='blue',marker='o')
[Link]("scatter plot of X and Y")
[Link]("X values")
[Link]("Y values")
[Link](True)
[Link]()
Line plot:
[Link](X,Y,color='green',linestyle='--',marker='o')
[Link]("line plot of X and Y")
[Link]("X values")
[Link]("Y values")
[Link](True)
[Link]()
Box plot :
[Link]([X,Y], labels=['X','Y'])
[Link]("Box plot of X and Y")
[Link]("values")
[Link](True)
[Link]()
Histogram:
[Link](X,bins=10,alpha=0.5,label="X values")
[Link](Y,bins=10,alpha=0.5,label="Y values")
[Link]("Histogram of X and Y")
[Link]("Values")
[Link]("Frequency")
[Link](True)
[Link]()
Bar Plot:
[Link](range(len(X)),Y,color='orange')
[Link]("bar plot of index and Y")
[Link]("Index")
[Link]("Y values")
[Link](True)
[Link]()
Develop a Python Program to Implement all the Activation Functions used in ANN
along with its derivatives and plot.
#step function
def step(x):
return [Link](x>0,1,0)
#sigmoid function
def sigmoid(x):
return 1/(1+[Link](-x))
#tanh function
def tanh(x):
return [Link](x)
#relu function
def relu(x):
return [Link](0,x)
#leaky_relu
def leaky_relu(x,alpha=0.01):
return [Link](x>0,x,alpha*x)
#softmax
def softmax(x):
exp_x=[Link]([Link](x))
return exp_x/[Link](exp_x)
#plotting
import [Link] as plt
#step
x=[Link](-10,10,100)
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,step(x),color='blue')
[Link](True)
#sigmoid
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,sigmoid(x),color='blue')
[Link](True)
#relu
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,relu(x),color='blue')
[Link](True)
#step_sign
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,step_sign(x),color='blue')
[Link](True)
#tanh
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,tanh(x),color='blue')
[Link](True)
#leaky_relu
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,leaky_relu(x),color='blue')
[Link](True)
#softmax
[Link](figsize=(12,8))
[Link](2,3,1)
[Link](x,softmax(x),color='blue')
[Link](True)
)Implement a Perceptron Learning Rule using Step / Sign Activation functions . Plot
the the line chart for epoch vs Error
import numpy as np
def step01(z):
return 1 if z>=0 else -1
def f(x,w,b):
return [Link](w,x)+b
def train_perceptron(X,y,lr=0.1,e=10):
n_s,n_f=[Link]
w=[Link](n_f)
b=0.0
err=[]
for _ in range(e):
errors=0
for idx,x_i in enumerate(X):
linear_output=[Link](x_i,w)+bias
y_pred=step01(linear_output)
update=lr*(y[idx]-y_pred)
w+=update*x_i
b+=update
errors+=int(update!=0.0)
[Link](errors)
return w,b,err
X=[Link]([
[0,0],
[0,1],
[1,0],
[1,1],
[2,2]])
y=[Link]([0,0,0,1,1])
weights,bias,err=train_perceptron(X,y)
print("learned weights:",weights)
print("learned bias:",bias)
Baxkpropogation
import numpy as np
# X = (hours sleeping, hours studying), y = test score of the student
X = [Link](([2, 9], [1, 5], [3, 6]), dtype=float)
y = [Link](([92], [86], [89]), dtype=float)
# scale units
X = X/[Link](X, axis=0) #maximum of X array
y = y/100 # maximum test score is 100
class NeuralNetwork(object):
def __init__(self):
#parameters
[Link] = 2
[Link] = 1
[Link] = 3
#weights
self.W1 = [Link]([Link], [Link]) #
(3x2) weight matrix from input to hidden layer
self.W2 = [Link]([Link], [Link]) #
(3x1) weight matrix from hidden to output layer
def feedForward(self, X):
#forward propogation through the network
self.z1 = [Link](X, self.W1) #dot product of X (input) and
first set of weights (3x2)
self.a1 = [Link](self.z1) #activation function
self.z2 = [Link](self.a1, self.W2) #dot product of hidden layer
(z2) and second set of weights (3x1)
self.a2=[Link](self.z2)
output = self.a2
return output
def sigmoid(self, s, deriv=False):
if (deriv == True):
return s * (1 - s)
return 1/(1 + [Link](-s))
def backward(self, X, y, output):
#backward propogate through the network
self.output_error = y - output # error in output
self.output_delta = self.output_error * [Link](output,
deriv=True)
self.z1_error = self.output_delta.dot(self.W2.T) #z2 error: how
much our hidden layer weights contribute to output error
self.z1_delta = self.z1_error * [Link](self.z1,
deriv=True) #applying derivative of sigmoid to z2 error
self.W1 += [Link](self.z1_delta) # adjusting first set (input -> hidden) weights
self.W2 += [Link](self.output_delta) # adjusting second
set (hidden -> output) weights
def train(self, X, y):
output = [Link](X)
[Link](X, y, output)
NN = NeuralNetwork()
for i in range(1000): #trains the NN 1000 times
if (i % 100 == 0):
print("Loss: " + str([Link]([Link](y -
[Link](X)))))
[Link](X, y)
print("Input: " + str(X))
print("Actual Output: " + str(y))
print("Loss: " + str([Link]([Link](y - [Link](X)))))
print("\n")
print("Predicted Output: " + str([Link](X)))