Learn R Programming

nnlib2Rcpp (version 0.1.8)

NN-class: Class "NN"

Description

NN module, for defining and manipulating custom neural networks.

Arguments

Extends

Class "'>RcppClass", directly.

All reference classes extend and inherit methods from "'>envRefClass".

Fields

.CppObject:

Object of class C++Object ~~

.CppClassDef:

Object of class activeBindingFunction ~~

.CppGenerator:

Object of class activeBindingFunction ~~

Methods

add_layer( name, size ):

Setup a new layer component (a layer of processing nodes) and append it to the NN topology. Parameters are:

  • name: string, containing name (that also Specifies type) of new layer. Names of predefined layers currently include 'pe'(same as 'generic'), 'pass-through', 'which-max', 'MAM', 'LVQ-input', 'LVQ-output', 'BP-hidden', 'BP-output', 'perceptron' (additional names for user-defined components may be used, see note below.)

  • size: integer, layer size i.e. number of pe (Processing Elements or nodes) to create in the layer.

add_connection_set( name ):

Create a new empty connection_set component (a set of connections between two layers). It does not connect any layers nor contain any connections between specific layer nodes. The set is appended to the NN topology. Parameters are:

  • name: string, containing name (that also specifies type) of new empty connection set. Names of predefined connection sets currently include 'generic', 'pass-through'(which does not multiply weights), 'wpass-through'(which does multiply weights), 'MAM', 'LVQ', 'BP', 'perceptron' (additional names for user-defined components may be used, see note below).

create_connections_in_sets( min_random_weight, max_random_weight ):

Find empty, unconnected connection_set components that are between two layers in the topology, and set them up to connect the adjacent layers, adding connections to fully connect their nodes (n x m connections are created, with n and m the number of nodes at each layer respectively). Parameters are:

  • min_random_weight: double, minimum value for random initial connection weights.

  • max_random_weight: double, maximum value for random initial connection weights.

connect_layers_at( source_pos, destin_pos, name ):

Insert a new empty connection_set component (a set of connections between two layers) between the layers at specified topology positions, and prepare it to connect them. No actual connections between any layer nodes are created. Parameters are:

  • source_pos: integer, position in topology of source layer.

  • destin_pos: integer, position in topology of destination layer.

  • name: string, containing name (that also specifies type) of new connection set (see above).

fully_connect_layers_at( source_pos, destin_pos, name, min_random_weight, max_random_weight ):

Same as connect_layers_at but also fills the new connection_set with connections between the nodes of the two layers, fully connecting the layers (n x m connections are created, with n and m the number of nodes at each layer respectively). Parameters are:

  • source_pos: integer, position in topology of source layer.

  • destin_pos: integer, position in topology of destination layer.

  • name: string, containing name (that also specifies type) of new connection set (see above).

  • min_random_weight: double, minimum value for random initial connection weights.

  • max_random_weight: double, maximum value for random initial connection weights.

add_single_connection( pos, source_pe, destin_pe, weight ):

Add a connection to a connection_set that already connects two layers. Parameters are:

  • pos: integer, position in topology of connection_set to which the new connection will be added.

  • source_pe: integer, pe in source layer to connect.

  • destin_pe: integer, pe in destination layer to connect.

  • weight: double, value for initial connection weight.

remove_single_connection( pos, con ):

Remove a connection from a connection_set. Parameters are:

  • pos: integer, position in topology of connection_set.

  • con: integer, connection to remove.

size():

Returns neural network size, i.e. the number of components its topology.

sizes():

Returns sizes of components in topology.

component_ids():

Returns an integer vector containing the ids of the components in topology (these ids are created at run-time and identify each NN component).

input_at( pos, data_in ):

Input a data vector to the component (layer) at specified topology index. Returns TRUE if successful. Parameters are:

  • pos: integer, position in topology of component to receive input.

  • data_in: NumericVector, data to be sent as input to component (sizes must match).

encode_at( pos ):

Trigger the encoding operation of the component at specified topology index (note: depending on implementation, an 'encode' operation usually collects inputs, processes the data, adjusts internal state variables and/or weights, and possibly produces output). Returns TRUE if successful. Parameters are:

  • pos: integer, position in topology of component to perform encoding.

recall_at( pos ):

Trigger the recall (mapping, data retrieval) operation of the component at specified topology index (note: depending on implementation, a 'recall' operation usually collects inputs, processes the data, and produces output). Returns TRUE if successful. Parameters are:

  • pos: integer, position in topology of component to perform recall.

encode_all( fwd ):

Trigger the encoding operation of all the components in the NN topology. Returns TRUE if successful. Parameters are:

  • fwd: logical, set to TRUE to encode forwards (first-to-last component), FALSE to encode backwards (last-to-first component).

encode_dataset_unsupervised( data, pos, epochs, fwd ):

Encode a dataset using unsupervised training. A faster method to encode a data set. All the components in the NN topology will perform 'encode' in specified direction. Returns TRUE if successful. Parameters are:

  • data: numeric matrix, containing input vectors as rows.

  • pos: integer, position in topology of component to receive input vectors.

  • epochs: integer, number of training epochs (encoding repetitions of the entire dataset).

  • fwd: logical, indicates direction, TRUE to trigger encoding forwards (first-to-last component), FALSE to encode backwards (last-to-first component).

encode_datasets_supervised( i_data, i_pos, j_data, j_pos, j_destination_register, epochs, fwd ):

Encode multiple (i,j) vector pairs stored in two corresponding data sets, using supervised training. A faster method to encode the data. All the components in the NN topology will perform 'encode' in specified direction. Returns TRUE if successful. Parameters are:

  • i_data: numeric matrix, data set, each row is a vector i of vector-pair (i,j).

  • i_pos: integer, position in topology of component to receive i vectors.

  • j_data: numeric matrix, data set, each row is a corresponding vector j of vector-pair (i,j).

  • j_pos: integer, position in topology of component to receive j vectors.

  • j_destination_selector: integer, selects which internal node (pe) registers will receive vector j, i.e. if 0 internal node register 'input' will be used (j will become the layer's input), if 1 register 'output' will be used (j will become the layer's output), if 2 register 'misc' will be used (implementations may use this as an alternative way to transfer data to nodes without altering current input or output).

  • epochs: integer, number of training epochs (encoding repetitions of the entire data).

  • fwd: logical, indicates direction, TRUE to trigger encoding forwards (first-to-last component), FALSE to encode backwards (last-to-first component).

recall_dataset( data_in, input_pos, output_pos, fwd ):

Recall (map, retrieve output for) a dataset. A faster method to recall an entire data set. All the components in the NN topology will perform 'recall' in specified direction. Returns numeric matrix containing corresponding output. Parameters are:

  • data_in: numeric matrix, containing input vectors as rows.

  • input_pos: integer, position in topology of component to receive input vectors.

  • output_pos: integer, position in topology of component to produce output.

  • fwd: logical, indicates direction, TRUE to trigger 'recall' (mapping) forwards (first-to-last component), FALSE to recall backwards (last-to-first component).

recall_all( fwd ):

Trigger the recall (mapping, data retrieval) operation of all the components in the NN topology. Returns TRUE if successful. Parameters are:

  • fwd: logical, set to TRUE to recall forwards (first-to-last component), FALSE to recall backwards (last-to-first component).

get_output_from( pos ):

Get the current output of the component at specified topology index. If successful, returns NumericVector of output values. Parameters are:

  • pos: integer, position in topology of component to use.

get_output_at( pos ):

Same as get_output_from, see above.

get_input_at( pos ):

