コード例 #1
0
def test_xcov_derivative_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n), dtype='float32', order='C')
    s = np.zeros_like(a)
    acc = xcc(a[:k1], a[k1:])  # k1 x k2
    c1 = a[k1:] - a[k1:].mean(1, keepdims=True)  # k2 x n
    c2 = a[:k1] - a[:k1].mean(1, keepdims=True)  # k1 x n

    s[:k1] = acc.dot(c1) / n
    s[k1:] = acc.T.dot(c2) / n

    outputs = GPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost_derivative(be, outputs, [], temp, k1)
    expected_result = GPUTensor(s)
    assert_tensor_near_equal(expected_result, my_result)
コード例 #2
0
ファイル: test_rbm.py プロジェクト: AI-Cdrone/neon
 def test_cudanet_negative(self):
     self.layer.positive(self.inputs)
     self.layer.negative(self.inputs)
     target = np.array([0.50274211,  0.50407821],
                       dtype='float32')
     assert_tensor_near_equal(self.layer.p_hid_minus.asnumpyarray()[:, 0],
                              target)
コード例 #3
0
ファイル: test_xcov.py プロジェクト: AI-Cdrone/neon
def test_xcov_derivative_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n), dtype='float32', order='C')
    s = np.zeros_like(a)
    acc = xcc(a[:k1], a[k1:])  # k1 x k2
    c1 = a[k1:] - a[k1:].mean(1, keepdims=True)  # k2 x n
    c2 = a[:k1] - a[:k1].mean(1, keepdims=True)  # k1 x n

    s[:k1] = acc.dot(c1)/n
    s[k1:] = acc.T.dot(c2)/n

    outputs = GPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost_derivative(be, outputs, [], temp, k1)
    expected_result = GPUTensor(s)
    assert_tensor_near_equal(expected_result, my_result)
