AI-NeuralNet-Simple
view release on metacpan or search on metacpan
croak("c_import_network() not given an array reference");
av = get_array(rv);
/* Check version number */
sav = av_fetch(av, i++, 0);
if (sav == NULL || SvIVx(*sav) != EXPORT_VERSION)
croak("c_import_network() given unknown version %d",
sav == NULL ? 0 : SvIVx(*sav));
/* Check length -- at version 1, length is fixed to 13 */
if (av_len(av) + 1 != EXPORTED_ITEMS)
croak("c_import_network() not given a %d-item array reference",
EXPORTED_ITEMS);
handle = c_new_handle();
n = c_get_network(handle);
sav = av_fetch(av, i++, 0);
if (sav == NULL)
croak("undefined input size (item %d)", i - 1);
for (i = 0; i < n->size.output; i++)
error += sqr(target[i] - n->neuron.output[i]);
return 0.5 * error;
}
double c_train(int handle, SV* input, SV* output)
{
NEURAL_NETWORK *n = c_get_network(handle);
int i,length;
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;
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);
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;
}
}
( run in 0.647 second using v1.01-cache-2.11-cpan-65fba6d93b7 )