Exemplo n.º 1
0
def test_cross_entropy_derivative_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)
    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 = ((outputs.asnumpyarray() - targets.asnumpyarray()) /
                       (outputs.asnumpyarray() * (1 - outputs.asnumpyarray())))
    assert_tensor_near_equal(
        expected_result, cross_entropy_derivative(be, outputs, targets, temp))
Exemplo n.º 2
0
def test_cross_entropy_derivative_cc2tensor():
    from neon.backends.cc2 import GPU, GPUTensor
    be = GPU(rng_seed=0)
    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 = ((outputs.asnumpyarray() - targets.asnumpyarray()) /
                       (outputs.asnumpyarray() * (1 - outputs.asnumpyarray())))
    assert_tensor_near_equal(expected_result,
                             cross_entropy_derivative(be, outputs,
                                                      targets, temp))
Exemplo n.º 3
0
def compare_cc2_tensors(inputs, outputs, deriv=False):
    from neon.backends.cc2 import GPU, GPUTensor
    rlin = RectLeaky()
    be = GPU()
    temp = be.zeros(inputs.shape)
    if deriv is True:
        rlin.apply_derivative(be, GPUTensor(inputs), temp)
    else:
        rlin.apply_function(be, GPUTensor(inputs), temp)
    be.subtract(temp, GPUTensor(outputs), temp)
    assert_tensor_equal(temp, be.zeros(inputs.shape))
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
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)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
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)
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
class TestGPU(object):
    def setup(self):
        from neon.backends.cc2 import GPU, GPUTensor
        # this code gets called prior to each test
        self.be = GPU(rng_seed=0)
        self.gpt = GPUTensor

    def test_empty_creation(self):
        tns = self.be.empty((4, 3))
        assert tns.shape == (4, 3)

    def test_array_creation(self):
        tns = self.be.array([[1, 2], [3, 4]])
        assert tns.shape == (2, 2)
        assert_tensor_equal(tns, self.gpt([[1, 2], [3, 4]]))

    def test_zeros_creation(self):
        tns = self.be.zeros([3, 1])
        assert tns.shape == (3, 1)
        assert_tensor_equal(tns, self.gpt([[0], [0], [0]]))

    def test_ones_creation(self):
        tns = self.be.ones([1, 4])
        assert tns.shape == (1, 4)
        assert_tensor_equal(tns, self.gpt([[1, 1, 1, 1]]))

    def test_all_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 1]]))

    def test_some_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.array([[0, 1], [0, 1]])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 1], [0, 1]]))

    def test_none_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.zeros([2, 2])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 0]]))

    def test_all_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.zeros([2, 2])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 1]]))

    def test_some_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.array([[0, 1], [0, 1]])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 0], [1, 0]]))

    def test_none_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 0]]))

    def test_greater(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.greater(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 1]]))

    def test_greater_equal(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.greater_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [1, 1]]))

    def test_less(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.less(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [0, 0]]))

    def test_less_equal(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.less_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 0]]))

    @nottest  # TODO: cudanet doesn't currently support noaxis argmin
    def test_argmin_noaxis(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty([1, 1])
        self.be.argmin(tsr, None, out)
        assert_tensor_equal(out, self.gpt([[0]]))

    def test_argmin_axis0(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty((1, 2))
        self.be.argmin(tsr, 0, out)
        assert_tensor_equal(out, self.gpt([[0, 0]]))

    def test_argmin_axis1(self):
        tsr = self.be.array([[-1, 10], [11, 9]])
        out = self.be.empty((2, 1))
        self.be.argmin(tsr, 1, out)
        assert_tensor_equal(out, self.gpt([[0], [1]]))

    @nottest  # TODO: cudanet doesn't currently support noaxis argmax
    def test_argmax_noaxis(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty([1, 1])
        self.be.argmax(tsr, None, out)
        assert_tensor_equal(out, self.gpt(3))

    def test_argmax_axis0(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty((1, 2))
        self.be.argmax(tsr, 0, out)
        assert_tensor_equal(out, self.gpt([[1, 1]]))

    def test_argmax_axis1(self):
        tsr = self.be.array([[-1, 10], [11, 9]])
        out = self.be.empty((2, 1))
        self.be.argmax(tsr, 1, out)
        assert_tensor_equal(out, self.gpt([[1], [0]]))

    def test_2norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        rpow = 1. / 2
        # -> sum([[1, 0], [1, 9]], axis=0)**.5 -> sqrt([2, 9])
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=2, axis=0, out=out),
                            self.gpt([[2**rpow, 9**rpow]]))
        # -> sum([[1, 0], [1, 9]], axis=1)**.5 -> sqrt([1, 10])
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=2, axis=1, out=out),
                            self.gpt([1**rpow, 10**rpow]))

    def test_1norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> sum([[1, 0], [1, 3]], axis=0)**1 -> [2, 3]
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=1, axis=0, out=out),
                            self.gpt([[2, 3]]))
        # -> sum([[1, 0], [1, 3]], axis=1)**1 -> [1, 4]
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=1, axis=1, out=out),
                            self.gpt([1, 4]))

    def test_0norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> sum(tsr != 0, axis=0) -> [2, 1]
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=0, axis=0, out=out),
                            self.gpt([[2, 1]]))
        # -> sum(tsr != 0, axis=1) -> [1, 2]
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=0, axis=1, out=out),
                            self.gpt([1, 2]))

    def test_infnorm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> max(abs(tsr), axis=0) -> [1, 3]
        assert_tensor_equal(self.be.norm(tsr, order=float('inf'), axis=0),
                            self.gpt([[1, 3]]))
        # -> max(abs(tsr), axis=1) -> [1, 3]
        assert_tensor_equal(self.be.norm(tsr, order=float('inf'), axis=1),
                            self.gpt([1, 3]))

    def test_neginfnorm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> min(abs(tsr), axis=0) -> [1, 0]
        assert_tensor_equal(self.be.norm(tsr, order=float('-inf'), axis=0),
                            self.gpt([[1, 0]]))
        # -> min(abs(tsr), axis=1) -> [0, 1]
        assert_tensor_equal(self.be.norm(tsr, order=float('-inf'), axis=1),
                            self.gpt([0, 1]))

    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)

    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)
