Module: DNN::Utils

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

Overview

This module provides utility functions.

Constant Summary collapse

NMath =
Xumo::NMath

Class Method Summary collapse

Class Method Details

.load_hash(hash) ⇒ Object

Convert hash to an object.



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

def self.load_hash(hash)
  dnn_class = DNN.const_get(hash[:class])
  if dnn_class.respond_to?(:load_hash)
    return dnn_class.load_hash(hash)
  end
  dnn_class.new
end

.sigmoid(x) ⇒ Object



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

def self.sigmoid(x)
  1 / (1 + NMath.exp(-x))
end

.softmax(x) ⇒ Object



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

def self.softmax(x)
  NMath.exp(x) / NMath.exp(x).sum(1).reshape(x.shape[0], 1)
end

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

Categorize labels into "num_classes" classes.



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

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