Class: Micrograd::MLP

Inherits:
Object
  • Object
show all
Defined in:
lib/micrograd/mlp.rb

Overview

Multi layer perceptron

MLP.new(input_size: 3, layer_sizes: [4, 4, 1])

In the above example, we have 3 inputs, an we have 3 layers, two layers of 4, and one output layer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_size:, layer_sizes:) ⇒ MLP

Returns a new instance of MLP.



12
13
14
15
16
# File 'lib/micrograd/mlp.rb', line 12

def initialize(input_size:, layer_sizes:)
  @layers = [input_size, *layer_sizes]
    .each_cons(2)
    .map { |x, y| Layer.new(x, y) }
end

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



18
19
20
# File 'lib/micrograd/mlp.rb', line 18

def layers
  @layers
end

Instance Method Details

#call(xs) ⇒ Object



20
21
22
23
# File 'lib/micrograd/mlp.rb', line 20

def call(xs)
  @layers.each { |layer| xs = layer.call(xs) }
  xs
end

#parametersObject



25
# File 'lib/micrograd/mlp.rb', line 25

def parameters = layers.flat_map(&:parameters)