Struct revonet::neuro::MultilayeredNetwork
[−]
[src]
pub struct MultilayeredNetwork { /* fields omitted */ }
Representation of multilayered neural network with linear activation on output and arbitrary activations for hidden layers.
Methods
impl MultilayeredNetwork
[src]
fn new(inputs_num: usize, outputs_num: usize) -> MultilayeredNetwork
Create a new multilayered network object with given number of inputs and outputs.
The resulting network is not initialized. To add hidden layeres use add_hidden_layer
and
then call build
function to finalize structure and weights initialization.
Arguments:
inputs_num
- number of input nodes.outputs_num
- number of output nodes.
fn from_layers<R: Rng + Sized>(
layers: &[u32],
acts: &[ActivationFunctionType],
arch: NeuralArchitecture,
rng: &mut R
) -> MultilayeredNetwork
layers: &[u32],
acts: &[ActivationFunctionType],
arch: NeuralArchitecture,
rng: &mut R
) -> MultilayeredNetwork
Create and initialize a new neural network object given sizes of layers (including input and output ones) and activation types.
The resulting network will have a linear activation in the output layer.
Arguments:
layers
- array containing sizes of input (layers[0]
), output (layers[layers.len()-1]
) and hidden layers (layers[1:layers.len()-1]
).acts
- array of activations for hidden layers. Note thatk
-th hidden layers will havek
-th activation!
fn len(&self) -> usize
Number of hidden+output layers in the neural network.
Add a hidden layer to an uninitialized network.
Panics if the network has already been initialized .
Arguments:
size
- number of nodes in a layer.actf
- type of activation function.
fn build<R: Rng + Sized>(&mut self, rng: &mut R, arch: NeuralArchitecture)
Finalize building neural network and initialize its weights.
Arguments:
rng
- mutable reference to the external RNG.
fn get_weights(&self) -> (Vec<Vec<f32>>, Vec<Vec<f32>>)
Returns tuple containing weights and biases layer by layer.
Return:
result.0
- matrix of weights.k
-th row corresponds to the flattened vector of weights fork
-th layer.result.1
- matrix of biases.k
-th row corresponds to the vector of biases fork
-th layer.
Example: Multilayered network with sigmoid activations:
extern crate rand; use rand::{Rng}; extern crate revonet; use revonet::neuro::*; fn main () { const INPUT_SIZE: usize = 20; const OUTPUT_SIZE: usize = 2; let mut rng = rand::thread_rng(); // needed for weights initialization when NN is built. let mut net: MultilayeredNetwork = MultilayeredNetwork::new(INPUT_SIZE, OUTPUT_SIZE); net.add_hidden_layer(30 as usize, ActivationFunctionType::Sigmoid) .add_hidden_layer(20 as usize, ActivationFunctionType::Sigmoid) .build(&mut rng, NeuralArchitecture::Multilayered); // `build` finishes creation of neural network. let (ws, bs) = net.get_weights(); // `ws` and `bs` are `Vec` arrays containing weights and biases for each layer. assert!(ws.len() == 3); // number of elements equals to number of hidden layers + 1 output layer assert!(bs.len() == 3); // number of elements equals to number of hidden layers + 1 output layer let rnd_input = (0..INPUT_SIZE).map(|_| rng.gen::<f32>()).collect::<Vec<f32>>(); println!("NN outputs: {:?}", net.compute(&rnd_input)); }
fn set_weights(&mut self, wss: &Vec<Vec<f32>>, bss: &Vec<Vec<f32>>)
Sets weights and biases layer by layer.
See description for the get_weights
for more details and example.
Arguments:
wss
- matrix of weights.wss[k]
flattened vector of weights for thek
-th layer.bss
- matrix of biases.bss[k]
flattened vector of biases for thek
-th layer.
fn iter_layers(&self) -> Iter<Box<NeuralLayer>>
Returns iterator for layers.
Trait Implementations
impl Debug for MultilayeredNetwork
[src]
impl NeuralNetwork for MultilayeredNetwork
[src]
fn get_inputs_num(&self) -> usize
Returns number of input nodes.
fn get_outputs_num(&self) -> usize
Returns number of output nodes.
fn compute(&mut self, xs: &[f32]) -> Vec<f32>
Compute output of neural network for a given input vector. Read more
impl Clone for MultilayeredNetwork
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more