Howto create and populate a real array

Discussions related to Visual Prolog
kingchris
Active Member
Posts: 26
Joined: 26 Sep 2005 9:35

Howto create and populate a real array

Unread post by kingchris »

I wish to interface with the fann library for neural nets.

I have a class fact that holds the 784 real values that I wish to pass. Assume that I have added the 784 real values already to the list.

Code: Select all

class facts - test     real_data: (real_list) nondeterm.
This is the function that I want to call. Its 2nd parameter must be an array of reals. Due to the size of my first neural network layer this will be 784 reals.

Code: Select all

 fann_train: (pointer Fann, pointer ArrayInput, unsigned SizeInput, pointer ArrayOutput, unsigned SizeOutput) language c as "_fann_train@12".
How do I declare/create an array of reals. Then populate this array with data from my list. How does one add values to an array. I know how to iterate through my list.

Thank You
User avatar
Thomas Linder Puls
VIP Member
Posts: 1176
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Fortunately there is a class called arrayM_inline that can handle this.

I suggest that you create a fann_api package containing two classes fann_api and fann_native.

In the _native you place all the language c as "..." declarations, and in the _api class you place Visual Prolog friendly versions of the same predicates.

The code below shows the important details.

Code: Select all

class fann_native     open core predicates     fann_train : (pointer Fann, pointer ArrayInput, positive SizeInput, pointer ArrayOutput, positive SizeOutput) language c as "_fann_train@12". end class fann_native

Code: Select all

class fann_api     open core predicates     fann_train : (pointer Fann, real* Input, positive SizeOutput) -> real* Output. end class fann_api

Code: Select all

implement fann_api     open core clauses     fann_train(Fann, Input) = Output :-         InArray = arrayM_inline::new_list(Input, true),         OutArraySize = InArray:size, % for the example we assume that the output array must have same size as the input array         OutArray = arrayM_inline::newAtomic(OutArraySize),         fann_native::fann_train(Fann, InArray:data, InArray:size, OutArray:data, OutArray:size),         Output = OutArray:valueList. end implement fann_api
Some comments:

I have changed the "size" arguments from unsigned to positive, because that fits better with arrayM_inline, and it makes no difference in the interfacing to the c routine.

More important: The output array is actually an "input array", which the fann_train routine fills with values. So you have to create this array and pass it to the routine. I do not know how to set the size of this array, so I have just used the same size as the input array.
Regards Thomas Linder Puls
PDC
kingchris
Active Member
Posts: 26
Joined: 26 Sep 2005 9:35

Unread post by kingchris »

Thanks for the help. You have excellent suggestions.

Just for question completeness.

Code: Select all

fann_train : (pointer Fann, pointer ArrayInput, positive SizeInput, pointer ArrayOutput, positive SizeOutput)
The

Code: Select all

pointer ArrayOutput
parameter is actually an input parameter to the neural net.

I am learning neural nets by using the Digit Recognizer data set from Kaggle
http://www.kaggle.com/c/digit-recognizer

This has 42 000 lines of 784 values representing a digit and the value of the digit. So if one line represented an eight. To train the net I would pass the 784 values to the ArrayInput as an array of reals. Then I would also create an array of 10 reals set to zero. I would then set the 8th value to 1.0. (being careful where zero sits) This I would pass as the ArrayOutput parameter. I am therefore telling the net that these input values represent the output value of
(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 )
which says an 8. I then call this function 42 000 times with all off my training data.

Later I start calling a different function ( I think ) fann_test which I fill the Real_Input_Array with the data of the digit that I want it to recognize. Then it will fill the Real_Output_Array with one the the entries have a higher value than zero to show which digit it thinks it is.

Code: Select all

fann_test:(pointer Fann, pointer Real_Input_Array, pointer Real_Output_Array) language c as "_fann_test@12".
Much fun and games to follow. I am sure.
Post Reply