コード例 #4
0
ファイル: test_softmax.py プロジェクト: AI-Cdrone/neon
def test_softmax_cputensor():
    sftmx = Softmax()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    temp = be.zeros((3, 1))
    outputs = np.exp(inputs-1) / np.sum(np.exp(inputs-1))
    sftmx.apply_function(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #5
0
ファイル: test_softmax.py プロジェクト: zz119/neon
def test_softmax_cputensor():
    sftmx = Softmax()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    temp = be.zeros((3, 1))
    outputs = np.exp(inputs - 1) / np.sum(np.exp(inputs - 1))
    sftmx.apply_function(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #6
0
def test_logistic_cputensor():
    lgstc = Logistic()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    temp = be.zeros((3, 1))
    outputs = 1.0 / (1.0 + np.exp(-inputs))
    lgstc.apply_function(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #7
0
def test_logistic_derivative_cputensor():
    lgstc = Logistic()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    outputs = 1.0 / (1.0 + np.exp(-inputs))
    outputs = outputs * (1.0 - outputs)
    temp = be.zeros(inputs.shape)
    lgstc.apply_derivative(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #8
0
ファイル: test_cross_entropy.py プロジェクト: xiaoyunwu/neon
def test_cross_entropy_derivative_cputensor():
    be = CPU(rng_seed=0)
    outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = CPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = ((outputs.asnumpyarray() - targets.asnumpyarray()) /
                       (outputs.asnumpyarray() * (1 - outputs.asnumpyarray())))
    assert_tensor_near_equal(
        expected_result, cross_entropy_derivative(be, outputs, targets, temp))
コード例 #9
0
ファイル: test_tanh.py プロジェクト: AI-Cdrone/neon
def test_tanh_cc2tensor():
    tntest = Tanh()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = GPUTensor([true_tanh(0), true_tanh(1), true_tanh(-2)])
    be = GPU(rng_seed=0)
    temp = be.zeros((3, 1))
    tntest.apply_function(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(outputs, temp)
コード例 #10
0
ファイル: test_tanh.py プロジェクト: AI-Cdrone/neon
def test_tanh_cputensor():
    tntest = Tanh()
    be = CPU(rng_seed=0)
    CPUTensor([0, 1, -2])
    inputs = np.array([0, 1, -2])
    temp = be.zeros(inputs.shape)
    outputs = np.array([true_tanh(0), true_tanh(1), true_tanh(-2)])
    tntest.apply_function(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #11
0
ファイル: test_tanh.py プロジェクト: zz119/neon
def test_tanh_derivative_cputensor():
    tntest = Tanh()
    inputs = np.array([0, 1, -2])
    be = CPU(rng_seed=0)
    outputs = np.array(
        [1 - true_tanh(0)**2, 1 - true_tanh(1)**2, 1 - true_tanh(-2)**2])
    temp = be.zeros(inputs.shape)
    tntest.apply_derivative(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #12
0
def test_logistic_cc2tensor():
    lgstc = Logistic()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = 1.0 / (1.0 + np.exp(-inputs))
    be = GPU(rng_seed=0)
    temp = be.zeros((3, 1))
    lgstc.apply_function(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(GPUTensor(outputs), temp)
コード例 #13
0
ファイル: test_softmax.py プロジェクト: AI-Cdrone/neon
def test_softmax_cc2tensor():
    sftmx = Softmax()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    be = GPU(rng_seed=0)
    temp = be.zeros((3, 1))
    sftmx.apply_function(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(GPUTensor(outputs), temp)
コード例 #14
0
ファイル: test_tanh.py プロジェクト: zz119/neon
def test_tanh_cc2tensor():
    tntest = Tanh()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = GPUTensor([true_tanh(0), true_tanh(1), true_tanh(-2)])
    be = GPU(rng_seed=0)
    temp = be.zeros((3, 1))
    tntest.apply_function(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(outputs, temp)
コード例 #15
0
ファイル: test_tanh.py プロジェクト: zz119/neon
def test_tanh_cputensor():
    tntest = Tanh()
    be = CPU(rng_seed=0)
    CPUTensor([0, 1, -2])
    inputs = np.array([0, 1, -2])
    temp = be.zeros(inputs.shape)
    outputs = np.array([true_tanh(0), true_tanh(1), true_tanh(-2)])
    tntest.apply_function(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #16
0
ファイル: test_softmax.py プロジェクト: zz119/neon
def test_softmax_cc2tensor():
    sftmx = Softmax()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    be = GPU(rng_seed=0)
    temp = be.zeros((3, 1))
    sftmx.apply_function(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(GPUTensor(outputs), temp)
コード例 #17
0
ファイル: test_tanh.py プロジェクト: zz119/neon
def test_tanh_derivative_cc2tensor():
    tntest = Tanh()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2], dtype='float32').reshape((3, 1))
    be = GPU(rng_seed=0)
    outputs = GPUTensor(
        [1 - true_tanh(0)**2, 1 - true_tanh(1)**2, 1 - true_tanh(-2)**2])
    temp = be.zeros(inputs.shape)
    tntest.apply_derivative(be, GPUTensor(inputs, dtype='float32'), temp)
    assert_tensor_near_equal(outputs, temp, tolerance=1e-5)
コード例 #18
0
ファイル: test_tanh.py プロジェクト: AI-Cdrone/neon
def test_tanh_derivative_cputensor():
    tntest = Tanh()
    inputs = np.array([0, 1, -2])
    be = CPU(rng_seed=0)
    outputs = np.array([1 - true_tanh(0) ** 2,
                        1 - true_tanh(1) ** 2,
                        1 - true_tanh(-2) ** 2])
    temp = be.zeros(inputs.shape)
    tntest.apply_derivative(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #19
0
ファイル: test_cpu.py プロジェクト: yourchanges/neon
 def test_lrgnorm(self):
     tsr = self.be.array([[-1, 0], [1, 3]])
     rpow = 1. / 5
     # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243])
     assert_tensor_equal(self.be.norm(tsr, order=5, axis=0),
                         CPUTensor([[2**rpow, 243**rpow]]))
     # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244])
     # 244**.2 == ~3.002465 hence the near_equal test
     assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1),
                              CPUTensor([1**rpow, 244**rpow]), 1e-6)
コード例 #20
0
ファイル: test_cross_entropy.py プロジェクト: AI-Cdrone/neon
def test_cross_entropy_derivative_cputensor():
    be = CPU(rng_seed=0)
    outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = CPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = ((outputs.asnumpyarray() - targets.asnumpyarray()) /
                       (outputs.asnumpyarray() * (1 - outputs.asnumpyarray())))
    assert_tensor_near_equal(expected_result,
                             cross_entropy_derivative(be, outputs,
                                                      targets, temp))
コード例 #21
0
ファイル: test_cpu.py プロジェクト: AI-Cdrone/neon
 def test_lrgnorm(self):
     tsr = self.be.array([[-1, 0], [1, 3]])
     rpow = 1. / 5
     # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243])
     assert_tensor_equal(self.be.norm(tsr, order=5, axis=0),
                         CPUTensor([[2**rpow, 243**rpow]]))
     # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244])
     # 244**.2 == ~3.002465 hence the near_equal test
     assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1),
                              CPUTensor([1**rpow, 244**rpow]), 1e-6)
コード例 #22
0
ファイル: test_cpu.py プロジェクト: AI-Cdrone/neon
 def test_negnorm(self):
     tsr = self.be.array([[-1, -2], [1, 3]])
     rpow = -1. / 3
     # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037])
     assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0),
                         CPUTensor([[2**rpow, .162037037037**rpow]]))
     # -> sum([[1, .125], [1, .037037]], axis=1)**rpow ->
     # rpow([1.125, 1.037037])
     assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1),
                              CPUTensor([1.125**rpow, 1.037037**rpow]),
                              1e-6)
コード例 #23
0
ファイル: test_cross_entropy.py プロジェクト: zz119/neon
def test_cross_entropy_cputensor():
    be = CPU(rng_seed=0)
    outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = CPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = np.sum(
        (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) -
        (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()),
        keepdims=True)
    assert_tensor_near_equal(expected_result,
                             cross_entropy(be, outputs, targets, temp))
コード例 #24
0
ファイル: test_softmax.py プロジェクト: AI-Cdrone/neon
def test_softmax_derivative_cputensor():
    sftmx = Softmax()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    errmat = np.ones(inputs.shape)
    a = np.einsum('ij,ji->i', errmat.T, outputs)
    outputs = outputs * (errmat - a[np.newaxis, :])
    temp = be.zeros(inputs.shape)
    sftmx.apply_derivative(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #25
0
ファイル: test_tanh.py プロジェクト: AI-Cdrone/neon
def test_tanh_derivative_cc2tensor():
    tntest = Tanh()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2], dtype='float32').reshape((3, 1))
    be = GPU(rng_seed=0)
    outputs = GPUTensor([1 - true_tanh(0) ** 2,
                         1 - true_tanh(1) ** 2,
                         1 - true_tanh(-2) ** 2])
    temp = be.zeros(inputs.shape)
    tntest.apply_derivative(be, GPUTensor(inputs, dtype='float32'), temp)
    assert_tensor_near_equal(outputs, temp, tolerance=1e-5)
コード例 #26
0
ファイル: test_cross_entropy.py プロジェクト: AI-Cdrone/neon
def test_cross_entropy_cputensor():
    be = CPU(rng_seed=0)
    outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = CPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = np.sum((- targets.asnumpyarray()) *
                             np.log(outputs.asnumpyarray()) -
                             (1 - targets.asnumpyarray()) *
                             np.log(1 - outputs.asnumpyarray()), keepdims=True)
    assert_tensor_near_equal(expected_result, cross_entropy(be, outputs,
                                                            targets, temp))
コード例 #27
0
ファイル: test_softmax.py プロジェクト: zz119/neon
def test_softmax_derivative_cputensor():
    sftmx = Softmax()
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    be = CPU(rng_seed=0)
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    errmat = np.ones(inputs.shape)
    a = np.einsum('ij,ji->i', errmat.T, outputs)
    outputs = outputs * (errmat - a[np.newaxis, :])
    temp = be.zeros(inputs.shape)
    sftmx.apply_derivative(be, CPUTensor(inputs), temp)
    assert_tensor_near_equal(CPUTensor(outputs), temp)
コード例 #28
0
ファイル: test_cpu.py プロジェクト: yourchanges/neon
 def test_negnorm(self):
     tsr = self.be.array([[-1, -2], [1, 3]])
     rpow = -1. / 3
     # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037])
     assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0),
                         CPUTensor([[2**rpow, .162037037037**rpow]]))
     # -> sum([[1, .125], [1, .037037]], axis=1)**rpow ->
     # rpow([1.125, 1.037037])
     assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1),
                              CPUTensor([1.125**rpow, 1.037037**rpow]),
                              1e-6)
