-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
154 lines (134 loc) · 3.52 KB
/
test.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include"config.h"
#include"vector.h"
#include"LDNN.h"
vector_t *read_data(FILE *file, int *dim, int *len) {
fscanf(file, "%d", dim);
fscanf(file, "%d", len);
vector_t *data = malloc((*len) * (*dim) * sizeof(PRECISION));
for(int i=0; i<*len; i++)
for(int j=0; j<*dim; j++)
fscanf(file, "%f", &data[i][j]);
return data;
}
void write_data(FILE *file, int dim, int len, vector_t *data) {
fprintf(file, "%d ", dim);
fprintf(file, "%d\n", len);
for(int i=0; i<len; i++) {
for(int j=0; j<dim; j++)
fprintf(file, "%f ", data[i][j]);
fprintf(file, "\n");
}
}
/*
network_t network *read_model(FILE *file) {
int N, M;
fscanf(file, "%d", N);
fscanf(file, "%d", M);
vector_t *data = malloc((*len) * (*dim) * sizeof(PRECISION));
for(int i=0; i<*len; i++)
for(int j=0; j<*dim; j++)
fscanf(file, "%f", &data[i][j]);
return data;
}*/
void write_model(FILE *file, network_t network) {
printf("%d %d %d\n", network.N, network.M, settings.DIM);
for(int i=0; i<network.N; i++) {
for(int j=0; j<network.M; j++) {
for(int k=0; k<settings.DIM; k++) {
//printf("%f ", );
}
}
}
}
vector_t *gen_train_data2(PRECISION range, PRECISION min_dist, PRECISION max_dist, int amount) {
vector_t *data = malloc(amount*sizeof(vector_t));
for(int i=0; i<amount; i++) {
data[i] = malloc(settings.DIM*sizeof(PRECISION));
PRECISION dist = 0;
do {
for(int j=0; j<settings.DIM; j++)
data[i][j] = (abs(rand())%(int)(1000*max_dist))/(PRECISION)1000;
dist = sqrt(vector_scalar_prod(data[i], data[i]));
} while (dist >= min_dist && dist <= max_dist);
}
return data;
}
vector_t *gen_train_data(PRECISION range, PRECISION offset, int amount) {
vector_t *data = malloc(amount*sizeof(vector_t));
for(int i=0; i<amount; i++) {
data[i] = malloc(settings.DIM*sizeof(PRECISION));
for(int j=0; j<settings.DIM; j++)
data[i][j] = (abs(rand())%(int)(1000*range))/(PRECISION)1000 + offset;
}
return data;
}
vector_t *gen_train_data3(PRECISION maxval, PRECISION minval, int amount) {
vector_t *data = malloc(amount*sizeof(vector_t));
for(int i=0; i<amount; i++) {
data[i] = malloc(settings.DIM*sizeof(PRECISION));
for(int j=0; j<settings.DIM; j++) {
do {
data[i][j] = (rand()%(int)(1000*maxval))/(PRECISION)1000;
} while(fabs(data[i][j]) < minval);
}
}
return data;
}
void test0() {
settings.N = 5;
settings.M = 5;
settings.DIM = 3;
settings.alpha = 0.001;
network_t *network = make_network();
int neg_len = 100;
int pos_len = 100;
vector_t *neg = gen_train_data(6, 0, neg_len);
vector_t *pos = gen_train_data(6, 3, pos_len);
init_network(network, neg_len, neg, pos_len, pos);
PRECISION array[10];
for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
for(int k=0; k<10; k++) {
array[0] = i;
array[1] = j;
array[2] = k;
printf("%1.0f ", classify(network, array));
}
puts("");
}
puts("");
}
}
void test1() {
settings.N = 3;
settings.M = 3;
settings.DIM = 2;
settings.alpha = 0.001;
network_t *network = make_network();
int neg_len = 5;
int pos_len = 5;
vector_t *neg = gen_train_data3(2, 0, neg_len);
vector_t *pos = gen_train_data3(4, 2, pos_len);
init_network(network, neg_len, neg, pos_len, pos);
PRECISION array[10];
for(int i=-5; i<5; i++) {
for(int j=-5; j<5; j++) {
array[0] = i;
array[1] = j;
printf("%1.0f ", classify(network, array));
}
puts("");
}
}
int main(int argc, char *argv[]) {
srand(time(NULL));
if(argc==1) {
test0();
} else {
test1();
}
}