Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- class Work :
- """ X - is a matrix of pattern, when x_ is the string,
- that we want to compare with all our pattern
- in our program X.size(0) = 3
- X[i][j] in (0, 1)
- W - is a matrix of weigth
- net - is a helper string for y
- fnet - the result of f(net)
- y - is a string in previous iteration
- We calculate y while it is not one of patterns
- in the begining y = x_"""
- def __init__(self, X, x_):
- self.X = X
- self.x_ = x_
- self.y = x_
- self.fnet = x_
- self.W = self.init_W()
- self.k = 0
- def init_W(self):
- W = np.zeros((self.x_.shape[0],self.x_.shape[0]))
- for i in range (0, self.X.shape[0]):
- for j in range (0,self.X.shape[1]):
- for k in range (0,self.X.shape[1]):
- if(k!=j):
- W[j][k]+=X[i][j]*X[i][k]
- return W
- def next_net(self):
- net = np.zeros((self.y.shape[0]))
- for j in range(0, self.y.shape[0]):
- for k in range(0, self.y.shape[0]):
- if (k!=j):
- net[k]+=self.W[j][k]*self.y[j]
- return net
- def f(self, net, fnet_old):
- fnet = np.zeros((net.shape[0]))
- for i in range(0, net.shape[0]):
- if (net[i] >0):
- fnet[i] = 1
- elif (net[i] == 0):
- fnet[i] = fnet_old[i]
- else:
- fnet[i] = -1
- return fnet
- def solution(self):
- self.k = 0
- while ((not(np.array_equal(self.y, self.X[0]))) and (not(np.array_equal(self.y, self.X[1])))
- and (not(np.array_equal(self.y, self.X[2])))):
- self.k+=1
- net = self.next_net()
- fnet = self.f(net, self.fnet)
- self.fnet = fnet
- self.y = self.fnet
- return self.y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement