Demo entry 6653189

123

   

Submitted by anonymous on Oct 24, 2017 at 17:44
Language: Python. Code size: 1.2 kB.

import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
    return 1.0/(1.0+np.exp(-x))
    
def train_func(train_x,train_y,iter_n,learn_rate):
    weight_len=len(train_x[0])
    num = float(len(train_x))

    learn_rate=learn_rate
    weight=np.zeros((weight_len,1))
    b=0.0
    iter_n=iter_n
    #iter_n=6
    for i in range(iter_n):
   
        error=train_y-sigmoid(train_x.dot(weight)+b)

        gradient_b=sum(error)
        gradient_w=train_x.T.dot(error)    

        weight=weight+learn_rate*gradient_w
        b=b+learn_rate*gradient_b
        
    
    return weight,b





if __name__=="__main__":
    train_x=np.array([[1,1],[1,2],[2,2],[1,0],
                     [4,5],[5,6],[6,7],[6,6]])
    train_y=np.array([[1],[1],[1],[1],[0],[0],[0],[0]])
    iter_n=1000
    learn_rate=1
    weight,b=train_func(train_x,train_y,iter_n,learn_rate)
    print weight
    print b
    
    x=train_x[0:len(train_x),0]
    y=train_x[0:len(train_x),1]
    print x
    print y
    plt.plot(x,y,"ro")
    x1=np.linspace(0,7,400)
    y1=-weight[0]/weight[1]*x1-b/weight[1]
    plt.plot(x1,y1)
    plt.xlabel("x1")
    plt.ylabel("x2")
    plt.xlim(xmin=0,xmax=7)
    plt.ylim(ymax=8)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).