Exemplo n.º 16
0
class TestGPU(object):

    def setup(self):
        from neon.backends.cc2 import GPU, GPUTensor
        # this code gets called prior to each test
        self.be = GPU(rng_seed=0)
        self.gpt = GPUTensor

    def test_empty_creation(self):
        tns = self.be.empty((4, 3))
        assert tns.shape == (4, 3)

    def test_array_creation(self):
        tns = self.be.array([[1, 2], [3, 4]])
        assert tns.shape == (2, 2)
        assert_tensor_equal(tns, self.gpt([[1, 2], [3, 4]]))

    def test_zeros_creation(self):
        tns = self.be.zeros([3, 1])
        assert tns.shape == (3, 1)
        assert_tensor_equal(tns, self.gpt([[0], [0], [0]]))

    def test_ones_creation(self):
        tns = self.be.ones([1, 4])
        assert tns.shape == (1, 4)
        assert_tensor_equal(tns, self.gpt([[1, 1, 1, 1]]))

    def test_all_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 1]]))

    def test_some_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.array([[0, 1], [0, 1]])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 1], [0, 1]]))

    def test_none_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.zeros([2, 2])
        out = self.be.empty([2, 2])
        self.be.equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 0]]))

    def test_all_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.zeros([2, 2])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 1]]))

    def test_some_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.array([[0, 1], [0, 1]])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 0], [1, 0]]))

    def test_none_not_equal(self):
        left = self.be.ones([2, 2])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.not_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 0]]))

    def test_greater(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.greater(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [0, 1]]))

    def test_greater_equal(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.greater_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[0, 0], [1, 1]]))

    def test_less(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.less(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [0, 0]]))

    def test_less_equal(self):
        left = self.be.array([[-1, 0], [1, 92]])
        right = self.be.ones([2, 2])
        out = self.be.empty([2, 2])
        self.be.less_equal(left, right, out)
        assert out.shape == (2, 2)
        assert_tensor_equal(out, self.gpt([[1, 1], [1, 0]]))

    @nottest  # TODO: cudanet doesn't currently support noaxis argmin
    def test_argmin_noaxis(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty([1, 1])
        self.be.argmin(tsr, None, out)
        assert_tensor_equal(out, self.gpt([[0]]))

    def test_argmin_axis0(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty((1, 2))
        self.be.argmin(tsr, 0, out)
        assert_tensor_equal(out, self.gpt([[0, 0]]))

    def test_argmin_axis1(self):
        tsr = self.be.array([[-1, 10], [11, 9]])
        out = self.be.empty((2, 1))
        self.be.argmin(tsr, 1, out)
        assert_tensor_equal(out, self.gpt([[0], [1]]))

    @nottest  # TODO: cudanet doesn't currently support noaxis argmax
    def test_argmax_noaxis(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty([1, 1])
        self.be.argmax(tsr, None, out)
        assert_tensor_equal(out, self.gpt(3))

    def test_argmax_axis0(self):
        tsr = self.be.array([[-1, 0], [1, 92]])
        out = self.be.empty((1, 2))
        self.be.argmax(tsr, 0, out)
        assert_tensor_equal(out, self.gpt([[1, 1]]))

    def test_argmax_axis1(self):
        tsr = self.be.array([[-1, 10], [11, 9]])
        out = self.be.empty((2, 1))
        self.be.argmax(tsr, 1, out)
        assert_tensor_equal(out, self.gpt([[1], [0]]))

    def test_2norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        rpow = 1. / 2
        # -> sum([[1, 0], [1, 9]], axis=0)**.5 -> sqrt([2, 9])
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=2, axis=0, out=out),
                            self.gpt([[2**rpow, 9**rpow]]))
        # -> sum([[1, 0], [1, 9]], axis=1)**.5 -> sqrt([1, 10])
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=2, axis=1, out=out),
                            self.gpt([1**rpow, 10**rpow]))

    def test_1norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> sum([[1, 0], [1, 3]], axis=0)**1 -> [2, 3]
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=1, axis=0, out=out),
                            self.gpt([[2, 3]]))
        # -> sum([[1, 0], [1, 3]], axis=1)**1 -> [1, 4]
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=1, axis=1, out=out),
                            self.gpt([1, 4]))

    def test_0norm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> sum(tsr != 0, axis=0) -> [2, 1]
        out = self.be.empty((1, 2))
        assert_tensor_equal(self.be.norm(tsr, order=0, axis=0, out=out),
                            self.gpt([[2, 1]]))
        # -> sum(tsr != 0, axis=1) -> [1, 2]
        out = self.be.empty((2, 1))
        assert_tensor_equal(self.be.norm(tsr, order=0, axis=1, out=out),
                            self.gpt([1, 2]))

    def test_infnorm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> max(abs(tsr), axis=0) -> [1, 3]
        assert_tensor_equal(self.be.norm(tsr, order=float('inf'), axis=0),
                            self.gpt([[1, 3]]))
        # -> max(abs(tsr), axis=1) -> [1, 3]
        assert_tensor_equal(self.be.norm(tsr, order=float('inf'), axis=1),
                            self.gpt([1, 3]))

    def test_neginfnorm(self):
        tsr = self.be.array([[-1, 0], [1, 3]])
        # -> min(abs(tsr), axis=0) -> [1, 0]
        assert_tensor_equal(self.be.norm(tsr, order=float('-inf'), axis=0),
                            self.gpt([[1, 0]]))
        # -> min(abs(tsr), axis=1) -> [0, 1]
        assert_tensor_equal(self.be.norm(tsr, order=float('-inf'), axis=1),
                            self.gpt([0, 1]))

    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)

    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)