Created
July 18, 2014 07:21
-
-
Save kennyballou/65b8195c5f249108a3bb to your computer and use it in GitHub Desktop.
(bad) Pythonic translation of a Java hopfield neural network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Hopfield Neural Network Simulation | |
# Copyright (C) 2014 Kenny Ballou | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
import math | |
NETWORK_SIZE = 4 | |
def createEmptyList(): | |
return list(list(0 for i in range(4)) for i in range(4)) | |
def convertToBipolar(bi): | |
for i in bi: | |
yield -1 if i == 0 else 1 | |
class Neuron(object): | |
def __init__(self, w): | |
self.activation = -1 | |
self.w = w | |
def act(self, pattern): | |
a = 0 | |
for i in range(4): | |
if pattern[i]: | |
a += self.w[i] | |
return a | |
class Layer(object): | |
def __init__(self, wt): | |
self.neurons = [] | |
self.wt = wt | |
for w in self.wt: | |
self.neurons.append(Neuron(w)) | |
self.output = list(False for x in range(4)) | |
def activation(self, pattern): | |
for i in range(4): | |
self.neurons[i].activation = self.neurons[i].act(pattern) | |
self.output[i] = self.threshold(self.neurons[i].activation) | |
def threshold(self, k): | |
return math.tanh(k) >= 0 | |
class Hopfield(object): | |
def __init__(self): | |
self.__initValues() | |
def __initValues(self): | |
self.wt = createEmptyList() | |
def run(self, pattern): | |
net = Layer(self.wt) | |
net.activation(pattern) | |
print(net.output) | |
def clear(self): | |
self.__initValues() | |
def train(self, bi): | |
work = createEmptyList() | |
self.bi = list(convertToBipolar(bi)) | |
for i in range(4): | |
for j in range(4): | |
work[i][j] = self.bi[i] * self.bi[j] | |
for i in range(4): | |
work[i][i] -= 1 | |
for i in range(4): | |
for j in range(4): | |
self.wt[i][j] += work[i][j] | |
if __name__ == "__main__": | |
hfNet = Hopfield() | |
hfNet.train([0, 1, 0, 1]) | |
print(hfNet.wt) | |
hfNet.run([False, True, False, True]) | |
hfNet.run([True, False, True, False]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment