Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- #prevent numpy expotential notation on print, default False
- np.set_printoptions(suppress=True)
- # HOPEFIELD!
- # HOPEFIELD!
- # HOPEFIELD!
- # HOPEFIELD!
- class Neuron:
- def __init__(self, size):
- self.size = size
- self.bias = 0
- self.spin = 1.0
- self.weights = np.zeros(size)
- def updateSpin(self, spins):
- # print(self.weights)
- for i in range(self.size):
- value = self.weights[i] * spins[i] + self.bias
- oldSpin = self.spin
- self.spin = 1.0 if value >= 0.0 else -1.0
- return self.spin != oldSpin
- class Network:
- def __init__(self, size):
- self.stableCount = 10000
- self.neurons = []
- # dla obrazka 50x50 size wynosi 2500
- self.size = size
- for i in range(self.size):
- self.neurons.append(Neuron(self.size))
- def learn(self, examples):
- example = []
- result = np.zeros(self.size)
- for e in examples:
- tmp = []
- for i in range(len(e)):
- # tmp.append(np.multiply(e, e[i]))
- tmp.append(e[i]*np.array(e))
- result = result + np.array(tmp)
- print(result)
- for i, row in enumerate(result):
- self.neurons[i].weights = row
- def recognize(self, image):
- correctCounter = 0
- for i in range(self.size):
- self.neurons[i].spin = image[i]
- while correctCounter != self.stableCount:
- randomNeuron = np.random.randint(self.size, size=1)[0]
- spins = []
- for neuron in self.neurons:
- spins.append(neuron.spin)
- # print(spins)
- correctCounter += 1.0
- if self.neurons[randomNeuron].updateSpin(spins):
- correctCounter = 0.0
- # return map(lambda x: x.spin == 1.0, self.neurons)
- result = []
- for neuron in self.neurons:
- if neuron.spin == 1.0:
- result.append(1)
- else:
- result.append(0)
- return result
- #
- # examples = [
- # [1, 2, 3, 4],
- # [1, 2, 3, 4],
- # ]
- #
- # net = Network(4)
- # net.learn(examples)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement