L1VM - FANN

I did write new FANN module functions to train, train from data file and save FANNs. I also did update the examples in the lib directory. With FANNs you can create and train neural networks for AI programs. And run them to get the output from the neural network. The complex AI tools like ChatGPT form OpenAI may use billions of neurons to run. Here is the simple XOR train from data file example:

// train-fann-file.l1com
// fann train library demo
// train XOR function and run trained neural network
#include <intr.l1h>
(main func)
    (set int64 1 zero 0)
    (set int64 1 one 1)
    (set int64 1 i 50000)
    (set int64 1 f 0)
    (set int64 1 ret 0)
    (set double 2 input1 -1.0 -1.0)
    (set double 2 input2 -1.0 1.0)
    (set double 2 input3 1.0 -1.0)
    (set double 2 input4 1.0 1.0)
    (set double 1 des_output_null -1.0)
    (set double 1 des_output_one 1.0)
    (set double 1 output 1.0)
    (set const-int64 1 Pinput1_addr 0)
    (set const-int64 1 Pinput2_addr 0)
    (set const-int64 1 Pinput3_addr 0)
    (set const-int64 1 Pinput4_addr 0)
    (set const-int64 1 Pdes_output_null_addr 0)
    (set const-int64 1 Pdes_output_one_addr 0)
    (set const-int64 1 Poutput_addr 0)
    (set int64 1 two 2)
    (set string 16 learning "learning xor...")
    (set string 10 txt_1 "0 xor 0: ")
    (set string 10 txt_2 "0 xor 1: ")
    (set string 10 txt_3 "1 xor 0: ")
    (set string 10 txt_4 "1 xor 1: ")
    (set string 11 run "running...")
    (set int64 1 handle 0)
    (set int64 1 three 3)
    (set int64 1 two 2)
    (set int64 1 layers 3)
    (set int64 1 inputs 2)
    (set int64 1 outputs 1)
    (set int64 1 hidden_neurons 3)
    (set string s create_str "creating xor fann...")
    (set string s fann_data_file_str "fann/xor.data")
    (set int64 1 max_epochs 500000)
    (set int64 1 epochs_between_reports 1000)
    (set double 1 desired_error 0.001)

    // set pointers
    (input1 Pinput1_addr pointer)
    (input2 Pinput2_addr pointer)
    (input3 Pinput3_addr pointer)
    (input4 Pinput4_addr pointer)
    (des_output_null Pdes_output_null_addr pointer)
    (des_output_one Pdes_output_one_addr pointer)
    (output Poutput_addr pointer)

    // init fann library
    (reset-reg)
    (zero one :fann_init_lib !)
    (ret stpopi)
    (((ret zero !=) f =) f if)
        free_mod (zero)
        exit (one)
    (endif)
    (reset-reg)

    // create fann
    print_s (create_str)
    print_n
    (layers inputs hidden_neurons outputs :fann_create !)
    (handle stpop)

    // train fann from xor data file
    (handle fann_data_file_str max_epochs epochs_between_reports desired_error :fann_train_ann_file !)

    // run ann
    print_s (runaddr)
    print_n
    // run fann with one output variable
    (handle Pinput1_addr Poutput_addr two one :fann_run !)
    print_s (txt_1addr)
    print_d (output)
    print_n
    (handle Pinput2_addr Poutput_addr two one :fann_run !)
    print_s (txt_2addr)
    print_d (output)
    print_n
    (handle Pinput3_addr Poutput_addr two one :fann_run !)
    print_s (txt_3addr)
    print_d (output)
    print_n
    (handle Pinput4_addr Poutput_addr two one :fann_run !)
    print_s (txt_4addr)
    print_d (output)
    print_n
    (handle :fann_free !)
    (:free_fanns !)
    free_mod (zero)
    exit (zero)
(funcend)
#include <fann-lib.l1h>

This is the training data file xor.data:

4 2 1
-1 -1
-1
-1 1
1
1 -1
1
1 1
-1

So here are 4 data entries with 2 inputs and 1 output, as set by the first line. Then the two data inputs and the output are following for all the 4 data entries. Here I use negative one -1 as false. And 1as true.

Here is the output of the FANN program:

$ l1vm lib/train-fann-file -q
creating xor fann...
Max epochs   500000. Desired error: 0.0010000000.
Epochs            1. Current error: 0.2501952052. Bit fail 4.
Epochs           26. Current error: 0.0002784925. Bit fail 0.
running...
0 xor 0: -0.9903308153
0 xor 1: 0.9816381335
1 xor 0: 0.9831197858
1 xor 1: -0.9875277281

So now you can train and save FANNs with my FANN module too!