Module: DNN::Utils

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

Overview

This module provides utility functions.

Class Method Summary collapse

Class Method Details

.broadcast_to(x, target_shape) ⇒ Numo::SFloat

Broadcast to target shape.

Parameters:

  • x (Numo::SFloat)

    Data to broadcast.

  • Shape (Array)

    to broadcast.

Returns:

  • (Numo::SFloat)

    Broadcasted data.



28
29
30
# File 'lib/dnn/core/utils.rb', line 28

def self.broadcast_to(x, target_shape)
  Layers::MathUtils.broadcast_to(x, target_shape)
end

.check_input_data_type(data_name, data, expected_type) ⇒ Object

Check training or evaluate input data type.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dnn/core/utils.rb', line 43

def self.check_input_data_type(data_name, data, expected_type)
  if !data.is_a?(expected_type) && !data.is_a?(Array)
    raise TypeError, "#{data_name}:#{data.class.name} is not an instance of #{expected_type.name} class or Array class."
  end
  if data.is_a?(Array)
    data.each.with_index do |v, i|
      unless v.is_a?(expected_type)
        raise TypeError, "#{data_name}[#{i}]:#{v.class.name} is not an instance of #{expected_type.name} class."
      end
    end
  end
end

.cumo2numo(ca) ⇒ Object

Convert cumo to numo.



69
70
71
72
73
# File 'lib/dnn/core/utils.rb', line 69

def self.cumo2numo(ca)
  b = ca.to_binary
  na = Numo::SFloat.from_binary(b)
  na.reshape(*ca.shape)
end

.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

.numerical_grad(x, func) ⇒ Object

Perform numerical differentiation.



57
58
59
# File 'lib/dnn/core/utils.rb', line 57

def self.numerical_grad(x, func)
  (func.(x + 1e-7) - func.(x)) / 1e-7
end

.numo2cumo(na) ⇒ Object

Convert numo to cumo.



62
63
64
65
66
# File 'lib/dnn/core/utils.rb', line 62

def self.numo2cumo(na)
  b = na.to_binary
  ca = Cumo::SFloat.from_binary(b)
  ca.reshape(*na.shape)
end

.sigmoid(x) ⇒ Object

Return the result of the sigmoid function.



33
34
35
# File 'lib/dnn/core/utils.rb', line 33

def self.sigmoid(x)
  Losses::SigmoidCrossEntropy.sigmoid(x)
end

.softmax(x) ⇒ Object

Return the result of the softmax function.



38
39
40
# File 'lib/dnn/core/utils.rb', line 38

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

.to_f(x) ⇒ Object

Force convert to Float.



76
77
78
79
80
81
82
# File 'lib/dnn/core/utils.rb', line 76

def self.to_f(x)
  if x.is_a?(Xumo::NArray)
    x[0].to_f
  else
    x.to_f
  end
end