# -*- coding: utf-8 -*-
"""
Created on Thu Mar 8 16:23:22 2018
@author: maeiei
"""
import numpy as np
def sigmoid (x , deriv = False):
if(deriv == True):
return x * (1 - x)
return 1 / (1 + np.exp(-x))
x = np.array([[1, 1, 1,
1, 0, 1,
1, 0, 1,
1, 0, 1,
1, 1, 1], # 0
[1, 1, 1,
0, 1, 0,
0, 1, 0,
0, 1, 0,
1, 1, 1], # 1
[1, 1, 1,
0, 0, 1,
1, 1, 1,
1, 0, 0,
1, 1, 1], # 2
[1, 1, 1,
0, 0, 1,
1, 1, 1,
0, 0, 1,
1, 1, 1], # 3
[1, 0, 1,
1, 0, 1,
1, 1, 1,
0, 0, 1,
0, 0, 1], # 4
[1, 1, 1,
1, 0, 0,
1, 1, 1,
0, 0, 1,
1, 1, 1], # 5
[1, 1, 1,
1, 0, 0,
1, 1, 1,
1, 0, 1,
1, 1, 1], # 6
[1, 1, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1], # 7
[1, 1, 1,
1, 0, 1,
1, 1, 1,
1, 0, 1,
1, 1, 1], # 8
[1, 1, 1,
1, 0, 1,
1, 1, 1,
0, 0, 1,
1, 1, 1] # 9
])
x = x.T
y = np.array([[0, 0, 0, 0],[0, 0, 0, 1],[0, 0, 1, 0],[0, 0, 1, 1],[0, 1, 0, 0],
[0, 1, 0, 1],[0, 1, 1, 0],[0, 1, 1, 1],[1, 0, 0, 0],[1, 0, 0, 1]])
y = y.T
np.random.seed(1)
w0 = 2*np.random.random((4, 15)) -1
w1 = 2*np.random.random((4, 4))-1
print(w0)
print(w1)
for j in range(100000):
l0 = x
l1 = sigmoid(np.dot(w0, l0))
l2 = sigmoid(np.dot(w1, l1))
l2_error = y - l2
if(j % 10000) == 0:
print('Error' + str(np.mean(np.abs(l2_error))))
l2_delta = l2_error * sigmoid(l2, deriv = True)
l1_error = w1.dot(l2_delta)
l1_delta = l1_error * sigmoid(l1, deriv = True)
w1 += l2_delta.dot(l1.T)
w0 += l1_delta.dot(l0.T)
for i in range(10):
l0 = x[i]
l1 = sigmoid(np.dot(l0, w0))
l2 = sigmoid(np.dot(l1, w1))
print(l2)
d = np.array([1, 1, 1,
1, 0, 1,
1, 0, 1,
1, 0, 1,
1, 1, 1])
l0 = d
l1 = sigmoid(np.dot(w0, l0))
l2 = sigmoid(np.dot(w1, l1))
print(l2)
d = np.array( [1, 1, 1,
0, 0, 1,
1, 1, 1,
1, 0, 0,
1, 1, 1])
l0 = d
l1 = sigmoid(np.dot(w0, l0))
l2 = sigmoid(np.dot(w1, l1))
print(l2)
BP神经网络
猜你喜欢
转载自maeiei.iteye.com/blog/2412572
今日推荐
周排行