Answers for "code for uni layer perceptron neural network"

0

code for uni layer perceptron neural network

#%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np

data = [[3,   1.5, 1],
        [2,   1,   0],
        [4,   1.5, 1],
        [3,   1,   0],
        [3.5, .5,  1],
        [2,   .5,  0],
        [5.5,  1,  1],
        [1,    1,  0]]
mysteryFlower = [4.5,1]
for i in data:
    col = 'r'
    if i[2] == 0:
        col = 'b'
    plt.scatter(i[0],i[1],c=col)
    
w1 = np.random.randn()
w2 = np.random.randn()
b = np.random.randn()
def sigmoid(x):
    return 1/(1+np.exp(-x))
#training loop
costs = []
for i in range(500000):
    rand_ind = np.random.randint(len(data))
    dataPoint = data[rand_ind]
    lrate = 1
    global w1
    global w2
    global b
    pred_prim = w1*dataPoint[0]+w2*dataPoint[1]+b
    pred = sigmoid(pred_prim)
    cost = (pred-dataPoint[2])**2
    costs.append(cost)
    #derivatives
    dcost_dpred = 2*(pred-dataPoint[2])
    dpred_dpred_prim = sigmoid(pred_prim)-sigmoid(pred_prim)**2
    dpred_prim_dw1 = dataPoint[0]
    dpred_prim_dw2 = dataPoint[1]
    dpred_prim_db = 1
    w1-=lrate*dcost_dpred*dpred_dpred_prim*dpred_prim_dw1
    w2-=lrate*dcost_dpred*dpred_dpred_prim*dpred_prim_dw2
    b -= lrate*dcost_dpred*dpred_dpred_prim*dpred_prim_db
print(w1,w2,b)
print(sigmoid(w1*mysteryFlower[0]+w2*mysteryFlower[1]+b))
plt.plot(costs)
Posted by: Guest on September-15-2020

Code answers related to "code for uni layer perceptron neural network"

Python Answers by Framework

Browse Popular Code Answers by Language