AI-NeuralNet-Simple
view release on metacpan or search on metacpan
n->learn_rate = rate;
}
double c_get_delta(int handle)
{
NEURAL_NETWORK *n = c_get_network(handle);
return n->delta;
}
void c_set_delta(int handle, double delta)
{
NEURAL_NETWORK *n = c_get_network(handle);
n->delta = delta;
}
int c_get_use_bipolar(int handle)
{
NEURAL_NETWORK *n = c_get_network(handle);
return n->use_bipolar;
}
void c_set_use_bipolar(int handle, int bipolar)
{
NEURAL_NETWORK *n = c_get_network(handle);
n->use_bipolar = bipolar;
}
int c_create_network(NEURAL_NETWORK *n)
{
int i;
/* each of the next two variables has an extra row for the "bias" */
int input_layer_with_bias = n->size.input + 1;
int hidden_layer_with_bias = n->size.hidden + 1;
n->learn_rate = .2;
n->delta = 1.0;
n->use_bipolar = 0;
n->tmp = malloc(sizeof(double) * n->size.input);
n->neuron.input = malloc(sizeof(double) * n->size.input);
n->neuron.hidden = malloc(sizeof(double) * n->size.hidden);
n->neuron.output = malloc(sizeof(double) * n->size.output);
n->neuron.target = malloc(sizeof(double) * n->size.output);
n->error.hidden = malloc(sizeof(double) * n->size.hidden);
n->error.output = malloc(sizeof(double) * n->size.output);
/* one extra for sentinel */
n->weight.input_to_hidden
= malloc(sizeof(void *) * (input_layer_with_bias + 1));
n->weight.hidden_to_output
= malloc(sizeof(void *) * (hidden_layer_with_bias + 1));
if(!n->weight.input_to_hidden || !n->weight.hidden_to_output) {
printf("Initial malloc() failed\n");
return 0;
}
/* now allocate the actual rows */
for(i = 0; i < input_layer_with_bias; i++) {
n->weight.input_to_hidden[i]
= malloc(hidden_layer_with_bias * sizeof(double));
if(n->weight.input_to_hidden[i] == 0) {
free(*n->weight.input_to_hidden);
printf("Second malloc() to weight.input_to_hidden failed\n");
return 0;
}
}
/* now allocate the actual rows */
for(i = 0; i < hidden_layer_with_bias; i++) {
n->weight.hidden_to_output[i]
= malloc(n->size.output * sizeof(double));
if(n->weight.hidden_to_output[i] == 0) {
free(*n->weight.hidden_to_output);
printf("Second malloc() to weight.hidden_to_output failed\n");
return 0;
}
}
/* initialize the sentinel value */
n->weight.input_to_hidden[input_layer_with_bias] = 0;
n->weight.hidden_to_output[hidden_layer_with_bias] = 0;
return 1;
}
void c_destroy_network(int handle)
{
double **row;
NEURAL_NETWORK *n = c_get_network(handle);
for(row = n->weight.input_to_hidden; *row != 0; row++) {
free(*row);
}
free(n->weight.input_to_hidden);
for(row = n->weight.hidden_to_output; *row != 0; row++) {
free(*row);
}
free(n->weight.hidden_to_output);
free(n->neuron.input);
free(n->neuron.hidden);
free(n->neuron.output);
free(n->neuron.target);
free(n->error.hidden);
free(n->error.output);
free(n->tmp);
network[handle] = NULL;
}
/*
* Build a Perl reference on array `av'.
* This performs something like "$rv = \@av;" in Perl.
*/
SV *build_rv(AV *av)
{
SV *rv;
/*
* To understand what is going on here, look at retrieve_ref()
* in the Storable.xs file. In particular, we don't perform
* an SvREFCNT_inc(av) because the av we're supplying is going
* to be referenced only by the REF we're building here.
* --RAM
*/
rv = NEWSV(10002, 0);
sv_upgrade(rv, SVt_RV);
SvRV(rv) = (SV *) av;
SvROK_on(rv);
( run in 0.505 second using v1.01-cache-2.11-cpan-39bf76dae61 )