Class: DNN::Models::Sequential

Inherits:
Model show all
Defined in:
lib/dnn/core/models.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#last_log, #loss_weights, #optimizer

Instance Method Summary collapse

Methods inherited from Model

#add_callback, #add_lambda_callback, #built?, #call, #clean_layers, #clear_callbacks, #copy, #evaluate, #evaluate_by_iterator, #get_all_params_data, #get_layer, load, #load_params, #loss_func, #loss_func=, #predict, #predict1, #save, #save_params, #set_all_params_data, #setup, #test_on_batch, #to_cpu, #to_gpu, #train, #train_by_iterator, #train_on_batch, #trainable_layers

Methods inherited from Chain

#call, #layers, #load_hash, #to_hash

Constructor Details

#initialize(stack = []) ⇒ Sequential

Returns a new instance of Sequential.

Parameters:

  • stack (Array) (defaults to: [])

    All layers possessed by the model.



675
676
677
678
679
680
681
# File 'lib/dnn/core/models.rb', line 675

def initialize(stack = [])
  super()
  @stack = LayersList.new
  stack.each do |layer|
    add(layer)
  end
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



672
673
674
# File 'lib/dnn/core/models.rb', line 672

def stack
  @stack
end

Instance Method Details

#add(layer) ⇒ DNN::Models::Model Also known as: <<

Add layer to the model.

Parameters:

Returns:



686
687
688
689
690
691
692
693
694
695
# File 'lib/dnn/core/models.rb', line 686

def add(layer)
  if layer.is_a?(Layers::MergeLayer)
    raise TypeError, "layer: #{layer.class.name} should not be a DNN::Layers::MergeLayer class."
  end
  unless layer.is_a?(Layers::Layer) || layer.is_a?(Chain)
    raise TypeError, "layer: #{layer.class.name} is not an instance of the DNN::Layers::Layer class or DNN::Models::Chain class."
  end
  @stack << layer
  self
end

#forward(x) ⇒ Object



720
721
722
723
724
725
# File 'lib/dnn/core/models.rb', line 720

def forward(x)
  @stack.each do |layer|
    x = layer.(x)
  end
  x
end

#insert(index, layer) ⇒ DNN::Models::Model

Insert layer to the model by index position.

Parameters:

Returns:



702
703
704
705
706
707
708
709
710
711
# File 'lib/dnn/core/models.rb', line 702

def insert(index, layer)
  if layer.is_a?(Layers::MergeLayer)
    raise TypeError, "layer: #{layer.class.name} should not be a DNN::Layers::MergeLayer class."
  end
  unless layer.is_a?(Layers::Layer) || layer.is_a?(Chain)
    raise TypeError, "layer: #{layer.class.name} is not an instance of the DNN::Layers::Layer class or DNN::Models::Chain class."
  end
  @stack.insert(index, layer)
  self
end

#remove(layer) ⇒ Boolean

Remove layer to the model.

Parameters:

Returns:

  • (Boolean)

    Return true if success for remove layer.



716
717
718
# File 'lib/dnn/core/models.rb', line 716

def remove(layer)
  @stack.delete(layer) ? true : false
end