Sha256: 4926ca4a78d72f2530a1094de9d29b20cec410690e6b53d970eab80195364b93

Contents?: true

Size: 594 Bytes

Versions: 1

Compression:

Stored size: 594 Bytes

Contents

# frozen_string_literal: true

module Micrograd
  # 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.
  #
  class MLP
    def initialize(input_size:, layer_sizes:)
      @layers = [input_size, *layer_sizes]
        .each_cons(2)
        .map { |x, y| Layer.new(x, y) }
    end

    attr_reader :layers

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

    def parameters = layers.flat_map(&:parameters)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micrograd-0.1.0 lib/micrograd/mlp.rb