Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import numpy as np
  2. class Work :
  3.     """ X - is a matrix of pattern, when x_ is the string,
  4.    that we want to compare with all our pattern
  5.    in our program X.size(0) = 3
  6.    X[i][j] in (0, 1)
  7.    W - is a matrix of weigth
  8.    net - is a helper string for y
  9.    fnet - the result of f(net)
  10.    y - is a string in previous iteration
  11.    We calculate y while it is not one of patterns
  12.    in the begining y = x_"""
  13.    
  14.     def __init__(self, X, x_):
  15.         self.X = X
  16.         self.x_ = x_
  17.         self.y = x_
  18.         self.fnet = x_
  19.         self.W = self.init_W()
  20.         self.k = 0
  21.        
  22.     def init_W(self):
  23.         W = np.zeros((self.x_.shape[0],self.x_.shape[0]))
  24.         for i in range (0, self.X.shape[0]):
  25.             for j in range (0,self.X.shape[1]):
  26.                 for k in range (0,self.X.shape[1]):
  27.                     if(k!=j):
  28.                         W[j][k]+=X[i][j]*X[i][k]          
  29.         return W
  30.        
  31.     def next_net(self):
  32.         net = np.zeros((self.y.shape[0]))
  33.         for j in range(0, self.y.shape[0]):
  34.             for k in range(0, self.y.shape[0]):
  35.                 if (k!=j):
  36.                     net[k]+=self.W[j][k]*self.y[j]
  37.         return net
  38.    
  39.     def f(self, net, fnet_old):
  40.         fnet = np.zeros((net.shape[0]))
  41.         for i in range(0, net.shape[0]):
  42.             if (net[i] >0):
  43.                 fnet[i] = 1
  44.             elif (net[i] == 0):
  45.                 fnet[i] = fnet_old[i]
  46.             else:
  47.                 fnet[i] = -1
  48.         return fnet
  49.    
  50.    
  51.    
  52.     def solution(self):
  53.         self.k = 0
  54.         while ((not(np.array_equal(self.y, self.X[0]))) and (not(np.array_equal(self.y, self.X[1])))
  55.                and (not(np.array_equal(self.y, self.X[2])))):
  56.             self.k+=1
  57.             net = self.next_net()
  58.             fnet = self.f(net, self.fnet)
  59.             self.fnet = fnet
  60.             self.y = self.fnet
  61.            
  62.         return self.y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement