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)

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source