Module: DNN::Utils

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

Overview

This module provides utility functions.

Class Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Object

Convert hash to an object.



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

def self.from_hash(hash)
  return nil if hash == nil
  dnn_class = DNN.const_get(hash[:class])
  if dnn_class.respond_to?(:from_hash)
    return dnn_class.from_hash(hash)
  end
  dnn_class.new
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)
  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)
  SoftmaxCrossEntropy.softmax(x)
end

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

Categorize labels into "num_classes" classes.



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

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