-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAI.py
63 lines (51 loc) · 2.03 KB
/
AI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
import random
class AI():
def __init__(self,noofInputs=5,outputs=3):
self.noofInputs=5
self.createNeuralNet()
def createNeuralNet(self,hiddenLayers=[4]):
"""Create Neural Net-#of Inputs=# of distances and 3 outputs matching ARROW_LEFT ARROW_UP ARROW_RIGHT"""
model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(5,),bias=False ))
for i in hiddenLayers:
model.add(Dense(i, activation='relu',bias=False))
model.add(Dense(3, activation='sigmoid',bias=False))
model.compile(optimizer='adam', loss='categorical_crossentropy')
self.model=model
def predict(self,distances):
#print(distances)
return self.model.predict(np.expand_dims(distances,axis=0))
def getWeightsVector(self):
weights=np.array(self.model.get_weights() )
#print(weights)
self.weightsShapes=[]
self.weightsVector=[]
for i in weights:
self.weightsShapes.append(i.shape)
vec=i.flatten()
for j in vec:
self.weightsVector.append(j)
return self.weightsVector
def setWeightsFromVector(self):
#noflayers=len(self.weightsShape)# # oflayers
weights=[]
start=0
for shape in self.weightsShapes:
noofWeigths=shape[0]*shape[1]
data=np.array( self.weightsVector[start:start+noofWeigths] )
weights.append( data.reshape(shape) )
start+=noofWeigths
#print(self.weightsVector)
#print("================================NEW WEIGTHS================================")
#for i in weights:
# print("================================================================")
# print(i)
self.model.set_weights(weights)
if __name__ == "__main__":
x=AI()
x.createNeuralNet()
distances=list(range(5))
x.predict(disatnces)