Class: NeuralNetworkRb::NeuralNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/neural_network_rb/builder.rb,
lib/neural_network_rb/neural_network.rb

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(neurons_count, learning_rate, random_seed) ⇒ NeuralNetwork

Returns a new instance of NeuralNetwork.



23
24
25
26
27
28
# File 'lib/neural_network_rb/neural_network.rb', line 23

def initialize(neurons_count, learning_rate, random_seed)
  @neurons_count = neurons_count
  @learning_rate = learning_rate
  @epoch = 0
  Numo::NArray.srand(random_seed)
end

Instance Attribute Details

#epochObject (readonly)

Returns the value of attribute epoch.



5
6
7
# File 'lib/neural_network_rb/neural_network.rb', line 5

def epoch
  @epoch
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/neural_network_rb/neural_network.rb', line 5

def output
  @output
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/neural_network_rb/neural_network.rb', line 5

def target
  @target
end

Instance Method Details

#backpropObject



47
48
49
50
51
52
53
# File 'lib/neural_network_rb/neural_network.rb', line 47

def backprop
  # backward 
  dZ   = NeuralNetworkRb.plain_diff(@target,  @output) 
  @w2 += Numo::Linalg.matmul(@hidden.transpose, dZ) * @learning_rate
  dH   = Numo::Linalg.matmul(dZ, @w2.transpose) * NeuralNetworkRb.sigmoid_prime(@hidden)
  @w1 += Numo::Linalg.matmul(@input.transpose, dH) * @learning_rate
end

#fit {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



30
31
32
33
34
35
# File 'lib/neural_network_rb/neural_network.rb', line 30

def fit()
  forward
  backprop
  @epoch += 1 
  yield self if block_given?
end

#forwardObject



37
38
39
40
41
42
43
44
45
# File 'lib/neural_network_rb/neural_network.rb', line 37

def forward
  # forward
  @hidden = NeuralNetworkRb.sigmoid(
              Numo::Linalg.matmul(@input, @w1) + 
              @b1)
  @output = NeuralNetworkRb.softmax(
              Numo::Linalg.matmul(@hidden, @w2) + 
              @b2)
end

#input=(input) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/neural_network_rb/neural_network.rb', line 7

def input=(input)
  @input = input/255.0
  input_width = input.shape[1]
  @w1 = (Numo::DFloat.new(input_width,  @neurons_count).rand - 
         Numo::DFloat.ones(input_width,  @neurons_count)/2) * 0.01 / Math.sqrt(input_width)
  @b1 = Numo::DFloat.zeros(@neurons_count)
end

#predict(input) ⇒ Object



55
56
57
58
59
# File 'lib/neural_network_rb/neural_network.rb', line 55

def predict(input)
  hidden = NeuralNetworkRb.sigmoid(Numo::Linalg.matmul(input/255.0, @w1))
  output = NeuralNetworkRb.softmax(Numo::Linalg.matmul(hidden, @w2))
  output
end