forked from shivamshekhar/Chrome-T-Rex-Rush
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.py
More file actions
42 lines (37 loc) · 1.39 KB
/
Copy pathnn.py
File metadata and controls
42 lines (37 loc) · 1.39 KB
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
import numpy as np
import time
import random
def sigmoid(x):
return 1/(1+np.exp(-x))
class net:
def __init__(self):
pass
def __init__(self,input_n,hidden_n,output_n):
self.N_input = input_n
self.N_hidden = hidden_n
self.N_output = output_n
self.weights_in_hidden = np.random.uniform(low=-1, high=1, size=(self.N_input,self.N_hidden))
self.weights_hidden_out = np.random.uniform(low=-1, high=1, size=(self.N_hidden,self.N_output))
def feed(self,X):
hidden_layer_in = np.dot(X, self.weights_in_hidden)
hidden_layer_out = sigmoid(hidden_layer_in)
output_layer_in = np.dot(hidden_layer_out, self.weights_hidden_out)
o = sigmoid(output_layer_in)
if o[0] > o[1] :
return 0
return 1
def copy(self):
n=copy.deepcopy(self)
return n
def mutate(self,value):
hap=0;
for i in range(len(self.weights_in_hidden)):
for j in range(len(self.weights_in_hidden[i])):
if np.random.normal()<value:
self.weights_in_hidden[i][j]+=random.gauss(0,0.1)
hap+=1
for i in range(len(self.weights_hidden_out)):
for j in range(len(self.weights_hidden_out[i])):
if np.random.normal()<value:
self.weights_hidden_out[i][j]+=random.gauss(0,0.1)
hap+=1