<jupyter_start><jupyter_text>DL Problem Set Computational Programming Proof Perceptron Algorithm Script<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>>In preparation for perceptron algorithm, the least square method will be used<jupyter_code>def activation_function(y):
    if y > 0:
        y = 1
    elif y <= 0:
        y = 0
    return y

W = np.array([-1, -1])
b = 1


def perceptron_algorithm(x):
    y = np.dot(W, x) + b
    y = activation_function(y)
    return y

# Input values to verify the NOR logic 
input1 = np.array([0, 0])
input2 = np.array([0, 1])
input3 = np.array([1, 0])
input4 = np.array([1, 1])

# Printing the results
print('NOR Logic: \n')
print(f'x1 = 0 and x2 = 0 => y = {perceptron_algorithm(input1)}')
print(f'x1 = 0 and x2 = 1 => y = {perceptron_algorithm(input2)}')
print(f'x1 = 1 and x2 = 0 => y = {perceptron_algorithm(input3)}')
print(f'x1 = 1 and x2 = 1 => y = {perceptron_algorithm(input4)}')
X = np.array([[1,0,0],
 [1,0,1],
 [1,1,0 ],
 [1,1,1]])

y=np.array([[0],
 [0],
 [0],
 [1]])
def sigmoid_func(x):
 y=1+np.exp(-x)
 y=1/y
 return y

def fwd_pass(X_training, wt1, wt2):
  a1=np.matmul(X_training,wt1)
  z1=sigmoid_func(a1)
  len_z1=len(z1)
  b=np.ones((len_z1,1))
  z1=np.concatenate((b,z1),axis=1)
  a2=np.matmul(z1,wt2)
  z2=sigmoid_func(a2)
  return a1,z1,a2,z2

def back_propagation(a2, z0, z1, z2, y):
  diff2 = z2-y
  Derivative2=np.matmul(z1.T,diff2)
  diff1=(diff2.dot(w2[1:,:].T ))*sigmoid_func(a1)*(1-sigmoid_func(a1))
  Derivative1=np.matmul(z0.T,diff1)
  return diff2,Derivative1, Derivative2

def updateWeights(Derivative1,Derivative2,learning_rate,m,w1,w2):
  change_in_w1= learning_rate*(1/m)*Derivative1
  w1=w1 - change_in_w1
  change_in_w2= learning_rate*(1/m)*Derivative2
  w2= w2 - change_in_w2
  return w1,w2

def predict(X_test,weight1,weight2):
  a1,z1,a2,z2=fwd_pass(X_test,weight1,weight2)
  return z2

def test(X_test,y_test): 
  y_predicted=predict(X_test,w1,w2)
  print("Test set is :")
  print(X_test[:,1:])
  print("\nPredicted values for Test set are")
  print(np.round(y_predicted))
  print("\n And actual y values for test set are")
  print(y_test)
w1=np.random.randn(3,5)
w2=np.random.randn(6,1)
learning_rate=0.05
costs=[]
num_epoch=10000
m=len(X)
for i in range(num_epoch):
  a1,z1,a2,z2=fwd_pass(X,w1,w2)
  diff2, Derivative1,Derivative2 = back_propagation(a2,X,z1,z2,y)
  w1,w2= updateWeights(Derivative1,Derivative2,learning_rate,m,w1,w2)
  cost_i=np.mean(np.abs(diff2))
  costs.append(cost_i)
  if i == 0 or i==num_epoch-1:
    print("In Iteration: "+ str(i+1))
    print("the error is : "+ str(cost_i) + "\n")
w1
w2<jupyter_output><empty_output><jupyter_text><jupyter_code>updateWeights(Derivative1,Derivative2,learning_rate,m,w1,w2)
fwd_pass(X,w1,w2)
back_propagation(a2,X,z1,z2,y)
predict(X,w1,w2)
print("After the completion of Training :\n")
z3=predict(X,w1,w2)
print("Y value predicted: ")
print(np.round(z3))
print("\n")
plt.plot(costs)
plt.ylabel('Error')
plt.xlabel('Epochs')
plt.show()
X_test = np.array([[1,0,0.03],
 [1,0,0.99],
 [1,1,0 ],
 [1,1,0.2]])
y_test = np.array([[1.],
 [0.],
 [0.],
 [0.]])

test(X_test,y_test)<jupyter_output>Test set is :
[[0.   0.03]
 [0.   0.99]
 [1.   0.  ]
 [1.   0.2 ]]

Predicted values for Test set are
[[0.]
 [0.]
 [0.]
 [0.]]

 And actual y values for test set are
[[1.]
 [0.]
 [0.]
 [0.]]
{'fraction_non_alphanumeric': 0.18040023543260741, 'fraction_numerical': 0.06650971159505592, 'mean_word_length': 4.860344827586207, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '558202', 'license': 'Apache-2.0', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1603, 'n_words': 290, 'path': '58026_PrExam_BrenanAndFriends/58026_PrExam_BrenanAndFriends.ipynb', 'repo_name': 'jamesmattluyon/CPE-AIDL', 'size': '3398'}