<jupyter_start><jupyter_text>we'll be implementing xor using 2 layer neural network<jupyter_code>#importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x=np.array([[0,0],[0,1],[1,0],[1,1]])
x=x.T
y=np.array([0,1,1,0])
c=['green' if l==1 else 'red' for l in y]
plt.scatter(x[0],x[1],color=c)
plt.axis('equal')
plt.show()
def cost(a,y):
    j=(-1/len(y))*np.sum(y*np.log(a)+(1-y)*np.log(1-a))
    return j
def sigmoid(x):
    a=1/(1+np.exp(-1*x))
    return a
np.random.seed(1)
w1=np.random.rand(2,2)*2-1
w2=np.random.rand(1,2)*2-1
b1=np.random.rand(2,1)*2-1
b2=np.random.rand(1,1)*2-1
w1
j_history=np.zeros((1000000,1))
alpha=0.01
for i in range(1000000):
    #forward propagation
    z1=np.dot(w1,x)+b1
    a1=np.tanh(z1)
    z2=np.dot(w2,a1)+b2
    a2=sigmoid(z2)
    
    #computing cost
    j=cost(a2,y)
    j_history[i]=j
    
    #backward propagation
    dz2=a2-y
    dw2=(1/len(y))*np.dot(dz2,a1.T)
    db2=(1/len(y))*np.sum(dz2, axis=1, keepdims=True)
    dz1=np.dot(w2.T,dz2)*(1-a1*a1)
    dw1=(1/len(y))*np.dot(dz1,x.T)
    db1=(1/len(y))*np.sum(dz1, axis=1, keepdims=True)
    
    #gradient descent
    w1=w1-alpha*dw1
    b1=b1-alpha*db1
    w2=w2-alpha*dw2
    b2=b2-alpha*db2
xa=np.arange(1,1000001,1)
plt.plot(xa,j_history)
plt.show()
b1
x_t=[[0],[0]]
z1=np.dot(w1,x_t)+b1
a1=np.tanh(z1)
z2=np.dot(w2,a1)+b2
a2=sigmoid(z2)
a2
j_history[-1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21870604781997188, 'fraction_numerical': 0.09423347398030943, 'mean_word_length': 4.270370370370371, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '555192', 'license': 'MIT', 'n_tokens_mistral': 784, 'n_tokens_neox': 749, 'n_words': 98, 'path': 'xor using neural network.ipynb', 'repo_name': 'the-oathbreaker/Shallow-Neural-Networks', 'size': '1422'}