Class: Neuronet::FeedForward

Inherits:
Object
  • Object
show all
Includes:
Arrayable, Exportable, NetworkStats, Trainable
Defined in:
lib/neuronet/feed_forward.rb

Overview

FeedForward is a fully connected neural network with >= 3 layers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arrayable

#[], #each, #each_with_index, #map, #reverse, #size

Methods included from Trainable

#pairs, #pivot, #train

Methods included from Exportable

#export, #export_to_file, #import, #import_from_file

Methods included from NetworkStats

#expected_nju, #expected_nju!, #njus

Constructor Details

#initialize(*sizes, full_neuron: Neuron) ⇒ FeedForward

Example:

ff = Neuronet::FeedForward.new(4, 8, 4)


17
18
19
20
21
22
23
24
25
26
# File 'lib/neuronet/feed_forward.rb', line 17

def initialize(*sizes, full_neuron: Neuron)
  length = sizes.length
  raise 'Need at least 3 layers' if length < 3

  @layers = Array.new(length) { Layer.new(sizes[it], full_neuron:) }
  1.upto(length - 1) { @layers[it].connect(@layers[it - 1]) }
  @input_layer = @layers[0]
  @output_layer = @layers[-1]
  @hidden_layers = @layers[1...-1]
end

Instance Attribute Details

#hidden_layersObject (readonly)

Returns the value of attribute hidden_layers.



28
29
30
# File 'lib/neuronet/feed_forward.rb', line 28

def hidden_layers
  @hidden_layers
end

#input_layerObject (readonly)

Returns the value of attribute input_layer.



28
29
30
# File 'lib/neuronet/feed_forward.rb', line 28

def input_layer
  @input_layer
end

#output_layerObject (readonly)

Returns the value of attribute output_layer.



28
29
30
# File 'lib/neuronet/feed_forward.rb', line 28

def output_layer
  @output_layer
end

Instance Method Details

#*(other) ⇒ Object

Forward pass: set input, update, return output.



46
47
48
49
50
# File 'lib/neuronet/feed_forward.rb', line 46

def *(other)
  set(other)
  update
  values
end

#set(values) ⇒ Object

Sets the input values



31
32
33
# File 'lib/neuronet/feed_forward.rb', line 31

def set(values)
  @input_layer.set(values)
end

#to_aObject



52
# File 'lib/neuronet/feed_forward.rb', line 52

def to_a = @layers

#updateObject

Updates hidden layers (input assumed set).



36
37
38
# File 'lib/neuronet/feed_forward.rb', line 36

def update
  @hidden_layers.each(&:update)
end

#valuesObject

Gets output



41
42
43
# File 'lib/neuronet/feed_forward.rb', line 41

def values
  @output_layer.values
end