Esempio n. 1
0
    def test_data(self):

        # confirm type as class tuple.
        a = dc.zeros(2, 3).astype('int')
        adata = a.data()
        assert type(adata) == type((1, ))

        # load new data
        new_data_list = [10, 11, 12, 13, 14, 15]
        a.load(dc.ivec(new_data_list))
        assert a[0] == 10

        # load one element with flat index
        a[0] = 777
        assert a[0] == 777

        # reshape, fetch and load with multi indices
        a = dc.arange(12).astype('int')
        a.reshape(dc.lvec([2, 2, 3]))
        assert a[0, 1, 1] == 4

        a[1, 1, 1] = 200
        assert a[1, 1, 1] == 200

        # negative test
        try:
            # This throws ValueError
            print(a[0, 0, 9, 9, 9])
        except ValueError as e:
            assert e
Esempio n. 2
0
 def setUp(self):
     self.nullT = dc.array(0)
     self.zeros = dc.zeros(2, 3).asTypeInt()
     self.ones = dc.ones(2, 3).asTypeInt()
     self.f0_4 = dc.arange(5)
     self.f5_9 = dc.arange(10, 5)
     self.i0_4 = self.f0_4.asTypeInt()
     self.i5_9 = self.f5_9.asTypeInt()
     self.b0_4 = self.f0_4.asTypeBool()
     self.b5_9 = self.f5_9.asTypeBool()
Esempio n. 3
0
    def test_assignments(self):
        assert not self.nullT
        assert self.zeros
        assert self.ones
        self.zeros += self.ones
        dnnc_testing.utils.assert_equal(self.zeros, self.ones)
        self.zeros -= self.ones
        tmp = self.ones.copy()
        tmp -= self.ones
        dnnc_testing.utils.assert_equal(tmp, self.zeros)
        tmp = self.f5_9
        tmp -= dc.array([5])
        dnnc_testing.utils.assert_allclose(tmp, self.f0_4)
        tmp = self.i5_9
        tmp -= dc.array([5]).asTypeInt()
        dnnc_testing.utils.assert_equal(tmp, self.i0_4)
        tmp = self.b5_9
        tmp -= dc.array([5]).asTypeBool()

        dnnc_testing.utils.assert_equal(tmp, dc.zeros(5).asTypeBool())
Esempio n. 4
0
    def test_create(self):

        # null tensor test
        a = dc.array(0)
        assert a.isnull() == True
        assert a.empty() == True

        # test assignment is shallow copy of memory
        b = a
        assert a.sameas(b) == True
        assert a.identifier() == b.identifier()

        # tensor without initiliaztion
        a = dc.array(2, 3, 4, 5)
        assert a.length() == 120

        # tensor random initiliaztion
        a = dc.random(2, 3, 4, 5)
        assert a.length() == 120

        # tensor without initiliaztion
        a = dc.empty(2, 3, 4, 5)
        assert a.length() == 120

        # zero tensor
        a = dc.zeros(2, 3, 4, 5)
        assert np.array(list(a.data())).sum().astype(np.int) == 0

        # one tensor
        a = dc.ones(2, 3, 4, 5)
        assert np.array(list(a.data())).sum().astype(np.int) == 120

        # tensor from python list
        l1D = [1, 3, 5]
        a = dc.array(l1D).astype('int')
        np.testing.assert_equal(np.array(l1D), np.array(list(a.data())))

        # tensor from python list of lists
        l2D = [[1, 3, 5], [2, 4, 6]]
        a = dc.array(l2D).astype('int')
        assert a.rank() == 2
        assert a.shape() == (2, 3)
        np.testing.assert_equal(np.array(l2D).flatten(), \
                np.array(list(a.data())))

        # copy tensor
        b = a.copy()
        assert a.sameas(b) == False
        assert a.identifier() != b.identifier()

        # arange
        a = dc.arange(10)
        assert a.length() == 10

        # add start and step
        a = dc.arange(10, 5, 3).astype('int')
        assert a.data() == (5, 8)

        # swap start and stop.
        a = dc.arange(5, 10, 3).astype('int')
        assert a.data() == (5, 8)
