File 00_simslope/simslope.c added (mode: 100644) (index 0000000..f97d055) |
|
1 |
|
|
|
2 |
|
/* |
|
3 |
|
** Generates simulated data of two classes. |
|
4 |
|
** Measurements come from 1 axis. |
|
5 |
|
** Class 0 has a generally downard slope, class 1 has a generally |
|
6 |
|
** upward slope. |
|
7 |
|
** The NN must learn weights that correspond to the difference |
|
8 |
|
** in slope (printing out the weights at end makes this clear). |
|
9 |
|
*/ |
|
10 |
|
|
|
11 |
|
#include <stdio.h> |
|
12 |
|
#include <stdlib.h> |
|
13 |
|
#include <math.h> |
|
14 |
|
#include <time.h> |
|
15 |
|
|
|
16 |
|
static unsigned int rand_x[56],rand_y[256],rand_z; /* used for normal dist */ |
|
17 |
|
static int rand_j, rand_k; /* used for normal dist */ |
|
18 |
|
|
|
19 |
|
|
|
20 |
|
unsigned urand0 (void) |
|
21 |
|
{ |
|
22 |
|
if (--rand_j == 0) rand_j = 55; |
|
23 |
|
if (--rand_k == 0) rand_k = 55; |
|
24 |
|
return rand_x[rand_k] += rand_x[rand_j]; |
|
25 |
|
} |
|
26 |
|
|
|
27 |
|
void init_generators(unsigned seed) |
|
28 |
|
{ |
|
29 |
|
int i; |
|
30 |
|
|
|
31 |
|
/* inits the normal generator */ |
|
32 |
|
rand_x[1] = 1; |
|
33 |
|
if(seed) |
|
34 |
|
rand_x[2] = seed; |
|
35 |
|
else |
|
36 |
|
rand_x[2] = time (NULL); |
|
37 |
|
for (i=3; i<56; ++i) rand_x[i] = rand_x[i-1] + rand_x[i-2]; |
|
38 |
|
rand_j = 24; |
|
39 |
|
rand_k = 55; |
|
40 |
|
for (i=255; i>=0; --i) |
|
41 |
|
urand0 (); //run loop for a while |
|
42 |
|
for (i=255; i>=0; --i) |
|
43 |
|
rand_y[i] = urand0 (); |
|
44 |
|
rand_z = urand0 (); |
|
45 |
|
|
|
46 |
|
/* inits the uniform generator */ |
|
47 |
|
srand( (unsigned)time( NULL ) ); |
|
48 |
|
} |
|
49 |
|
|
|
50 |
|
unsigned urand (void) |
|
51 |
|
{ |
|
52 |
|
int i; |
|
53 |
|
|
|
54 |
|
i = rand_z % 256; |
|
55 |
|
rand_z = rand_y[i]; |
|
56 |
|
if (--rand_j == 0) rand_j = 55; |
|
57 |
|
if (--rand_k == 0) rand_k = 55; |
|
58 |
|
rand_y[i] = rand_x[rand_k] += rand_x[rand_j]; |
|
59 |
|
return rand_z; |
|
60 |
|
} |
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
/* returns a value from a normal dist with mean=0 stddev=1 */ |
|
66 |
|
|
|
67 |
|
double normal_rand (void) |
|
68 |
|
{ |
|
69 |
|
static int flag = 0; |
|
70 |
|
static double z, a = 2147483648.0; |
|
71 |
|
double v1, v2, s; |
|
72 |
|
|
|
73 |
|
if (flag) { |
|
74 |
|
flag = 0; |
|
75 |
|
return z; |
|
76 |
|
} |
|
77 |
|
flag = 1; |
|
78 |
|
do { |
|
79 |
|
v1 = urand()/a - 1; |
|
80 |
|
v2 = urand()/a - 1; |
|
81 |
|
} |
|
82 |
|
while ((s = v1*v1 + v2*v2) > 1.0); |
|
83 |
|
s = sqrt (-2.0 * log(s) / s); |
|
84 |
|
z = v1 * s; |
|
85 |
|
return v2 * s; |
|
86 |
|
} |
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
/* returns a value from -4...+4 */ |
|
91 |
|
|
|
92 |
|
double uniform_rand (void) |
|
93 |
|
{ |
|
94 |
|
int randgen; |
|
95 |
|
double ret; |
|
96 |
|
|
|
97 |
|
randgen=rand(); |
|
98 |
|
ret=((double)randgen/(double)RAND_MAX*8.0)-4.0; |
|
99 |
|
return ret; |
|
100 |
|
} |
|
101 |
|
|
|
102 |
|
|
|
103 |
|
#define DATA 1000 |
|
104 |
|
#define SAMPLES_PER_DATA 20 |
|
105 |
|
|
|
106 |
|
int main() |
|
107 |
|
|
|
108 |
|
{ |
|
109 |
|
int i,j; |
|
110 |
|
int d,slope; |
|
111 |
|
double base,n,step; |
|
112 |
|
|
|
113 |
|
init_generators(0); |
|
114 |
|
|
|
115 |
|
for (j=0; j<DATA; j++) |
|
116 |
|
{ |
|
117 |
|
n=normal_rand(); |
|
118 |
|
if (n < 0.0) |
|
119 |
|
slope=-1; |
|
120 |
|
else |
|
121 |
|
slope=1; |
|
122 |
|
n=normal_rand(); |
|
123 |
|
base=n*100.0+500.0; |
|
124 |
|
printf("%d\t%d",(slope == -1 ? 0 : 1),(int)base); |
|
125 |
|
for (d=1; d<SAMPLES_PER_DATA; d++) |
|
126 |
|
{ |
|
127 |
|
n=normal_rand(); |
|
128 |
|
step=n*3.0+(double)slope*2; /* 3.0 => low noise, 10.0 => high noise */ |
|
129 |
|
base+=step; |
|
130 |
|
printf("\t%d",(int)base); |
|
131 |
|
} |
|
132 |
|
printf("\n"); |
|
133 |
|
} |
|
134 |
|
|
|
135 |
|
} |
File 00_simslope/simslope0.py added (mode: 100644) (index 0000000..ae8e351) |
|
1 |
|
# |
|
2 |
|
# Normalizes the data; this was the key to getting it to work. |
|
3 |
|
# I also taught myself a little python so I could load and |
|
4 |
|
# manipulate the data using raw python instead of relying on numpy. |
|
5 |
|
# |
|
6 |
|
# fixed normalization code to calculate from "data" instead of "d" |
|
7 |
|
# |
|
8 |
|
# added line to save architecture, and adjusted architecture so |
|
9 |
|
# that it will save/load correct (see README.txt) |
|
10 |
|
# |
|
11 |
|
|
|
12 |
|
# TensorFlow, keras, numpy |
|
13 |
|
import tensorflow as tf |
|
14 |
|
from tensorflow import keras |
|
15 |
|
import numpy as np |
|
16 |
|
|
|
17 |
|
# open file |
|
18 |
|
f=open('simslope.txt','r') |
|
19 |
|
d=[[int(x) for x in line.split()] for line in f] |
|
20 |
|
f.close() |
|
21 |
|
|
|
22 |
|
# copy to classes and data |
|
23 |
|
classes=[] |
|
24 |
|
data=[] |
|
25 |
|
|
|
26 |
|
for a in range(0,len(d)): |
|
27 |
|
classes.append(d[a][0]) |
|
28 |
|
row=[] |
|
29 |
|
for b in range(1,len(d[a])): |
|
30 |
|
row.append(d[a][b]) |
|
31 |
|
data.append(row) |
|
32 |
|
|
|
33 |
|
#print(classes) |
|
34 |
|
#print(data) |
|
35 |
|
|
|
36 |
|
# normalize each row of data |
|
37 |
|
normdata=[] |
|
38 |
|
for a in range(0,len(data)): |
|
39 |
|
norm=[] |
|
40 |
|
s=min(data[a]) |
|
41 |
|
t=max(data[a]) |
|
42 |
|
for b in range(0,len(data[a])): |
|
43 |
|
norm.append((float(data[a][b])-float(s))/(float(t)-float(s))) |
|
44 |
|
normdata.append(norm) |
|
45 |
|
|
|
46 |
|
#print(normdata) |
|
47 |
|
|
|
48 |
|
classes=np.array(classes) |
|
49 |
|
data=np.array(normdata) |
|
50 |
|
|
|
51 |
|
print("data has shape", data.shape) |
|
52 |
|
print("classes has shape ", classes.shape) |
|
53 |
|
|
|
54 |
|
# convert classes to one hot vectors (each vector position is a class) |
|
55 |
|
# don't need this, but left it in here in case I ever do |
|
56 |
|
# i.e. convert classes like 1 2 3 4 to [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1] |
|
57 |
|
# vect_classes = keras.utils.to_categorical(classes, num_classes=None) |
|
58 |
|
|
|
59 |
|
model = keras.Sequential([ |
|
60 |
|
# cannot have InputLayer() or network will not save/load (bug in TF) |
|
61 |
|
# instead, specify input_shape in first real layer |
|
62 |
|
# keras.layers.InputLayer(input_shape=(20,)), |
|
63 |
|
keras.layers.Dense(2, input_shape=(20,), activation='sigmoid') |
|
64 |
|
]) |
|
65 |
|
|
|
66 |
|
model.compile(optimizer='adam', |
|
67 |
|
loss='sparse_categorical_crossentropy', |
|
68 |
|
metrics=['accuracy']) |
|
69 |
|
|
|
70 |
|
print("Training") |
|
71 |
|
metrics = model.fit(data, classes, epochs=100, verbose=2, |
|
72 |
|
validation_split=0.2) |
|
73 |
|
print("Max value: ", max(metrics.history['acc']), " at epoch", np.argmax(metrics.history['acc'])) |
|
74 |
|
|
|
75 |
|
print("Testing") |
|
76 |
|
test_loss, test_acc = model.evaluate(data, classes) |
|
77 |
|
print('Test accuracy:', test_acc) |
|
78 |
|
|
|
79 |
|
w=model.get_weights() |
|
80 |
|
print(w) |
|
81 |
|
|
|
82 |
|
tf.keras.models.save_model(model,'simslope.h5',True,True) |
|
83 |
|
|
File 00_simslope/simslope1.py added (mode: 100644) (index 0000000..6e36081) |
|
1 |
|
# |
|
2 |
|
# Loads model from saved file, then classifies data |
|
3 |
|
# |
|
4 |
|
|
|
5 |
|
# TensorFlow, keras, np |
|
6 |
|
import tensorflow as tf |
|
7 |
|
from tensorflow import keras |
|
8 |
|
import numpy as np |
|
9 |
|
|
|
10 |
|
# open file |
|
11 |
|
f=open('simslope.txt','r') |
|
12 |
|
d=[[int(x) for x in line.split()] for line in f] |
|
13 |
|
f.close() |
|
14 |
|
|
|
15 |
|
# copy to classes and data |
|
16 |
|
classes=[] |
|
17 |
|
data=[] |
|
18 |
|
|
|
19 |
|
for a in range(0,len(d)): |
|
20 |
|
classes.append(d[a][0]) |
|
21 |
|
row=[] |
|
22 |
|
for b in range(1,len(d[a])): |
|
23 |
|
row.append(d[a][b]) |
|
24 |
|
data.append(row) |
|
25 |
|
|
|
26 |
|
#print(classes) |
|
27 |
|
#print(data) |
|
28 |
|
|
|
29 |
|
# normalize each row of data |
|
30 |
|
normdata=[] |
|
31 |
|
for a in range(0,len(data)): |
|
32 |
|
norm=[] |
|
33 |
|
s=min(data[a]) |
|
34 |
|
t=max(data[a]) |
|
35 |
|
for b in range(0,len(data[a])): |
|
36 |
|
norm.append((float(data[a][b])-float(s))/(float(t)-float(s))) |
|
37 |
|
normdata.append(norm) |
|
38 |
|
|
|
39 |
|
#print(normdata) |
|
40 |
|
|
|
41 |
|
classes=np.array(classes) |
|
42 |
|
data=np.array(normdata) |
|
43 |
|
|
|
44 |
|
print("data has shape", data.shape) |
|
45 |
|
print("classes has shape ", classes.shape) |
|
46 |
|
|
|
47 |
|
model=keras.models.load_model('simslope.h5') |
|
48 |
|
model.summary() |
|
49 |
|
|
|
50 |
|
print("Testing") |
|
51 |
|
test_loss, test_acc = model.evaluate(data, classes) |
|
52 |
|
print('Test accuracy:', test_acc) |
|
53 |
|
|
|
54 |
|
w=model.get_weights() |
|
55 |
|
print(w) |
|
56 |
|
|
|
57 |
|
|