コード例 #29
0
ファイル: test_softmax.py プロジェクト: zz119/neon
def test_softmax_derivative_cc2tensor():
    sftmx = Softmax()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    errmat = np.ones(inputs.shape)
    a = np.einsum('ij,ji->i', errmat.T, outputs)
    outputs = outputs * (errmat - a[np.newaxis, :])
    be = GPU(rng_seed=0)
    temp = be.zeros(inputs.shape)
    sftmx.apply_derivative(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(GPUTensor(outputs), temp)
コード例 #30
0
ファイル: test_cc2.py プロジェクト: zz119/neon
 def test_negnorm(self):
     tsr = self.be.array([[-1, -2], [1, 3]])
     rpow = -1. / 3
     # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037])
     out = self.be.empty((1, 2))
     assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0, out=out),
                         self.gpt([[2**rpow, .162037037037**rpow]]))
     # -> sum([[1, .125], [1, .037037]], axis=1)**rpow ->
     # rpow([1.125, 1.037037])
     out = self.be.empty((2, 1))
     assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1, out=out),
                              self.gpt([1.125**rpow, 1.037037**rpow]), 1e-6)
コード例 #31
0
ファイル: test_cc2.py プロジェクト: zz119/neon
 def test_lrgnorm(self):
     tsr = self.be.array([[-1, 0], [1, 3]])
     rpow = 1. / 5
     # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243])
     out = self.be.empty((1, 2))
     assert_tensor_equal(self.be.norm(tsr, order=5, axis=0, out=out),
                         self.gpt([[2**rpow, 243**rpow]]))
     # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244])
     # 244**.2 == ~3.002465 hence the near_equal test
     out = self.be.empty((2, 1))
     assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1, out=out),
                              self.gpt([1**rpow, 244**rpow]), 1e-6)
