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, #call_callbacks, #check_early_stop_requested, #clean_layers, #clear_callbacks, #copy, #evaluate, #evaluate_by_iterator, #get_all_params_data, #get_all_trainable_params, #get_layer, load, #load_params, #loss_func, #loss_func=, #predict, #predict1, #request_early_stop, #save, #save_params, #set_all_params_data, #setup, #test_on_batch, #test_step, #to_cpu, #to_gpu, #train, #train_by_iterator, #train_on_batch, #train_step, #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.



1000
1001
1002
1003
1004
1005
1006
# File 'lib/dnn/core/models.rb', line 1000

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.



997
998
999
# File 'lib/dnn/core/models.rb', line 997

def stack
  @stack
end

Instance Method Details

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

Add layer to the model.

Parameters:

Returns:



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/dnn/core/models.rb', line 1011

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



1045
1046
1047
1048
1049
1050
# File 'lib/dnn/core/models.rb', line 1045

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:



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/dnn/core/models.rb', line 1027

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.



1041
1042
1043
# File 'lib/dnn/core/models.rb', line 1041

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