Made code pep8 compliant

pull/2/merge^2
Alve 4 years ago
parent 2925776052
commit 118b13c971
  1. 55
      rgbAI/lib/func.py
  2. 9
      rgbAI/main.py

@ -1,14 +1,17 @@
import numpy as np import numpy as np
from copy import deepcopy as copy from copy import deepcopy as copy
class AIlib: class AIlib:
def sigmoid(x): def sigmoid(x):
return 1/(1 + np.exp(-x)) return 1/(1 + np.exp(-x))
def correctFunc(inp: np.array): # generates the correct answer for the AI def correctFunc(inp: np.array): # generates the correct answer for the AI
return np.asarray( [1.0 - inp[0], 1.0 - inp[1], 1.0 - inp[2]] ) # basically invert the rgb values # basically invert the rgb values
return np.asarray([1.0 - inp[0], 1.0 - inp[1], 1.0 - inp[2]])
def calcCost( predicted:np.array, correct:np.array ): # cost function, lower -> good, higher -> bad, bad bot, bad # cost function, lower -> good, higher -> bad, bad bot, bad
def calcCost(predicted: np.array, correct: np.array):
costSum = 0 costSum = 0
maxLen = len(correct) maxLen = len(correct)
@ -21,15 +24,18 @@ class AIlib:
corr = AIlib.correctFunc(inp) corr = AIlib.correctFunc(inp)
return AIlib.calcCost(predicted, corr) return AIlib.calcCost(predicted, corr)
def genRandomMatrix( x:int, y:int, min: float=0.0, max: float=1.0 ): # generate a matrix with x, y dimensions with random values from min-max in it # generate a matrix with x, y dimensions with random values from min-max in it
def genRandomMatrix(x: int, y: int, min: float = 0.0, max: float = 1.0):
# apply ranger with * and - # apply ranger with * and -
mat = np.random.rand(x, y) - 0.25 mat = np.random.rand(x, y) - 0.25
return mat return mat
def think(inp: np.array, obj, layerIndex: int = 0): # recursive thinking, hehe def think(inp: np.array, obj, layerIndex: int = 0): # recursive thinking, hehe
maxLayer = len(obj.weights) - 1 maxLayer = len(obj.weights) - 1
weightedLayer = np.dot( inp, obj.weights[layerIndex] ) # dot multiply the input and the weights # dot multiply the input and the weights
layer = AIlib.sigmoid( np.add(weightedLayer, obj.bias[layerIndex]) ) # add the biases weightedLayer = np.dot(inp, obj.weights[layerIndex])
layer = AIlib.sigmoid(
np.add(weightedLayer, obj.bias[layerIndex])) # add the biases
if(layerIndex < maxLayer): if(layerIndex < maxLayer):
return AIlib.think(layer, obj, layerIndex + 1) return AIlib.think(layer, obj, layerIndex + 1)
@ -57,15 +63,18 @@ class AIlib:
# Create new a instance of the object # Create new a instance of the object
obj2 = copy(obj) # annoying way to create a new instance of the object obj2 = copy(obj) # annoying way to create a new instance of the object
obj2.weights[layerIndex][neuronIndex_X][neuronIndex_Y] += theta # mutate the second objects neuron # mutate the second objects neuron
dCost, curCost = AIlib.compareAIobjects( inp, obj, obj2 ) # compare the two and get the dCost with respect to the weights obj2.weights[layerIndex][neuronIndex_X][neuronIndex_Y] += theta
# compare the two and get the dCost with respect to the weights
dCost, curCost = AIlib.compareAIobjects(inp, obj, obj2)
return dCost, curCost return dCost, curCost
def compareInstanceBias(obj, inp, theta: float, layerIndex: int, biasIndex: int): def compareInstanceBias(obj, inp, theta: float, layerIndex: int, biasIndex: int):
obj2 = copy(obj) obj2 = copy(obj)
obj2.bias[layerIndex][0][biasIndex] += theta # do the same thing for the bias # do the same thing for the bias
obj2.bias[layerIndex][0][biasIndex] += theta
dCost, curCost = AIlib.compareAIobjects(inp, obj, obj2) dCost, curCost = AIlib.compareAIobjects(inp, obj, obj2)
return dCost, curCost return dCost, curCost
@ -74,7 +83,8 @@ class AIlib:
mirrorObj = copy(obj) mirrorObj = copy(obj)
# Fill the buffer with None so that the dCost can replace it later # Fill the buffer with None so that the dCost can replace it later
dCost_W = np.zeros( shape = mirrorObj.weights[layerIndex].shape ) # fill it with a placeholder # fill it with a placeholder
dCost_W = np.zeros(shape=mirrorObj.weights[layerIndex].shape)
dCost_B = np.zeros(shape=mirrorObj.bias[layerIndex].shape) dCost_B = np.zeros(shape=mirrorObj.bias[layerIndex].shape)
# Get the cost change for the weights # Get the cost change for the weights
@ -83,23 +93,25 @@ class AIlib:
for x in range(weightLenX): # get the dCost for each x,y for x in range(weightLenX): # get the dCost for each x,y
for y in range(weightLenY): for y in range(weightLenY):
dCost_W[x][y], curCostWeight = AIlib.compareInstanceWeight( obj, inp, theta, layerIndex, x, y ) dCost_W[x][y], curCostWeight = AIlib.compareInstanceWeight(
obj, inp, theta, layerIndex, x, y)
# Get the cost change for the biases # Get the cost change for the biases
biasLenY = len(dCost_B[0]) biasLenY = len(dCost_B[0])
for index in range(biasLenY): for index in range(biasLenY):
dCost_B[0][index], curCostBias = AIlib.compareInstanceBias( obj, inp, theta, layerIndex, index ) dCost_B[0][index], curCostBias = AIlib.compareInstanceBias(
obj, inp, theta, layerIndex, index)
return dCost_W, dCost_B, (curCostBias + curCostWeight)/2 return dCost_W, dCost_B, (curCostBias + curCostWeight)/2
# Calculate the gradient for that prop
def gradient(inp: np.array, obj, theta: float, maxLayer: int, layerIndex: int = 0, grads=None, obj1=None, obj2=None):
def gradient( inp:np.array, obj, theta:float, maxLayer:int, layerIndex: int=0, grads=None, obj1=None, obj2=None ): # Calculate the gradient for that prop
# Check if grads exists, if not create the buffer # Check if grads exists, if not create the buffer
if(not grads): if(not grads):
grads = [None] * (maxLayer+1) grads = [None] * (maxLayer+1)
dCost_W, dCost_B, meanCurCost = AIlib.getChangeInCost( obj, inp, theta, layerIndex ) dCost_W, dCost_B, meanCurCost = AIlib.getChangeInCost(
obj, inp, theta, layerIndex)
# Calculate the gradient for the layer # Calculate the gradient for the layer
weightDer = AIlib.propDer(dCost_W, theta) weightDer = AIlib.propDer(dCost_W, theta)
@ -120,7 +132,8 @@ class AIlib:
def mutateProps(inpObj, maxLen: int, gradient: list): def mutateProps(inpObj, maxLen: int, gradient: list):
obj = copy(inpObj) obj = copy(inpObj)
for i in range(maxLen): for i in range(maxLen):
obj.weights[i] -= obj.learningrate * gradient[i]["weight"] # mutate the weights obj.weights[i] -= obj.learningrate * \
gradient[i]["weight"] # mutate the weights
obj.bias[i] -= obj.learningrate * gradient[i]["bias"] obj.bias[i] -= obj.learningrate * gradient[i]["bias"]
return obj return obj
@ -133,16 +146,18 @@ class AIlib:
# i.e. : W' = W - lr * gradient (respect to W in layer i) = W - lr*[ dC / dW[i] ... ] # i.e. : W' = W - lr * gradient (respect to W in layer i) = W - lr*[ dC / dW[i] ... ]
# So if we change all the weights with i.e. 0.01 = theta, then we can derive the gradient with math and stuff # So if we change all the weights with i.e. 0.01 = theta, then we can derive the gradient with math and stuff
inp = np.asarray(np.random.rand( 1, inputNum ))[0] # create a random learning sample inp = np.asarray(np.random.rand(1, inputNum))[
0] # create a random learning sample
while( not curCost or curCost > targetCost ): # targetCost is the target for the cost function # targetCost is the target for the cost function
while(not curCost or curCost > targetCost):
maxLen = len(obj.bias) maxLen = len(obj.bias)
grads, curCost = AIlib.gradient(inp, obj, theta, maxLen - 1) grads, curCost = AIlib.gradient(inp, obj, theta, maxLen - 1)
obj = AIlib.mutateProps( obj, maxLen, grads ) # mutate the props for next round # mutate the props for next round
obj = AIlib.mutateProps(obj, maxLen, grads)
print(f"Cost: {curCost}") print(f"Cost: {curCost}")
print("DONE\n") print("DONE\n")
print(obj.weights) print(obj.weights)
print(obj.bias) print(obj.bias)