コード例 #32
0
ファイル: test_softmax.py プロジェクト: AI-Cdrone/neon
def test_softmax_derivative_cc2tensor():
    sftmx = Softmax()
    from neon.backends.cc2 import GPU, GPUTensor
    inputs = np.array([0, 1, -2]).reshape((3, 1))
    outputs = np.exp(inputs) / np.sum(np.exp(inputs))
    errmat = np.ones(inputs.shape)
    a = np.einsum('ij,ji->i', errmat.T, outputs)
    outputs = outputs * (errmat - a[np.newaxis, :])
    be = GPU(rng_seed=0)
    temp = be.zeros(inputs.shape)
    sftmx.apply_derivative(be, GPUTensor(inputs), temp)
    assert_tensor_near_equal(GPUTensor(outputs), temp)
コード例 #33
0
ファイル: test_cc2.py プロジェクト: AI-Cdrone/neon
 def test_lrgnorm(self):
     tsr = self.be.array([[-1, 0], [1, 3]])
     rpow = 1. / 5
     # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243])
     out = self.be.empty((1, 2))
     assert_tensor_equal(self.be.norm(tsr, order=5, axis=0, out=out),
                         self.gpt([[2**rpow, 243**rpow]]))
     # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244])
     # 244**.2 == ~3.002465 hence the near_equal test
     out = self.be.empty((2, 1))
     assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1, out=out),
                              self.gpt([1**rpow, 244**rpow]), 1e-6)
コード例 #34
0
ファイル: test_cross_entropy.py プロジェクト: AI-Cdrone/neon
def test_cross_entropy_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)  # to ensure cublas_init() is called.
    outputs = GPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = GPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = np.sum((- targets.asnumpyarray()) *
                             np.log(outputs.asnumpyarray()) -
                             (1 - targets.asnumpyarray()) *
                             np.log(1 - outputs.asnumpyarray()), keepdims=True)
    assert_tensor_near_equal(expected_result, cross_entropy(be, outputs,
                                                            targets, temp),
                             tolerance=1e-6)
コード例 #35
0
ファイル: test_cross_entropy.py プロジェクト: xiaoyunwu/neon
def test_cross_entropy_limits():
    be = CPU(rng_seed=0)
    outputs = CPUTensor([0.5, 1.0, 0.0, 0.0001])
    targets = CPUTensor([0.5, 0.0, 1.0, 0.2])
    eps = 2**-23
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = np.sum(
        (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray() + eps) -
        (1 - targets.asnumpyarray()) *
        np.log(1 - outputs.asnumpyarray() + eps),
        keepdims=True)
    assert_tensor_near_equal(expected_result,
                             cross_entropy(be, outputs, targets, temp, eps))
コード例 #36
0
ファイル: test_cross_entropy.py プロジェクト: zz119/neon
def test_cross_entropy_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)  # to ensure cublas_init() is called.
    outputs = GPUTensor([0.5, 0.9, 0.1, 0.0001])
    targets = GPUTensor([0.5, 0.99, 0.01, 0.2])
    temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)]
    expected_result = np.sum(
        (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) -
        (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()),
        keepdims=True)
    assert_tensor_near_equal(expected_result,
                             cross_entropy(be, outputs, targets, temp),
                             tolerance=1e-6)
コード例 #37
0
ファイル: test_cc2.py プロジェクト: AI-Cdrone/neon
 def test_negnorm(self):
     tsr = self.be.array([[-1, -2], [1, 3]])
     rpow = -1. / 3
     # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037])
     out = self.be.empty((1, 2))
     assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0, out=out),
                         self.gpt([[2**rpow, .162037037037**rpow]]))
     # -> sum([[1, .125], [1, .037037]], axis=1)**rpow ->
     # rpow([1.125, 1.037037])
     out = self.be.empty((2, 1))
     assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1, out=out),
                              self.gpt([1.125**rpow, 1.037037**rpow]),
                              1e-6)
コード例 #38
0
def test_xcov_cputensor():
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n) * 10, dtype='float32', order='C')
    acc = xcc(a[:k1], a[k1:])
    expected_result = 0.5 * (acc**2.).sum()

    be = CPU(rng_seed=0)
    outputs = CPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost(be, outputs, [], temp, k1)
    assert_tensor_near_equal(expected_result, my_result)
コード例 #39
0
ファイル: test_xcov.py プロジェクト: AI-Cdrone/neon
def test_xcov_cputensor():
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n)*10, dtype='float32', order='C')
    acc = xcc(a[:k1], a[k1:])
    expected_result = 0.5 * (acc**2.).sum()

    be = CPU(rng_seed=0)
    outputs = CPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost(be, outputs, [], temp, k1)
    assert_tensor_near_equal(expected_result, my_result)
コード例 #40
0
def test_xcov_cc2tensor():
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n) * 10, dtype='float32', order='C')
    acc = xcc(a[:k1], a[k1:])
    expected_result = 0.5 * (acc**2.).sum()

    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)  # to ensure cublas_init() is called.
    outputs = GPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost(be, outputs, [], temp, k1)
    assert_tensor_near_equal(expected_result, my_result, tolerance=1e-3)
コード例 #41
0
ファイル: test_xcov.py プロジェクト: AI-Cdrone/neon
def test_xcov_cc2tensor():
    np.random.seed(0)
    n = 10
    k = 8
    (k1, k2) = (3, 5)
    a = np.array(np.random.randn(k, n)*10, dtype='float32', order='C')
    acc = xcc(a[:k1], a[k1:])
    expected_result = 0.5 * (acc**2.).sum()

    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)  # to ensure cublas_init() is called.
    outputs = GPUTensor(a.copy())
    tempbuf1 = be.empty((k1, n))
    tempbuf2 = be.empty((k2, n))
    tempbuf3 = be.empty((k1, k2))
    tempbuf4 = be.empty(outputs.shape)
    temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4]
    my_result = xcov_cost(be, outputs, [], temp, k1)
    assert_tensor_near_equal(expected_result, my_result, tolerance=1e-3)
コード例 #42
0
 def test_cudanet_positive(self):
     self.layer.positive(self.inputs)
     target = np.array([0.50541031, 0.50804842], dtype='float32')
     assert_tensor_near_equal(self.layer.p_hid_plus.asnumpyarray()[:, 0],
                              target)
コード例 #43
0
 def test_cudanet_cost(self):
     self.layer.positive(self.inputs)
     self.layer.negative(self.inputs)
     thecost = self.cost.apply_function(self.inputs)
     target = 106.588943481
     assert_tensor_near_equal(thecost, target)
コード例 #44
0
ファイル: test_rbm.py プロジェクト: AI-Cdrone/neon
 def test_cudanet_cost(self):
     self.layer.positive(self.inputs)
     self.layer.negative(self.inputs)
     thecost = self.cost.apply_function(self.inputs)
     target = 106.588943481
     assert_tensor_near_equal(thecost, target)
コード例 #45
0
ファイル: test_rbm.py プロジェクト: AI-Cdrone/neon
 def test_cudanet_positive(self):
     self.layer.positive(self.inputs)
     target = np.array([0.50541031, 0.50804842],
                       dtype='float32')
     assert_tensor_near_equal(self.layer.p_hid_plus.asnumpyarray()[:, 0],
                              target)
コード例 #46
0
 def test_cudanet_negative(self):
     self.layer.positive(self.inputs)
     self.layer.negative(self.inputs)
     target = np.array([0.50274211, 0.50407821], dtype='float32')
     assert_tensor_near_equal(self.layer.p_hid_minus.asnumpyarray()[:, 0],
                              target)