/
sigmoid_test.rb
45 lines (38 loc) · 1.36 KB
/
sigmoid_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true
require 'chainer/functions/activation/sigmoid'
class Chainer::Functions::Activation::SigmoidTest < Test::Unit::TestCase
data = {
'test1' => {shape: [3, 2], dtype: xm::SFloat},
'test2' => {shape: [], dtype: xm::SFloat},
'test3' => {shape: [3, 2], dtype: xm::DFloat},
'test4' => {shape: [], dtype: xm::DFloat}}
def _setup(data)
@shape = data[:shape]
@dtype = data[:dtype]
@dtype.srand(1) # To avoid false of "nearly_eq().all?", Use fixed seed value.
@x = @dtype.new(@shape).rand(1) - 0.5
@gy = @dtype.new(@shape).rand(0.2) - 0.1
@check_forward_options = {}
@check_backward_options = {}
end
def check_forward(x_data, use_cudnn: "always")
x = Chainer::Variable.new(x_data)
y = Chainer::Functions::Activation::Sigmoid.sigmoid(x)
assert_equal(@dtype, y.data.class)
y_expect = Chainer::Functions::Activation::Sigmoid.sigmoid(Chainer::Variable.new(@x))
assert_true(y.data.nearly_eq(y_expect.data).all?)
end
data(data)
def test_forward(data)
_setup(data)
check_forward(@x.dup)
end
def check_backward(x_data, y_grad, use_cudnn: "always")
Chainer::check_backward(Chainer::Functions::Activation::Sigmoid.method(:sigmoid), x_data, y_grad, @check_backward_options)
end
data(data)
def test_backward(data)
_setup(data)
check_backward(@x.dup, @gy.dup)
end
end