Skip to content

Commit a3e085f

Browse files
committed
Read and store networks in ascii instead of raw bytes, necessary for some machines.
1 parent a4eb580 commit a3e085f

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cmake_minimum_required (VERSION 2.6)
22
add_compile_definitions(WINDOWS)
3+
add_compile_definitions(STORE_NET_AS_ASCII)
34
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
45
project (net)
56
add_executable(net main.c layers.c lstm.c set.c utilities.c)

lstm.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,19 @@ void lstm_store_net_layers(lstm_model_t** model, const char * filename, int laye
825825

826826
while ( p < layers ) {
827827

828+
#ifdef STORE_NET_AS_ASCII
829+
vector_store_ascii(model[p]->Wy, model[p]->F * model[p]->N, fp);
830+
vector_store_ascii(model[p]->Wi, model[p]->N * model[p]->S, fp);
831+
vector_store_ascii(model[p]->Wc, model[p]->N * model[p]->S, fp);
832+
vector_store_ascii(model[p]->Wo, model[p]->N * model[p]->S, fp);
833+
vector_store_ascii(model[p]->Wf, model[p]->N * model[p]->S, fp);
834+
835+
vector_store_ascii(model[p]->by, model[p]->F, fp);
836+
vector_store_ascii(model[p]->bi, model[p]->N, fp);
837+
vector_store_ascii(model[p]->bc, model[p]->N, fp);
838+
vector_store_ascii(model[p]->bf, model[p]->N, fp);
839+
vector_store_ascii(model[p]->bo, model[p]->N, fp);
840+
#else
828841
vector_store(model[p]->Wy, model[p]->F * model[p]->N, fp);
829842
vector_store(model[p]->Wi, model[p]->N * model[p]->S, fp);
830843
vector_store(model[p]->Wc, model[p]->N * model[p]->S, fp);
@@ -836,6 +849,7 @@ void lstm_store_net_layers(lstm_model_t** model, const char * filename, int laye
836849
vector_store(model[p]->bc, model[p]->N, fp);
837850
vector_store(model[p]->bf, model[p]->N, fp);
838851
vector_store(model[p]->bo, model[p]->N, fp);
852+
#endif
839853

840854
++p;
841855
}
@@ -918,6 +932,19 @@ void lstm_read_net_layers(lstm_model_t** model, const char * filename, int layer
918932

919933
while ( p < layers ) {
920934

935+
#ifdef STORE_NET_AS_ASCII
936+
vector_read_ascii(model[p]->Wy, model[p]->F * model[p]->N, fp);
937+
vector_read_ascii(model[p]->Wi, model[p]->N * model[p]->S, fp);
938+
vector_read_ascii(model[p]->Wc, model[p]->N * model[p]->S, fp);
939+
vector_read_ascii(model[p]->Wo, model[p]->N * model[p]->S, fp);
940+
vector_read_ascii(model[p]->Wf, model[p]->N * model[p]->S, fp);
941+
942+
vector_read_ascii(model[p]->by, model[p]->F, fp);
943+
vector_read_ascii(model[p]->bi, model[p]->N, fp);
944+
vector_read_ascii(model[p]->bc, model[p]->N, fp);
945+
vector_read_ascii(model[p]->bf, model[p]->N, fp);
946+
vector_read_ascii(model[p]->bo, model[p]->N, fp);
947+
#else
921948
vector_read(model[p]->Wy, model[p]->F * model[p]->N, fp);
922949
vector_read(model[p]->Wi, model[p]->N * model[p]->S, fp);
923950
vector_read(model[p]->Wc, model[p]->N * model[p]->S, fp);
@@ -929,6 +956,7 @@ void lstm_read_net_layers(lstm_model_t** model, const char * filename, int layer
929956
vector_read(model[p]->bc, model[p]->N, fp);
930957
vector_read(model[p]->bf, model[p]->N, fp);
931958
vector_read(model[p]->bo, model[p]->N, fp);
959+
#endif
932960

933961
++p;
934962
}

utilities.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,31 @@ void vector_read(double * V, int L, FILE * fp)
546546

547547
}
548548

549+
void vector_store_ascii(double* V, int L, FILE * fp)
550+
{
551+
int l = 0;
552+
553+
while ( l < L ){
554+
fprintf(fp, "%.20lf\r\n", V[l]);
555+
++l;
556+
}
557+
}
558+
559+
void vector_read_ascii(double * V, int L, FILE * fp)
560+
{
561+
int l = 0;
562+
563+
while ( l < L ) {
564+
if ( fscanf(fp, "%lf", &V[l]) <= 0 ) {
565+
fprintf(stderr, "%s.%s Failed to read file\r\n",
566+
__FILE__, __func__);
567+
exit(1);
568+
}
569+
++l;
570+
}
571+
572+
}
573+
549574
/*
550575
* This function is used to store a JSON file representation
551576
* of a LSTM neural network that can be read by an HTML application.

utilities.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,7 @@ void vector_print_min_max(char *, double *, int);
8282
void vector_read(double *, int, FILE *);
8383
void vector_store(double *, int, FILE *);
8484
void matrix_store(double **, int, int, FILE *);
85-
void matrix_read(double **, int, int, FILE *);
85+
void matrix_read(double **, int, int, FILE *);
86+
void vector_read_ascii(double *, int, FILE *);
87+
void vector_store_ascii(double *, int, FILE *);
8688
#endif

0 commit comments

Comments
 (0)