Sha256: 2c48897e5941a3e82befcd2f98f77d4d5a5285c45fd644d26c49e3bddacd676a

Contents?: true

Size: 954 Bytes

Versions: 16

Compression:

Stored size: 954 Bytes

Contents

module DNN
  # This module provides utility functions.
  module Util
    # Create a mini batch for "batch_size".
    def self.get_minibatch(x, y, batch_size)
      indexes = (0...x.shape[0]).to_a.sample(batch_size)
      [x[indexes, false], y[indexes, false]]
    end

    # Categorize labels into "num_classes" classes.
    def self.to_categorical(y, num_classes, narray_type = nil)
      narray_type ||= y.class
      y2 = narray_type.zeros(y.shape[0], num_classes)
      y.shape[0].times do |i|
        y2[i, y[i]] = 1
      end
      y2
    end
  
    # Perform numerical differentiation.
    def self.numerical_grad(x, func)
      (func.(x + 1e-7) - func.(x)) / 1e-7
    end

    # Convert hash to an object.
    def self.load_hash(hash)
      dnn_class = DNN.const_get(hash[:name])
      if dnn_class.respond_to?(:load_hash)
        return dnn_class.load_hash(hash)
      end
      dnn_class.new
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby-dnn-0.5.12 lib/dnn/core/util.rb
ruby-dnn-0.5.11 lib/dnn/core/util.rb
ruby-dnn-0.5.10 lib/dnn/core/util.rb
ruby-dnn-0.5.9 lib/dnn/core/util.rb
ruby-dnn-0.5.8 lib/dnn/core/util.rb
ruby-dnn-0.5.7 lib/dnn/core/util.rb
ruby-dnn-0.5.6 lib/dnn/core/util.rb
ruby-dnn-0.5.5 lib/dnn/core/util.rb
ruby-dnn-0.5.4 lib/dnn/core/util.rb
ruby-dnn-0.5.3 lib/dnn/core/util.rb
ruby-dnn-0.5.2 lib/dnn/core/util.rb
ruby-dnn-0.5.1 lib/dnn/core/util.rb
ruby-dnn-0.5.0 lib/dnn/core/util.rb
ruby-dnn-0.4.4 lib/dnn/core/util.rb
ruby-dnn-0.4.3 lib/dnn/core/util.rb
ruby-dnn-0.4.2 lib/dnn/core/util.rb