require 'numo/narray' def identity_function(x) return x end def step_function(x) grad = Numo::Uint32.zeros(x.shape) grad[x>=0] = 1 grad end def sigmoid(x) 1 / (1 + Numo::DFloat::Math.exp(-x)) end def sigmoid_grad(x) (1.0 - sigmoid(x)) * sigmoid(x) end def relu(x) copy = x.copy copy[x < 0] = 0 copy end def relu_grad(x) grad = Numo::DFloat.zeros(x.shape) grad[x>=0] = 1 grad end def softmax(x) if x.n