import numpy as np import random import math import matplotlib.pyplot as plt from functools import reduce class Perceptron(object): w = np.array([[1],[1]]) x = np.array([[1, 1], [0, 0], [1, 0], [0, 1]]) t = [1,0,0,0] x1 = np.array(x[:,0]) x2 = np.array(x[:,1]) e0 = 9 e1 = 9 eps = 1e-4 abc = 0 def __init__(self): self.eps = 1e-4 self.a = 0.01 def jia(x, y): a = list(map(lambda x: x*y,x)) c = np.sum(a) return c def f(x): return 1.0 / (1 + np.exp(-x)) for abc in range(1000): i=0 w1 = w[0] w2 = w[1] for i in range(0,4): i = random.randint(0, 3) e0 = np.sum(2*(f(jia(w1,x1[i])-t[i])*(1-f(jia(w1,x1)))*x1)) e1 = np.sum(2*(f(jia(w2,x2[i])-t[i])*(1-f(jia(w2,x2)))*x2)) w1 = w1-0.1*e0 w2 = w2-0.1*e1 abc=abc+1 if __name__ == '__main__': print(w1,w2 )