@ -2,16 +2,19 @@
import numpy as np import numpy as np
from lib.func import AIlib as ai from lib.func import AIlib as ai
class rgb(object): class rgb(object):
def __init__(self, loadedWeights: np.matrix = None, loadedBias: np.matrix = None): def __init__(self, loadedWeights: np.matrix = None, loadedBias: np.matrix = None):
if(not loadedWeights or not loadedBias): # if one is null (None) then just generate new ones if(not loadedWeights or not loadedBias): # if one is null (None) then just generate new ones
print("Generating weights and biases...") print("Generating weights and biases...")
self.weights = [ ai.genRandomMatrix(3, 8), ai.genRandomMatrix(8, 8), ai.genRandomMatrix(8, 3) ] # array of matrices of weights self.weights = [ai.genRandomMatrix(3, 8), ai.genRandomMatrix(
8, 8), ai.genRandomMatrix(8, 3)] # array of matrices of weights
# 3 input neurons -> 8 hidden neurons -> 8 hidden neurons -> 3 output neurons # 3 input neurons -> 8 hidden neurons -> 8 hidden neurons -> 3 output neurons
# Generate the biases # Generate the biases
self.bias = [ ai.genRandomMatrix(1, 8), ai.genRandomMatrix(1, 8), ai.genRandomMatrix(1, 3) ] self.bias = [ai.genRandomMatrix(1, 8), ai.genRandomMatrix(
1, 8), ai.genRandomMatrix(1, 3)]
# This doesn't look very good, but it works so... # This doesn't look very good, but it works so...
self.learningrate = 0.01 # the learning rate of this ai self.learningrate = 0.01 # the learning rate of this ai
@ -41,6 +44,7 @@ class rgb(object):
print(res) print(res)
return res return res
def init(): def init():
bot = rgb() bot = rgb()
bot.learn() bot.learn()
@ -50,4 +54,5 @@ def init():
err = bot.calcError(inpArr, res) err = bot.calcError(inpArr, res)
print(err) print(err)
init() init()

Loading…
Cancel
Save