forked from HBPNeurorobotics/hbpprak_2018_throwing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedforward_network.py
executable file
·44 lines (32 loc) · 1.15 KB
/
feedforward_network.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
import numpy as np
try:
import _pickle as pickle
except ImportError:
import cPickle as pickle
class FeedForwardNetwork(object):
def __init__(self, layer_sizes, *args,**kwargs):
self.weights = []
self.bias = []
for index in range(len(layer_sizes)-1):
self.weights.append(np.random.uniform(-1,1,(layer_sizes[index], layer_sizes[index+1])))
self.bias.append(np.random.uniform(-1,1, (1,layer_sizes[index+1])))
def predict(self, inp, logger):
out = inp
for weight, bias in zip(self.weights, self.bias):
out = np.dot(out, weight) + bias
out = np.tanh(out)
return out[0]
def get_weights(self):
return self.weights
def set_weights(self, weights):
self.weights = weights
def get_bias(self):
return self.bias
def set_bias(self,bias):
self.bias = bias
def save(self, filename='weights.pkl'):
with open(filename, 'wb') as fp:
pickle.dump(self.weights, fp)
def load(self, filename='weights.pkl'):
with open(filename, 'rb') as fp:
self.weights = pickle.load(fp)