Module: DNN::Utils

Defined in:
lib/dnn/core/utils.rb

Overview

This module provides utility functions.

Class Method Summary collapse

Class Method Details

.hash_to_obj(hash) ⇒ Object

Convert hash to an object.



18
19
20
21
22
# File 'lib/dnn/core/utils.rb', line 18

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

.sigmoid(x) ⇒ Object

Return the result of the sigmoid function.



25
26
27
# File 'lib/dnn/core/utils.rb', line 25

def self.sigmoid(x)
  Layers::Sigmoid.new.forward(x)
end

.softmax(x) ⇒ Object

Return the result of the softmax function.



30
31
32
# File 'lib/dnn/core/utils.rb', line 30

def self.softmax(x)
  Losses::SoftmaxCrossEntropy.softmax(x)
end

.to_categorical(y, num_classes, narray_type = nil) ⇒ Object

Categorize labels into “num_classes” classes.

Parameters:

  • y (Numo::SFloat)

    Label data.

  • num_classes (Numo::SFloat)

    Number of classes to classify.

  • narray_type (Class) (defaults to: nil)

    Type of Numo::Narray data after classification.



8
9
10
11
12
13
14
15
# File 'lib/dnn/core/utils.rb', line 8

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