Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- x = np.array([
- [
- 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 0,
- 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0,
- ],
- [
- 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 0,
- 0, 1, 0, 1, 0,
- 0, 1, 1, 1, 0,
- 0, 0, 0, 0, 0,
- ],
- [
- 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 0,
- 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0,
- ],
- [
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 0,
- 1, 0, 0, 0, 1,
- 0, 1, 0, 1, 0,
- 0, 0, 1, 0, 0,
- ]
- ])
- y = np.array([
- np.array([[1, 0, 0, 0]]).T,
- np.array([[0, 1, 0, 0]]).T,
- np.array([[0, 0, 1, 0]]).T,
- np.array([[0, 0, 0, 1]]).T
- ])
- def sigmoid(x):
- return 1 / (1 + np.exp(-x))
- def study(tr_stroka):
- sin_ves = 2 * np.random.random((25, 1)) - 1
- for i in range(10000):
- x1 = x
- y1 = sigmoid(np.dot(x1, sin_ves))
- error = tr_stroka - y1
- raspr = np.dot(x1.T, error * y1 * (1 - y1))
- sin_ves += raspr
- return sin_ves
- sin_ves = np.array([
- 2 * np.random.random((25, 1)) - 1,
- 2 * np.random.random((25, 1)) - 1,
- 2 * np.random.random((25, 1)) - 1,
- 2 * np.random.random((25, 1)) - 1
- ])
- for i in range(4):
- sin_ves[i] = study(y[i])
- def check(x0):
- chance = 0
- y0 = -1
- for i in range(4):
- proz = sigmoid(np.dot(x0, sin_ves[i]))
- if proz > chance:
- chance = proz
- y0 = i
- return y0
- new_x = np.array([
- [
- 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 0,
- 0, 0, 1, 1, 0,
- 0, 0, 0, 0, 0,
- ],
- [
- 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 0,
- 0, 1, 0, 1, 0,
- 0, 1, 1, 1, 0,
- 0, 0, 0, 0, 0,
- ],
- [
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 0,
- 1, 1, 0, 0, 1,
- 0, 1, 0, 1, 0,
- 0, 0, 1, 0, 0,
- ],
- [
- 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 1, 0, 1, 1,
- 1, 1, 1, 1, 1,
- 0, 0, 0, 0, 0,
- ]
- ])
- def symbol(a):
- if a == 0:
- return '∆'
- if a == 1:
- return '□'
- if a == 2:
- return '0'
- if a == 3:
- return '◊'
- for i in range(4):
- print(i, 'RESULT=', symbol(check(new_x[i])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement