Module: DNN::Util

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

Overview

This module provides utility functions.

Class Method Summary collapse

Class Method Details

.get_minibatch(x, y, batch_size) ⇒ Object

Create a mini batch for “batch_size”.



5
6
7
8
# File 'lib/dnn/core/util.rb', line 5

def self.get_minibatch(x, y, batch_size)
  indexes = (0...x.shape[0]).to_a.sample(batch_size)
  [x[indexes, false], y[indexes, false]]
end

.load_hash(hash) ⇒ Object

Convert hash to an object.



21
22
23
24
25
26
27
# File 'lib/dnn/core/util.rb', line 21

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

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

Categorize labels into “num_classes” classes.



11
12
13
14
15
16
17
18
# File 'lib/dnn/core/util.rb', line 11

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