Class: MachineLearningWorkbench::NeuralNetwork::Base
- Inherits:
-
Object
- Object
- MachineLearningWorkbench::NeuralNetwork::Base
- Defined in:
- lib/machine_learning_workbench/neural_network/base.rb
Overview
Neural Network base class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#act_fn ⇒ #call
readonly
activation function, common to all neurons (for now).
-
#dtype ⇒ Object
readonly
Returns the value of attribute dtype.
-
#layers ⇒ Array<NMatrix>
readonly
List of matrices, each being the weights connecting a layer’s inputs (rows) to a layer’s neurons (columns), hence its shape is ‘[ninputs, nneurs]`.
-
#state ⇒ Array<NMatrix>
readonly
It’s a list of one-dimensional matrices, each an input to a layer, plus the output layer’s output.
-
#struct ⇒ Array<Integer>
readonly
list of number of (inputs or) neurons in each layer.
Instance Method Summary collapse
-
#activate(input) ⇒ Array
Activate the network on a given input.
-
#bias ⇒ Object
The “fixed ‘1`” used in the layer’s input.
-
#deep_reset ⇒ Object
Resets memoization: needed to play with structure modification.
-
#get_act_fn(type, *args) ⇒ NMatrix
Activation function caller.
-
#init_random ⇒ Object
Initialize the network with random weights.
-
#initialize(struct, act_fn: nil, dtype: :float32) ⇒ Base
constructor
A new instance of Base.
-
#interface_methods ⇒ Object
Declaring interface methods - implement in child class!.
-
#layer_col_sizes ⇒ Array
Number of neurons per layer.
-
#layer_shapes ⇒ Array<Array[Integer, Integer]>
Shapes for the weight matrices, each corresponding to a layer.
-
#lecun_hyperbolic ⇒ Object
LeCun hyperbolic activation.
-
#load_weights(weights) ⇒ true
Loads a plain list of weights into the weight matrices (one per layer).
-
#logistic ⇒ Object
Traditional logistic.
-
#nlayers ⇒ Integer
Count the layers.
-
#nneurs(nlay = nil) ⇒ Integer
Count the neurons in a particular layer or in the whole network.
-
#nweights ⇒ Integer
Total weights in the network.
-
#nweights_per_layer ⇒ Array<Integer>
List of per-layer number of weights.
-
#out ⇒ Array
Extract and convert the output layer’s activation.
-
#relu ⇒ Object
Rectified Linear Unit (ReLU).
-
#reset_state ⇒ Object
Reset the network to the initial state.
-
#sigmoid(k = 0.5) ⇒ Object
Traditional sigmoid with variable steepness.
-
#weights ⇒ Array
Returns the weight matrix.
Constructor Details
#initialize(struct, act_fn: nil, dtype: :float32) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 28 def initialize struct, act_fn: nil, dtype: :float32 @struct = struct @act_fn = self.get_act_fn(act_fn || :sigmoid) # @state holds both inputs, possibly recurrency, and bias # it is a complete input for the next layer, hence size from layer sizes @state = layer_row_sizes.collect do |size| NMatrix.zeros([1, size], dtype: dtype) end # to this, append a matrix to hold the final network output @state.push NMatrix.zeros([1, nneurs(-1)], dtype: dtype) reset_state end |
Instance Attribute Details
#act_fn ⇒ #call (readonly)
activation function, common to all neurons (for now)
20 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 20 attr_reader :layers, :state, :act_fn, :struct, :dtype |
#dtype ⇒ Object (readonly)
Returns the value of attribute dtype.
20 21 22 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 20 def dtype @dtype end |
#layers ⇒ Array<NMatrix> (readonly)
List of matrices, each being the weights connecting a layer’s inputs (rows) to a layer’s neurons (columns), hence its shape is ‘[ninputs, nneurs]`
20 21 22 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 20 def layers @layers end |
#state ⇒ Array<NMatrix> (readonly)
It’s a list of one-dimensional matrices, each an input to a layer, plus the output layer’s output. The first element is the input to the first layer of the network, which is composed of the network’s input, possibly the first layer’s activation on the last input (recursion), and a bias (fixed ‘1`). The second to but-last entries follow the same structure, but with the previous layer’s output in place of the network’s input. The last entry is the activation of the output layer, without additions since it’s not used as an input by anyone.
20 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 20 attr_reader :layers, :state, :act_fn, :struct, :dtype |
#struct ⇒ Array<Integer> (readonly)
list of number of (inputs or) neurons in each layer
20 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 20 attr_reader :layers, :state, :act_fn, :struct, :dtype |
Instance Method Details
#activate(input) ⇒ Array
Activate the network on a given input
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 150 def activate input raise ArgumentError unless input.size == struct.first raise ArgumentError unless input.is_a? Array # load input in first state @state[0][0, 0..-2] = input # activate layers in sequence (0...nlayers).each do |i| act = activate_layer i @state[i+1][0,0...act.size] = act end return out end |
#bias ⇒ Object
The “fixed ‘1`” used in the layer’s input
143 144 145 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 143 def bias @bias ||= NMatrix[[1], dtype: dtype] end |
#deep_reset ⇒ Object
Resets memoization: needed to play with structure modification
61 62 63 64 65 66 67 68 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 61 def deep_reset # reset memoization [:@layer_row_sizes, :@layer_col_sizes, :@nlayers, :@layer_shapes, :@nweights_per_layer, :@nweights].each do |sym| instance_variable_set sym, nil end reset_state end |
#get_act_fn(type, *args) ⇒ NMatrix
Activation function caller. Allows to cleanly define the activation function as one-dimensional, by calling it over the inputs and building a NMatrix to return.
175 176 177 178 179 180 181 182 183 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 175 def get_act_fn type, *args fn = send(type,*args) lambda do |inputs| NMatrix.new([1, inputs.size], dtype: dtype) do |_,i| # single-row matrix, indices are columns fn.call inputs[i] end end end |
#init_random ⇒ Object
Initialize the network with random weights
52 53 54 55 56 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 52 def init_random # Will only be used for testing, no sense optimizing it (NMatrix#rand) # Reusing #load_weights instead helps catching bugs load_weights nweights.times.collect { rand(-1.0..1.0) } end |
#interface_methods ⇒ Object
Declaring interface methods - implement in child class!
214 215 216 217 218 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 214 [:layer_row_sizes, :activate_layer].each do |sym| define_method sym do raise NotImplementedError, "Implement ##{sym} in child class!" end end |
#layer_col_sizes ⇒ Array
Number of neurons per layer. Although this implementation includes inputs in the layer counts, this methods correctly ignores the input as not having neurons.
100 101 102 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 100 def layer_col_sizes @layer_col_sizes ||= struct.drop(1) end |
#layer_shapes ⇒ Array<Array[Integer, Integer]>
Shapes for the weight matrices, each corresponding to a layer
108 109 110 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 108 def layer_shapes @layer_shapes ||= layer_row_sizes.zip layer_col_sizes end |
#lecun_hyperbolic ⇒ Object
LeCun hyperbolic activation
202 203 204 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 202 def lecun_hyperbolic lambda { |x| 1.7159 * Math.tanh(2.0*x/3.0) + 1e-3*x } end |
#load_weights(weights) ⇒ true
Loads a plain list of weights into the weight matrices (one per layer). Preserves order. Reuses allocated memory if available.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 126 def load_weights weights raise ArgumentError unless weights.size == nweights weights_iter = weights.each @layers ||= layer_shapes.collect { |shape| NMatrix.new shape, dtype: dtype } layers.each do |nmat| nmat.each_with_indices do |_val, *idxs| nmat[*idxs] = weights_iter.next end end reset_state return true end |
#logistic ⇒ Object
Traditional logistic
193 194 195 196 197 198 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 193 def logistic lambda { |x| exp = Math.exp(x) exp.infinite? ? exp : exp / (1.0 + exp) } end |
#nlayers ⇒ Integer
Count the layers. This is a computation helper, and for this implementation the inputs are considered as if a layer like the others.
85 86 87 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 85 def nlayers @nlayers ||= layer_shapes.size end |
#nneurs(nlay = nil) ⇒ Integer
Count the neurons in a particular layer or in the whole network.
117 118 119 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 117 def nneurs nlay=nil nlay.nil? ? struct.reduce(:+) : struct[nlay] end |
#nweights ⇒ Integer
Total weights in the network
72 73 74 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 72 def nweights @nweights ||= nweights_per_layer.reduce(:+) end |
#nweights_per_layer ⇒ Array<Integer>
List of per-layer number of weights
78 79 80 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 78 def nweights_per_layer @nweights_per_layer ||= layer_shapes.collect { |shape| shape.reduce(:*) } end |
#out ⇒ Array
Extract and convert the output layer’s activation
165 166 167 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 165 def out state.last.to_flat_a end |
#relu ⇒ Object
Rectified Linear Unit (ReLU)
207 208 209 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 207 def relu lambda { |x| x>0 && x || 0 } end |
#reset_state ⇒ Object
Reset the network to the initial state
42 43 44 45 46 47 48 49 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 42 def reset_state @state.each do |m| # state has only single-row matrices # reset all to zero m[0,0..-1] = 0 # add bias to all but output m[0,-1] = 1 unless m.object_id == @state.last.object_id end end |
#sigmoid(k = 0.5) ⇒ Object
Traditional sigmoid with variable steepness
186 187 188 189 190 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 186 def sigmoid k=0.5 # k is steepness: 0<k<1 is flatter, 1<k is flatter # flatter makes activation less sensitive, better with large number of inputs lambda { |x| 1.0 / (Math.exp(-k * x) + 1.0) } end |
#weights ⇒ Array
Returns the weight matrix
92 93 94 |
# File 'lib/machine_learning_workbench/neural_network/base.rb', line 92 def weights layers.collect(&:to_consistent_a) end |