Sha256: c77bb5c88d8f49c7953207c7f8cbd0a159841359365a0308b34943b25598535a

Contents?: true

Size: 1.47 KB

Versions: 10

Compression:

Stored size: 1.47 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 = {name: 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)
        prev_dim = layer.prev_layer.shape.reduce(:*)
        super(layer, param_key, layer.params[param_key].rand_norm / Math.sqrt(prev_dim))
      end
    end
    
    
    class He < Initializer
      def init_param(layer, param_key)
        prev_dim = layer.prev_layer.shape.reduce(:*)
        super(layer, param_key, layer.params[param_key].rand_norm / Math.sqrt(prev_dim) * Math.sqrt(2))
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ruby-dnn-0.5.12 lib/dnn/core/initializers.rb
ruby-dnn-0.5.11 lib/dnn/core/initializers.rb
ruby-dnn-0.5.10 lib/dnn/core/initializers.rb
ruby-dnn-0.5.9 lib/dnn/core/initializers.rb
ruby-dnn-0.5.8 lib/dnn/core/initializers.rb
ruby-dnn-0.5.7 lib/dnn/core/initializers.rb
ruby-dnn-0.5.6 lib/dnn/core/initializers.rb
ruby-dnn-0.5.5 lib/dnn/core/initializers.rb
ruby-dnn-0.5.4 lib/dnn/core/initializers.rb
ruby-dnn-0.5.3 lib/dnn/core/initializers.rb