Sha256: a4194d2637edf5d689d776e02454953e98a821c0dd3d15ed7ff98ceb77f6ceb1

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module DNN
  module Initializers

    class Initializer
      def init_param(layer, param_key, param)
        layer.params[param_key] = param
      end

      def to_hash(merge_hash = nil)
        hash = {class: self.class.name}
        hash.merge!(merge_hash) if merge_hash
        hash
      end
    end


    class Zeros < Initializer
      def init_param(layer, param_key)
        super(layer, param_key, layer.params[param_key].fill(0))
      end
    end
    
    
    class RandomNormal < Initializer
      attr_reader :mean
      attr_reader :std
      
      def self.load_hash(hash)
        self.new(hash[:mean], hash[:std])
      end

      def initialize(mean = 0, std = 0.05)
        @mean = mean
        @std = std
      end

      def init_param(layer, param_key)
        super(layer, param_key, layer.params[param_key].rand_norm(@mean, @std))
      end

      def to_hash
        super({mean: @mean, std: @std})
      end
    end
    
    
    class Xavier < Initializer
      def init_param(layer, param_key)
        num_prev_nodes = layer.prev_layer.shape.reduce(:*)
        super(layer, param_key, layer.params[param_key].rand_norm / Math.sqrt(num_prev_nodes))
      end
    end
    
    
    class He < Initializer
      def init_param(layer, param_key)
        num_prev_nodes = layer.prev_layer.shape.reduce(:*)
        super(layer, param_key, layer.params[param_key].rand_norm / Math.sqrt(num_prev_nodes) * Math.sqrt(2))
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-dnn-0.6.9 lib/dnn/core/initializers.rb
ruby-dnn-0.6.8 lib/dnn/core/initializers.rb
ruby-dnn-0.6.7 lib/dnn/core/initializers.rb
ruby-dnn-0.6.6 lib/dnn/core/initializers.rb
ruby-dnn-0.6.5 lib/dnn/core/initializers.rb