module DNN::Utils
This module provides utility functions.
Public Class Methods
broadcast_to(x, target_shape)
click to toggle source
Broadcast to target shape. @param [Numo::SFloat] x Data to broadcast. @param [Array] Shape to broadcast. @return [Numo::SFloat] Broadcasted data.
# File lib/dnn/core/utils.rb, line 28 def self.broadcast_to(x, target_shape) Layers::MathUtils.broadcast_to(x, target_shape) end
cumo2numo(ca)
click to toggle source
Convert cumo to numo.
# File lib/dnn/core/utils.rb, line 55 def self.cumo2numo(ca) b = ca.to_binary na = Numo::SFloat.from_binary(b) na.reshape(*ca.shape) end
hash_to_obj(hash)
click to toggle source
Convert hash to an object.
# 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)
click to toggle source
Perform numerical differentiation.
# File lib/dnn/core/utils.rb, line 43 def self.numerical_grad(x, func) (func.(x + 1e-7) - func.(x)) / 1e-7 end
numo2cumo(na)
click to toggle source
Convert numo to cumo.
# File lib/dnn/core/utils.rb, line 48 def self.numo2cumo(na) b = na.to_binary ca = Cumo::SFloat.from_binary(b) ca.reshape(*na.shape) end
sigmoid(x)
click to toggle source
Return the result of the sigmoid function.
# File lib/dnn/core/utils.rb, line 33 def self.sigmoid(x) Losses::SigmoidCrossEntropy.sigmoid(x) end
softmax(x)
click to toggle source
Return the result of the softmax function.
# 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)
click to toggle source
Categorize labels into “num_classes” classes. @param [Numo::SFloat] y Label data. @param [Numo::SFloat] num_classes Number of classes to classify. @param [Class] narray_type Type of Numo::Narray data after classification.
# 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)
click to toggle source
Force convert to Float
.
# File lib/dnn/core/utils.rb, line 62 def self.to_f(x) if x.is_a?(Xumo::NArray) x[0].to_f else x.to_f end end