Get the current input of the component at specified topology index (depends on the implementation: for layers, this may be valid after the pes (nodes) have performed their input_function on incoming values; pes have an overridable input_function that collects all incoming values and produces a single value for further processing which is stored at an internal input register (whose value is retrieved here); by default input_function performs summation. If successful, returns NumericVector of final input values. Parameters are:

  • pos: integer, position in topology of component to use.

get_weights_at( pos ):

Get the current weights of the component (connection_set) at specified topology index. If successful, returns NumericVector of connection weights. Parameters are:

  • pos: integer, position in topology of component to use.

get_weight_at( pos, connection ):

Get the current weight of a connection in component (connection_set) at specified topology index. If successful, returns weight, otherwise 0. Parameters are:

  • pos: integer, position in topology of component to use.

  • connection: connection to use.

set_weight_at( pos, connection, value ):

Set the weight of a connection in component (connection_set) at specified topology index. If successful, returns TRUE. Parameters are:

  • pos: integer, position in topology of component to use.

  • connection: connection to use.

  • value: new weight for connection.

set_misc_values_at( pos, data_in ):

Set the values in the misc data register that pe and connection objects maintain, for objects at specified topology index. If successful, returns TRUE. Parameters are:

  • pos: integer, position in topology of component to use.

  • data_in: NumericVector, data to be used for new values in misc registers (sizes must match).

set_output_at( pos, data_in ):

Set the values in the output data register that pe objects maintain, for layer at specified topology index (currenly only layer components are supported). If successful, returns TRUE. Parameters are:

  • pos: integer, position in topology of component to use.

  • data_in: NumericVector, data to be used for new values in misc registers (sizes must match).

print( ):

Print internal NN state, including all components in topology.

outline( ):

Print a summary description of all components in topology.

The following methods are inherited (from the corresponding class): objectPointer ("RcppClass"), initialize ("RcppClass"), show ("RcppClass")

See Also

BP, LVQs, MAM.

Examples

Run this code
# NOT RUN {
# Example 1:

# (1.A) create new 'NN' object:

n <- new("NN")

# (1.B) Add topology components:

# 1. add a layer of 4 generic nodes:
n$add_layer("generic",4)
# 2. add a set for connections that pass data unmodified:
n$add_connection_set("pass-through")
# 3. add another layer of 2 generic nodes:
n$add_layer("generic",2)
# 4. add a set for connections that pass data x weight:
n$add_connection_set("wpass-through")
# 5. add a layer of 1 generic node:
n$add_layer("generic",1)
# Create actual full connections in sets, random initial weights in [0,1]:
n$create_connections_in_sets(0,1)
# Optionaly, show an outline of the topology:
n$outline()

# (1.C) use the network.

# input some data, and create output for it:
n$input_at(1,c(10,20,30,40))
n$recall_all(TRUE)
# the final output:
n$get_output_from(5)

# (1.D) optionally, examine the network:

# the input at first layer at position 1:
n$get_input_at(1)
# Data is passed unmodified through connections at position 2,
# and (by default) summed together at each node of layer at position 3.
# Final output from layer in position 3:
n$get_output_from(3)
# Data is then passed multiplied by the random weights through
# connections at position 4. The weights of these connections:
n$get_weights_at(4)
# Data is finally summed together at the node of layer at position 5,
# producing the final output, which (again) is:
n$get_output_from(5)

# Example 2: A simple MAM NN

# (2.A) Preparation:

# Create data pairs

iris_data    <- as.matrix( scale( iris[1:4] ) )
iris_species <- matrix(data=-1, nrow=nrow(iris_data), ncol=3)
for(r in 1:nrow(iris_data))
 iris_species[r ,as.integer( iris$Species )[r]]=1

# Create the NN and its components:

m <- new( "NN" )
m$add_layer( "generic" , 4 )
m$add_layer( "generic" , 3 )
m$fully_connect_layers_at(1, 2, "MAM", 0, 0)

# (2.B) Use the NN to store iris (data,species) pair:

# encode pairs in NN:

m$encode_datasets_supervised(
	iris_data,1,
	iris_species,3,0,
	1,TRUE)

# (2.C) Recall iris species from NN:

recalled_data <- m$recall_dataset(iris_data,1,3,TRUE)

# (2.D) Convert recalled data to ids and plot results:

recalled_ids <- apply(recalled_data, 1, which.max)
plot(iris_data, pch=recalled_ids)
# }

Run the code above in your browser using DataLab