AI-NeuralNet-Simple
view release on metacpan or search on metacpan
AV *array;
double *input_array = malloc(sizeof(double) * n->size.input);
double *output_array = malloc(sizeof(double) * n->size.output);
double error;
if (! is_array_ref(input) || ! is_array_ref(output)) {
croak("train() takes two arrayrefs.");
}
array = get_array(input);
length = av_len(array)+ 1;
if (length != n->size.input) {
croak("Length of input array does not match network");
}
for (i = 0; i < length; i++) {
input_array[i] = get_float_element(array, i);
}
array = get_array(output);
length = av_len(array) + 1;
if (length != n->size.output) {
croak("Length of output array does not match network");
}
for (i = 0; i < length; i++) {
output_array[i] = get_float_element(array, i);
}
c_feed(n, input_array, output_array, 1);
error = mean_square_error(n, output_array);
free(input_array);
free(output_array);
return error;
}
int c_new_network(int input, int hidden, int output)
{
NEURAL_NETWORK *n;
int handle;
handle = c_new_handle();
n = c_get_network(handle);
n->size.input = input;
n->size.hidden = hidden;
n->size.output = output;
if (!c_create_network(n))
return -1;
/* Perl already seeded the random number generator, via a rand(1) call */
c_assign_random_weights(n);
return handle;
}
double c_train_set(int handle, SV* set, int iterations, double mse)
{
NEURAL_NETWORK *n = c_get_network(handle);
AV *input_array, *output_array; /* perl arrays */
double *input, *output; /* C arrays */
double max_error = 0.0;
int set_length=0;
int i,j;
int index;
set_length = av_len(get_array(set))+1;
if (!set_length)
croak("_train_set() array ref has no data");
if (set_length % 2)
croak("_train_set array ref must have an even number of elements");
/* allocate memory for out input and output arrays */
input_array = get_array_from_aoa(set, 0);
input = malloc(sizeof(double) * set_length * (av_len(input_array)+1));
output_array = get_array_from_aoa(set, 1);
output = malloc(sizeof(double) * set_length * (av_len(output_array)+1));
for (i=0; i < set_length; i += 2) {
input_array = get_array_from_aoa(set, i);
if (av_len(input_array)+1 != n->size.input)
croak("Length of input data does not match");
/* iterate over the input_array and assign the floats to input */
for (j = 0; j < n->size.input; j++) {
index = (i/2*n->size.input)+j;
input[index] = get_float_element(input_array, j);
}
output_array = get_array_from_aoa(set, i+1);
if (av_len(output_array)+1 != n->size.output)
croak("Length of output data does not match");
for (j = 0; j < n->size.output; j++) {
index = (i/2*n->size.output)+j;
output[index] = get_float_element(output_array, j);
}
}
for (i = 0; i < iterations; i++) {
max_error = 0.0;
for (j = 0; j < (set_length/2); j++) {
double error;
c_feed(n, &input[j*n->size.input], &output[j*n->size.output], 1);
if (mse >= 0.0 || i == iterations - 1) {
error = mean_square_error(n, &output[j*n->size.output]);
if (error > max_error)
max_error = error;
}
}
if (mse >= 0 && max_error <= mse) /* Below their target! */
break;
}
free(input);
free(output);
return max_error;
}
SV* c_infer(int handle, SV *array_ref)
{
NEURAL_NETWORK *n = c_get_network(handle);
int i;
AV *perl_array, *result = newAV();
/* feed the data */
perl_array = get_array(array_ref);
for (i = 0; i < n->size.input; i++)
n->tmp[i] = get_float_element(perl_array, i);
c_feed(n, n->tmp, NULL, 0);
/* read the results */
for (i = 0; i < n->size.output; i++) {
av_push(result, newSVnv(n->neuron.output[i]));
}
return newRV_noinc((SV*) result);
}
void c_feed(NEURAL_NETWORK *n, double *input, double *output, int learn)
{
int i;
for (i=0; i < n->size.input; i++) {
n->neuron.input[i] = input[i];
}
if (learn)
for (i=0; i < n->size.output; i++)
n->neuron.target[i] = output[i];
c_feed_forward(n);
if (learn) c_back_propagate(n);
}
/*
* The original author of this code is M. Tim Jones <mtj@cogitollc.com> and
* written for the book "AI Application Programming", by Charles River Media.
*
* It's been so heavily modified that it bears little resemblance to the
* original, but credit should be given where credit is due. Therefore ...
c_destroy_network(handle);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
SV *
build_rv (av)
AV * av
SV *
build_axaref (arena, rows, columns)
void * arena
int rows
int columns
SV *
c_export_network (handle)
int handle
void
c_load_axa (hold, idx, arena, rows, columns)
AV * hold
int idx
void * arena
int rows
int columns
PREINIT:
I32* temp;
PPCODE:
temp = PL_markstack_ptr++;
c_load_axa(hold, idx, arena, rows, columns);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
int
c_import_network (rv)
SV * rv
double
c_train (handle, input, output)
int handle
SV * input
SV * output
int
c_new_network (input, hidden, output)
int input
int hidden
int output
double
c_train_set (handle, set, iterations, mse)
int handle
SV * set
int iterations
double mse
SV *
c_infer (handle, array_ref)
int handle
SV * array_ref
( run in 0.428 second using v1.01-cache-2.11-cpan-96521ef73a4 )