Learning that doesn't work

pull/1/head
E. Almqvist 4 years ago
parent 86727591df
commit 544f6e454c
  1. 32
      rgbAI/lib/func.py
  2. 19
      rgbAI/main.py

@ -54,29 +54,41 @@ class AIlib:
def gradient( dCost:float, prop:list ):
propLen = len(prop)
gradient = [None] * propLen
for i in range( propLen, -1, -1 ):
if( i == propLen ):
for i in range( propLen - 1, -1, -1 ):
# if( i == propLen - 1 ):
# gradient[i] = dCost / prop[i]
# else:
# gradient[i] = dCost / (prop[i] + gradient[i+1])
gradient[i] = dCost / prop[i]
else:
gradient[i] = dCost / (prop[i] + gradient[i+1])
return gradient
def learn( inp:np.array, weights:list, bias:list, theta:float ):
def mutateProp( prop:list, gradient:list ):
newProp = [None] * len(gradient)
for i in range(len(gradient)):
newProp[i] = prop[i] - gradient[i] # * theta (relative to slope or something)
return newProp
def learn( inp:np.array, obj, theta:float ):
# Calculate the derivative for:
# Cost in respect to weights
# Cost in respect to biases
res1 = AIlib.think( inp, weights, bias ) # Think the first result
res1 = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result
cost1 = AIlib.calcCost( inp, res1 ) # Calculate the cost of the thought result
inp2 = np.asarray( inp + theta ) # make the new input with `theta` as diff
res2 = AIlib.think( inp2, weights, bias ) # Think the second result
res2 = AIlib.think( inp2, obj.weights, obj.bias ) # Think the second result
cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost
dCost = cost2 - cost1 # get the difference
weightDer = AIlib.gradient( dCost, weights )
biasDer = AIlib.gradient( dCost, bias )
weightDer = AIlib.gradient( dCost, obj.weights )
biasDer = AIlib.gradient( dCost, obj.bias )
obj.weights = AIlib.mutateProp( obj.weights, weightDer )
obj.bias = AIlib.mutateProp( obj.bias, biasDer )
print(weights, len(weights))
print("Cost: ", cost1)

@ -16,6 +16,9 @@ class rgb(object):
self.generation = 0
print( self.weights )
print( self.bias )
else: # if we want to load our progress from before then this would do it
self.weights = loadedWeights
self.bias = loadedBias
@ -26,9 +29,9 @@ class rgb(object):
return cost
def learn( self, inp:np.array, theta:float ):
ai.learn( inp, self.weights, self.bias, theta )
ai.learn( inp, self, theta )
def think(self, inp:np.array):
def think( self, inp:np.array ):
print("-----Gen " + str(self.generation) + "------")
print("\n-Input-")
print(inp)
@ -41,10 +44,18 @@ class rgb(object):
print("\n----------------\n\n")
return res
def train( self ):
for i in range(self.traintimes):
inpArr = np.asarray(np.random.rand( 1, 3 ))[0]
print("##################", inpArr)
self.generation = i
self.learn( inpArr, 0.1 )
def init(): # init
bot = rgb()
inpArr = np.array( [0.2, 0.4, 0.8] )
bot.learn( inpArr, 0.1 )
bot.traintimes = 100
bot.train()
init()

Loading…
Cancel
Save