neural.rb

Path: lib/charlie/list/neural.rb
Last Update: Tue Feb 12 15:45:34 +0100 2008

This file contains everything related to neural network genotypes

Methods

Constants

NN_TANH = proc{|x| Math.tanh x }   The tanh function is the default
NN_SIGN = proc{|x| x>=0?1:-1 }   The sign function is also commonly used

Public Instance methods

Genotype for neural networks with a single hidden layer.

  • Inherits FloatListGenotype
  • input_n, hidden_n, output_n are the number of neurons at each layer
  • scaling determines how the floats in genes relate to the weights of the links (important for mutation size, initial values).
    • input to hidden weights are (list element) multiplied by scaling / input_n
    • hidden to output weights are (list element) multiplied by scaling / hidden_n
  • hidden_f, output_f are the (usually sigmoidal) functions to determine the output of nodes.
    • Output of hidden node i is hidden_f( weights . input - threshold)

functions of the returned class:

  • output(input) - runs the neural network in some input

[Source]

    # File lib/charlie/list/neural.rb, line 19
19: def NeuralNetworkGenotype(input_n,hidden_n,output_n=1,scaling=1.0,hidden_f=NN_TANH,output_f=NN_TANH)
20:   links_n = hidden_n * (input_n + output_n)
21:   thr_n = hidden_n  + output_n
22:   Class.new(FloatListGenotype(links_n+thr_n, -1..1 )) {
23:     define_method(:output){|input|
24:       raise ArgumentError unless input.size==input_n
25:       si = scaling / input_n
26:       sh = scaling / hidden_n
27: 
28:       wts = genes
29:       input = input.map{|i| i * si } # scale inputs instead of weights: same effect, but faster
30: 
31:       hidden_val = Array.new(hidden_n,0.0)
32:       output_val = Array.new(output_n,0.0)
33:       (0...hidden_n).each{|i|
34:         wi    = i * input_n
35:         thr = wts[links_n + i]
36:         v = 0.0
37:         (0...input_n).each{|j| v += input[j] * wts[wi + j] }
38:         hidden_val[i] = hidden_f.call(v - thr) * sh # again, scale this instead of hidden->output weights
39:       }
40:       oi = hidden_n * input_n
41:       (0...output_n).each{|i|
42:         wi = oi + i * hidden_n
43:         thr = wts[links_n + hidden_n + i]
44:         v = 0.0
45:         (0...hidden_n).each{|j| v +=  hidden_val[j] * wts[wi + j] }
46:         output_val[i] = output_f.call(v - thr)
47:       }
48:       output_val
49:     }
50:     use UniformCrossover.dup
51:   }
52: end

[Validate]