-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNN_util_funcs.py
More file actions
55 lines (42 loc) · 1.7 KB
/
NN_util_funcs.py
File metadata and controls
55 lines (42 loc) · 1.7 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
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
# split x and y into random batches
# return a list of [(batch1_x,batch1_y)...]
def get_random_batches(x,y,batch_size):
batches = []
batch_cnt = x.shape[0] // batch_size
if x.shape[0] % batch_size != 0:
batch_cnt += 1
vals = np.arange(0,x.shape[0])
for i in range(batch_cnt-1):
batch_idx = np.random.choice(vals, batch_size, replace=False)
# remove elems that have been selected
for j in range(len(batch_idx)):
posDex = np.where(vals==batch_idx[j])[0][0]
vals = np.delete(vals, posDex)
if len(x.shape) == 1:
batches.append( (x[batch_idx], y[batch_idx]) )
else:
batches.append( (x[batch_idx,:], y[batch_idx]) )
# last batch often is less than batch size, we can still include that data
# by doing this though:
if len(x.shape) == 1:
batches.append( (x[vals], y[vals]) )
else:
batches.append( (x[vals,:], y[vals]) )
return batches
def compute_loss_and_acc(y_actual, y_predicted, acceptable_diff=25):
"""
Parameters:
acceptable_diff - [float] range (-diff, diff) that NN estimate needs to be
in to be deemed 'correct'
"""
loss, acc = None, None
y_actual.flatten()
y_predicted.flatten()
loss = np.sum( (y_actual - y_predicted)**2 ) / y_actual.shape[0] # MSE loss
if len(y_actual.shape) == 1:
acc = np.sum( (np.abs(y_actual - y_predicted) < acceptable_diff).astype(np.int) ) / y_actual.shape[0]
else:
# assume 2 outputs for PID case [PWM, angle]
acc = np.sum( (np.abs(y_actual[:,0] - y_predicted[:,0]) < acceptable_diff).astype(np.int) ) / y_actual.shape[0]
return loss, acc