Esempio n. 5
0
    def setUp(self):
        ## random testcase
        self.channels = 1
        self.featuremaps = 1
        self.batchsize = 1
        #self.oneK          = 1024
        self.oneK = 50
        self.X_h = self.oneK + np.random.randint(self.oneK * 3)
        self.X_w = self.oneK + np.random.randint(self.oneK * 3)
        self.K_h = 3 + np.random.randint(97)
        self.K_w = 3 + np.random.randint(97)
        self.np_strides = np.zeros(2).astype(np.float32)
        self.np_strides[0] = 1 + np.random.randint(self.K_w - 1)
        self.np_strides[1] = 1 + np.random.randint(self.K_h - 1)
        self.np_B = np.zeros(self.featuremaps).astype(np.float32)
        self.np_X_data = np.random.randn(self.X_w * self.X_h).astype(
            np.float32)
        self.np_K_data = np.random.randn(self.K_w * self.K_h).astype(
            np.float32)
        self.np_X = np.reshape(self.np_X_data, (self.X_h, self.X_w))
        self.np_K = np.reshape(self.np_K_data, (self.K_h, self.K_w))

        self.dc_X = dc.reshape(
            dc.array(list(self.np_X_data)),
            (self.batchsize, self.channels, self.X_h, self.X_w)).asTypeFloat()
        self.dc_K = dc.reshape(dc.array(list(self.np_K_data)),
                               (self.featuremaps, self.channels, self.K_h,
                                self.K_w)).asTypeFloat()
        self.dc_B = dc.zeros(self.featuremaps).asTypeFloat()
        self.dc_strides = dc.reshape(dc.array(list(self.np_strides)),
                                     (2)).asTypeInt()
        self.dc_nullT = dc.array(0)

        ## onnx conv example testcase
        self.onnx_dc_X = dc.reshape(
            dc.array([
                0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
                14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.
            ]), (1, 1, 5, 5))
        self.onnx_dc_X2 = dc.reshape(
            dc.array([
                0., 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.
            ]), (1, 1, 7, 5))
        self.onnx_dc_W = dc.reshape(
            dc.array([1., 1., 1., 1., 1., 1., 1., 1., 1.]), (1, 1, 3, 3))
        self.onnx_npr_su = np.array([
            12., 21., 27., 33., 24., 33., 54., 63., 72., 51., 63., 99., 108.,
            117., 81., 93., 144., 153., 162., 111., 72., 111., 117., 123., 84.
        ])
        self.onnx_npr_vl = np.array(
            [54., 63., 72., 99., 108., 117., 144., 153., 162.])
        self.onnx_npr_vl_s2 = np.array([54., 72., 144., 162., 234., 252.])
        self.onnx_npr_sp_s2 = np.array([
            12., 27., 24., 63., 108., 81., 123., 198., 141., 112., 177., 124.
        ])
        self.onnx_npr_ap_s2 = np.array(
            [21., 33., 99., 117., 189., 207., 171., 183.])
        self.onnx_dc_BIGW = dc.reshape(dc.array(list(np.ones(900))),
                                       (1, 1, 30, 30))
Esempio n. 6
0
#print("relu", t4.to_string())

#replace first few values in tensor with new values.
data = dc.fvec([1.0, 2.0, 3.0, 4.0])
t3.load(data)
#print(t3.to_string())

arr = dc.array([1, 2])
#print(arr)
arr2D = dc.array([[1, 2], [10, 20]])
#print(arr2D)
arrRand = dc.random(2, 3);
#print(arrRand)
empty = dc.empty(3, 2);
#print(empty)
zeros = dc.zeros(2, 2);
#print(zeros);
ones = dc.ones(2, 2);
#print(ones)
ranges = dc.arange(15, 3, 2)
#print(ranges)


def test_multiply(a,b):
    c = dc.multiply(a, b)
    #print(c)


#3D MatMul Test1
a = dc.make_tensor(2, 2, 2)
b = dc.make_tensor(2, 2, 2)
Esempio n. 7
0
    def test_assignments(self):
        assert not self.nullT
        assert self.zeros
        assert self.ones

        # Add
        temp_zeros = self.zeros.copy()
        temp_zeros += self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.ones)
        temp_zeros = self.zeros.copy()
        temp_zeros += 1
        dnnc_testing.utils.assert_equal(temp_zeros, self.ones)

        # Sub
        temp_ones = self.ones.copy()
        temp_ones -= self.ones
        dnnc_testing.utils.assert_equal(temp_ones, self.zeros)
        temp = self.f5_9
        temp -= dc.array([5])
        dnnc_testing.utils.assert_allclose(temp, self.f0_4)
        temp = self.i5_9
        temp -= dc.array([5]).asTypeInt()
        dnnc_testing.utils.assert_equal(temp, self.i0_4)
        temp = self.b5_9
        temp -= dc.array([5]).asTypeBool()
        dnnc_testing.utils.assert_equal(temp, dc.zeros(5).asTypeBool())

        # Mul
        temp_zeros = self.zeros.copy()
        temp_zeros *= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)
        temp_ones = self.ones.copy()
        temp_ones *= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.zeros)

        # TrueDiv
        temp_zeros = self.zeros.copy()
        temp_zeros /= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros.asTypeFloat(),
                                        self.zeros.asTypeFloat())
        temp_zeros = self.zeros.copy()
        temp_zeros /= 1
        dnnc_testing.utils.assert_equal(temp_zeros.asTypeFloat(),
                                        self.zeros.asTypeFloat())

        # FloorDiv
        temp_zeros = self.zeros.copy()
        temp_zeros //= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)
        temp_zeros = self.zeros.copy()
        temp_zeros //= 1
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)

        # Pow
        temp_zeros = self.zeros.copy()
        temp_zeros **= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)
        temp_ones = self.ones.copy()
        temp_ones **= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.ones)

        # Mod
        temp_zeros = self.zeros.copy()
        temp_zeros %= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)
        temp_zeros = self.zeros.copy()
        temp_zeros %= 1
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)

        # Left Shift
        temp_zeros = self.zeros.copy()
        temp_zeros <<= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros << self.ones)
        temp_ones = self.ones.copy()
        temp_ones <<= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.ones << 0)

        # Right Shift
        temp_zeros = self.zeros.copy()
        temp_zeros <<= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros >> self.ones)
        temp_ones = self.ones.copy()
        temp_ones <<= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.ones >> 0)

        # And
        temp_zeros = self.zeros.copy()
        temp_zeros &= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.zeros)
        temp_ones = self.ones.copy()
        temp_ones &= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.zeros)

        # Or
        temp_zeros = self.zeros.copy()
        temp_zeros |= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.ones)
        temp_ones = self.ones.copy()
        temp_ones |= 0
        dnnc_testing.utils.assert_equal(temp_ones, self.ones)

        # Xor
        temp_zeros = self.zeros.copy()
        temp_zeros ^= self.ones
        dnnc_testing.utils.assert_equal(temp_zeros, self.ones)
        temp_ones = self.ones.copy()
        temp_ones ^= 1
        dnnc_testing.utils.assert_equal(temp_ones, self.zeros)