Sha256: fa71ade72e123fe94fea48c5ff21e79a6c2530149e61eaaac5290d84882c27ae

Contents?: true

Size: 983 Bytes

Versions: 5

Compression:

Stored size: 983 Bytes

Contents

module DNN
  # This module provides utility functions.
  module Utils
    # Categorize labels into "num_classes" classes.
    # @param [Numo::SFloat] y Label data.
    # @param [Numo::SFloat] num_classes Number of classes to classify.
    # @param [Class] narray_type Type of Numo::Narray data after classification.
    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

    # Convert hash to an object.
    def self.hash_to_obj(hash)
      return nil if hash == nil
      dnn_class = DNN.const_get(hash[:class])
      dnn_class.from_hash(hash)
    end

    # Return the result of the sigmoid function.
    def self.sigmoid(x)
      Layers::Sigmoid.new.forward(x)
    end

    # Return the result of the softmax function.
    def self.softmax(x)
      Losses::SoftmaxCrossEntropy.softmax(x)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-dnn-0.15.0 lib/dnn/core/utils.rb
ruby-dnn-0.14.3 lib/dnn/core/utils.rb
ruby-dnn-0.14.2 lib/dnn/core/utils.rb
ruby-dnn-0.14.1 lib/dnn/core/utils.rb
ruby-dnn-0.14.0 lib/dnn/core/utils.rb