Class: Sabina::Layer::BaseLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/sabina/layer/base_layer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ BaseLayer

Returns a new instance of BaseLayer.



6
7
8
9
10
# File 'lib/sabina/layer/base_layer.rb', line 6

def initialize(size)
  @size = size
  @f = ->(x){ 1.0 / (1.0 + Math.exp(-x)) }
  @f_ = ->(x){ @f[x]*(1.0 - @f[x]) }
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



4
5
6
# File 'lib/sabina/layer/base_layer.rb', line 4

def b
  @b
end

#IObject

Returns the value of attribute I.



4
5
6
# File 'lib/sabina/layer/base_layer.rb', line 4

def I
  @I
end

#JObject

Returns the value of attribute J.



4
5
6
# File 'lib/sabina/layer/base_layer.rb', line 4

def J
  @J
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/sabina/layer/base_layer.rb', line 3

def size
  @size
end

#WObject

Returns the value of attribute W.



4
5
6
# File 'lib/sabina/layer/base_layer.rb', line 4

def W
  @W
end

Instance Method Details

#activate(u_ary) ⇒ Object

An activation function



24
25
26
# File 'lib/sabina/layer/base_layer.rb', line 24

def activate(u_ary)
  u_ary.map { |u| @f[u] }
end

#activate_(u_ary) ⇒ Object

Differentiation of activation function



29
30
31
# File 'lib/sabina/layer/base_layer.rb', line 29

def activate_(u_ary)
  u_ary.map { |u| @f_[u] }
end

#init_weightObject

Initialize the weights of this layer.



13
14
15
16
17
18
19
20
21
# File 'lib/sabina/layer/base_layer.rb', line 13

def init_weight
  # (J, 1)
  @b = Matrix.columns([Array.new(@size) { 0.0 }])

  # (J, I)
  @W = Array.new(@J) do
    Array.new(@I) { Sabina::Utils.box_muller }
  end.tap { |ary| break Matrix[*ary] }
end