You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.1 KiB
53 lines
2.1 KiB
4 years ago
|
import numpy as np
|
||
|
|
||
4 years ago
|
class AIlib:
|
||
|
def sigmoid(x):
|
||
|
return 1/(1 + np.exp(-x))
|
||
4 years ago
|
|
||
4 years ago
|
def correctFunc(inp:np.array): # generates the correct answer for the AI
|
||
|
return np.array( rgb[2], rgb[1], rgb[0] ) # basically invert the rgb values
|
||
4 years ago
|
|
||
4 years ago
|
def calcCost( inp:np.array, out:np.array ): # cost function, lower -> good, higher -> bad, bad bot, bad
|
||
|
sumC = 0
|
||
|
outLen = len(out)
|
||
|
|
||
|
correctOut = correctFunc(inp) # the "correct" output
|
||
|
|
||
|
for i in range(outLen):
|
||
|
sumC += (out[i] - correctOut[i])**2 # get the difference of every value
|
||
|
|
||
|
return sumC / outLen # return the average cost of all rows
|
||
|
|
||
|
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
|
||
4 years ago
|
return np.random.rand(x, y)
|
||
4 years ago
|
|
||
4 years ago
|
def think( inp:np.array, weights:list, bias:list, layerIndex: int=0 ): # recursive thinking, hehe
|
||
4 years ago
|
# the length of weights and bias should be the same
|
||
|
# if not then the neural net is flawed/incorrect
|
||
4 years ago
|
maxLayer = len(weights) - 1
|
||
|
biasLen = len(bias) - 1
|
||
|
if( maxLayer != biasLen ):
|
||
4 years ago
|
print("Neural Network Error: Length of weights and bias are not equal.")
|
||
4 years ago
|
print( "Weights: " + str(maxLayer) + " Bias: " + str(biasLen) )
|
||
4 years ago
|
exit()
|
||
4 years ago
|
|
||
4 years ago
|
try:
|
||
4 years ago
|
weightedInput = np.dot( inp, weights[layerIndex] ) # dot multiply the input and the weights
|
||
4 years ago
|
layer = AIlib.sigmoid( np.add(weightedInput, bias[layerIndex]) ) # add the biases
|
||
4 years ago
|
|
||
4 years ago
|
if( layerIndex < maxLayer ):
|
||
|
print("Layer " + str(layerIndex))
|
||
|
print(layer)
|
||
|
print("\n")
|
||
4 years ago
|
|
||
|
if( layerIndex < maxLayer ):
|
||
|
return AIlib.think( layer, weights, bias, layerIndex + 1 )
|
||
4 years ago
|
else:
|
||
4 years ago
|
return layer
|
||
|
|
||
|
except (ValueError, IndexError) as err:
|
||
4 years ago
|
print("\n---------")
|
||
|
print( "Error: " + str(err) )
|
||
|
print( "Layer index: " + str(layerIndex) )
|
||
4 years ago
|
print( "Max layer index: " + str(maxLayer) )
|