Skip to content

Instantly share code, notes, and snippets.

@LizardLeliel
Created June 22, 2015 14:51
Show Gist options
  • Save LizardLeliel/cca1c314c831078c4f07 to your computer and use it in GitHub Desktop.
Save LizardLeliel/cca1c314c831078c4f07 to your computer and use it in GitHub Desktop.
Its a hardcoded neural network that was built so I could have just a neural network, regardless of implementation (so I can build off, reference, change, etc.). By the way its python3
# How nessecary is this?
class Gene:
def __init__(self, inNeuron, outNeuron, weight = 1):
# inNeuron and outNeuron represent array indices
self.inNeuron = inNeuron
self.outNeuron = outNeuron
self.weight = weight
def compareOuts(self, otherGene):
if self.outNeuron < otherGene.outNeuron: return -1
elif self.outNeuron > otherGene.outNeuron: return 1
else: return 0
class Neuron:
# incoming = [] is an array of --genes-- incoming indexes.
# Do we need paramters?
def __init__(self):
self.incoming = []
self.value = 0
#def setBias
def bisigmoid(x):
return 2/(1 + 2.71828**(-x)) - 1
class Network:
def __init__(self, inputs, hiddenNeurons, outputAmmount, genome=[]):
self.genome = genome # Array of genes
biasNeuron = Neuron()
biasNeuron.value = 1
biasNeuron.incoming = None
self.network = [biasNeuron]
self.inputs = inputs
self.hiddens = hiddenNeurons
self.outputs = outputAmmount
self.hiddenStart = 1+inputs
self.outputStart = hiddenNeurons + self.hiddenStart
self.total = 1 + inputs + hiddenNeurons + outputAmmount
self.built = False
for i in range(0, inputs+hiddenNeurons+outputAmmount):
self.network.append(Neuron())
#def addGene(self, gene):
# self.genome.append(gene)
# self.built = False
def setInputs(self, inputArray):
if len(inputArray) != self.inputs:
raise Exception("Array of input doesn't match first layer"
+ " of neurons")
for i in range(0, len(inputArray)):
self.network[i+1].value = inputArray[i]
def __getOutput(self):
outputs = []
for i in range(self.outputStart, self.total):
outputs.append(self.network[i].value)
return outputs
def buildNetwork(self):
#sorted(self.genome, key= lambda x: x.outNeuron)
if self.built == True:
return
# Now, let's iterate down the genes...
for gene in genome:
if gene.outNeuron <= gene.inNeuron:
raise Exception("Gene must output to a neuron higher"
+ " then its input")
self.network[gene.outNeuron].incoming.append(gene)
self.built = True
def evaluateNetwork(self, inputs = None):
self.buildNetwork()
if inputs is not None:
self.setInputs(inputs)
# Skip to hidden layer
for i in range(self.hiddenStart, self.total):
neuron = self.network[i]
summ = 0
for gene in neuron.incoming:
incomingNeuron = self.network[gene.inNeuron]
incomingWeight = gene.weight
summ += incomingNeuron.value*incomingWeight
#print(str(incomingNeuron.value) + "*" + str(incomingWeight)
# + " = " + str(incomingNeuron.value*incomingWeight))
#print("Current sum: " + str(summ))
neuron.value = bisigmoid(summ)
#print("Neuron value: " + str(neuron.value))
# And then extract the outputs using getOutput()
#print("Network output is " + str(self.__getOutput()))
return self.__getOutput()
if __name__ == "__main__":
#a = Gene(0,0)
#b = Neuron()
# 0 = bias
# 1 = input1
# 2 = input2
# 3 = hidden1
# 4 = hidden2
# 5 = output
genome = [
Gene(1,3,1.0),
Gene(1,4,1.0),
Gene(2,3,1.0),
Gene(2,4,1.0),
Gene(0,4,-1.0),
Gene(3,5,3.0),
Gene(4,5,-2.5),
Gene(0,5,-1.2)
]
xorBrain = Network(2,2,1, genome)
print("XOR [1, 1] is " + str(xorBrain.evaluateNetwork([1.0, 1.0])[0] > 0.0))
print("XOR [1, 0] is " + str(xorBrain.evaluateNetwork([1.0, 0.0])[0] > 0.0))
print("XOR [0, 1] is " + str(xorBrain.evaluateNetwork([0.0, 1.0])[0] > 0.0))
print("XOR [0, 0] is " + str(xorBrain.evaluateNetwork([0.0, 0.0])[0] > 0.0))
print("The results should be False, True, True, False")
#for i in range(0,5):
# print(str(i))
print("Hello, world!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment