VNNikolaidis / nnlib2Rcpp

An R package for Neural Nets created using nnlib2
Other
13 stars 4 forks source link

Help retrieving pe attributes from nn object #13

Closed tpq closed 1 year ago

tpq commented 1 year ago

Hi, great package! Love it already :)

I am starting to have a play with the code and find myself stuck on what might be a silly problem.

I would like to access the learned biases from the pe attributes of an nn object.

For example, I follow the code:

a <- new( "NN" ) # create a NN object in variable a
a$add_layer( "generic", 4 ) # 1. a layer of 4 generic nodes
a$add_connection_set( "BP" ) # 2. a set of BP connections

a$add_layer( "BP-hidden", 3 ) # 3. a layer of 3 BP pes
a$add_connection_set( "BP" ) # 4. another set of BP connections
a$add_layer( "BP-hidden", 2 ) # 5. another layer of 2 BP pes
a$add_connection_set( "BP" ) # 6. another set of BP connections
a$add_layer( "BP-hidden", 3 ) # 7. another layer of 3 BP pes
a$add_connection_set( "BP" ) # 8. another set of BP connections
a$add_layer( "BP-output", 4 ) # 9. a layer of 4 BP output pes
a$create_connections_in_sets ( 0, 1 ) # Populate sets with actual connections

for(e in 1:1000) # for 1000 epochs
    for(r in 1:nrow(iris_s)) # for each data case
    {
        a$input_at( 1, iris_s[ r , ] ) # present data at 1st layer
        a$recall_all( TRUE ) # recall (fwd direction, entire topology)
        a$input_at( 9, iris_s[ r , ] ) # present data at last layer
        a$encode_all ( FALSE ) # encode, adjusting weights (bwd-direction in topology)
    }

If I wish to access the bias associated with the n-th component, is there any R syntax akin to something like a[[n]]$bias that may let me retrieve these (and other attributes) directly from the nn topology?

VNNikolaidis commented 1 year ago

Thank you for your comments on the package as well as your useful suggestions. Indeed, having access to the biases may be useful when experimenting with NNs.

Based on those suggestions, in the current version of package (v.0.1.10) some methods were added to the NN module. These are: set_biases_at, set_bias_at, get_biases_atand get_bias_at.

For your example you can now write

a$set_biases_at(1,c(10,20,30,40))

to change the biases in the 4 nodes of layer @1 to 10, 20, 30 and 40 respectively, or

a$get_biases_at(3)

to get a vector containing the biases of nodes in layer @3.

The other methods, get_bias_at and set_bias_at allow you to specify a single node in a layer (note that nodes are numbered starting from 0) and access its bias.

See help(NN) for details.

The package v.0.1.10 can be found at the github repo and can be installed from there, see instructions here.

Once some more testing is performed it will also be submitted to CRAN. Please see here for some other modifications in the current version of package that you may find useful.

Will be happy to hear more suggestions from you and other users of the package.

If you find the aforementioned solution sufficient, please close the issue on GitHub (see here).

Best Regards, VNN

Edit: an additional note or completeness:

Unless extended by custom code, connections have 2 internal registers:

these are already accessible by methods get_weights_at, get_weight_at, set_weight_at, set_misc_values_at (there is currently no _get_misc_valuesat but this could easily be added).

Similarly, unmodified, typical nodes (processing elements, pes) have:

These are accessible by methods such as get_output_from, set_output_at, get_input_at, input_at, set_misc_values_at (there is currently no _get_misc_valuesat but could easily be added), as well as the aforementioned new get_bias_atetc. which were added by your suggestion .

Thus almost all of the default internal registers can be accessed for both 'reading' and 'writing' operations.

VNNikolaidis commented 1 year ago

As of v.0.1.11 (available on GitHub - not yet released to CRAN) the missing get_misc_values_at was added. Along with the other methods for accessing internal registers of nodes and connections (such biases etc - see previous comment), all internal registers are now accessible.

A better notation for accessing internal registers may be added in the future (similar to what you suggest), but at least the functionality mentioned is now available. I hope this covers the issue, so I am closing it.

Regards. VNN

tpq commented 1 year ago

Thank you so much!