mirror of https://github.com/glouw/tinn
removed asserts and added ecalloc and efopen instead
This commit is contained in:
parent
09bd3f647c
commit
72dd9ed70a
44
Tinn.c
44
Tinn.c
|
@ -1,7 +1,7 @@
|
|||
#include "Tinn.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
|
@ -93,6 +93,34 @@ static void twrand(const Tinn t)
|
|||
for(int i = 0; i < t.nb; i++) t.b[i] = frand() - 0.5f;
|
||||
}
|
||||
|
||||
// Prints a message and exits.
|
||||
static void bomb(const char* const message, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
vprintf(message, args);
|
||||
va_end(args);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Fail safe file opening.
|
||||
static FILE* efopen(const char* const pathname, const char* const mode)
|
||||
{
|
||||
FILE* const file = fopen(pathname, mode);
|
||||
if(file == NULL)
|
||||
bomb("failure: fopen(\"%s\", \"%s\")\n", pathname, mode);
|
||||
return file;
|
||||
}
|
||||
|
||||
// Fail safe clear allocation.
|
||||
static void* ecalloc(const size_t nmemb, const size_t size)
|
||||
{
|
||||
void* const mem = calloc(nmemb, size);
|
||||
if(mem == NULL)
|
||||
bomb("failure: calloc(%d, %d)\n", nmemb, size);
|
||||
return mem;
|
||||
}
|
||||
|
||||
float* xpredict(const Tinn t, const float* in)
|
||||
{
|
||||
forewards(t, in);
|
||||
|
@ -112,11 +140,11 @@ Tinn xtbuild(int nips, int nhid, int nops)
|
|||
// Tinn only supports one hidden layer so there are two biases.
|
||||
t.nb = 2;
|
||||
t.nw = nhid * (nips + nops);
|
||||
assert(t.w = (float*) calloc(t.nw, sizeof(*t.w)));
|
||||
t.w = (float*) ecalloc(t.nw, sizeof(*t.w));
|
||||
t.x = t.w + nhid * nips;
|
||||
assert(t.b = (float*) calloc(t.nb, sizeof(*t.b)));
|
||||
assert(t.h = (float*) calloc(nhid, sizeof(*t.h)));
|
||||
assert(t.o = (float*) calloc(nops, sizeof(*t.o)));
|
||||
t.b = (float*) ecalloc(t.nb, sizeof(*t.b));
|
||||
t.h = (float*) ecalloc(nhid, sizeof(*t.h));
|
||||
t.o = (float*) ecalloc(nops, sizeof(*t.o));
|
||||
t.nips = nips;
|
||||
t.nhid = nhid;
|
||||
t.nops = nops;
|
||||
|
@ -126,8 +154,7 @@ Tinn xtbuild(int nips, int nhid, int nops)
|
|||
|
||||
void xtsave(const Tinn t, const char* path)
|
||||
{
|
||||
FILE* file;
|
||||
assert(file = fopen(path, "w"));
|
||||
FILE* file = efopen(path, "w");
|
||||
// Header.
|
||||
fprintf(file, "%d %d %d\n", t.nips, t.nhid, t.nops);
|
||||
// Biases and weights.
|
||||
|
@ -138,8 +165,7 @@ void xtsave(const Tinn t, const char* path)
|
|||
|
||||
Tinn xtload(const char* path)
|
||||
{
|
||||
FILE* file;
|
||||
assert(file = fopen(path, "r"));
|
||||
FILE* file = efopen(path, "r");
|
||||
int nips = 0;
|
||||
int nhid = 0;
|
||||
int nops = 0;
|
||||
|
|
Loading…
Reference in New Issue