Ejemplo n.º 1
0
    def setUp(self):
        self.len = 24
        self.np_a = np.random.randn(self.len).astype(np.float32)
        self.np_b = np.random.randn(self.len).astype(np.float32)
        self.dc_a = dc.array(list(self.np_a))

        self.dc_b = dc.array(list(self.np_b))
Ejemplo n.º 2
0
 def setUp(self):
     self.len = 24
     self.np_a_float = np.random.randn(self.len).astype(np.int32)
     self.np_b_float = np.random.randn(self.len).astype(np.int32)
     self.dc_a_float = dc.array(list(self.np_a_float))
     self.dc_b_float = dc.array(list(self.np_b_float))
     self.fmod_flag = 1
Ejemplo n.º 3
0
 def test_Slice2D (self):
     len   = np.sqrt(self.len).astype(int)
     np_a  = np.reshape(self.np_a, (len, len))
     # dc_a  = dc.reshape(self.dc_a, (len, len)) $ BUG (reported bug.py) - for now using a WA
     dc_a  = dc.reshape(self.dc_a, (64, 64))
     np_start  = np.random.randint(len/2)
     np_end    = np.random.randint(len/2 + 1, len)
     np_step   = np.random.randint(1, len-1)
     np_axes   = 1
     np_start2 = np.random.randint(len/2)
     np_end2   = np.random.randint(len/2 + 1, len)
     np_step2  = np.random.randint(1, len-1)
     np_axes2  = 2
     dc_start  = dc.array(2).asTypeULong()
     dc_end    = dc.array(2).asTypeULong()
     dc_axes   = dc.array(2).asTypeInt()
     dc_step   = dc.array(2).asTypeULong()
     dc_start[0] = np_start
     dc_end[0]   = np_end
     dc_axes[0]  = np_axes  
     dc_step[0]  = np_step 
     dc_start[1] = np_start2
     dc_end[1]   = np_end2
     dc_axes[1]  = np_axes2  
     dc_step[1]  = np_step2 
     npr   = np_a[np_start:np_end:np_step, np_start2:np_end2:np_step2]
     dcr   = dc.slice(dc_a, dc_start, dc_end, dc_axes, dc_step)
     np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32),
             rtol=1e-3, atol=1e-3)
Ejemplo n.º 4
0
    def setUp(self):
        self.len = 48

        self.np_float_a = np.random.randn(self.len).astype(np.float32)
        self.dc_float_a = dc.array(list(self.np_float_a))

        self.np_double_a = np.random.randn(self.len).astype(np.float64)
        self.dc_double_a = dc.array(list(self.np_double_a))
Ejemplo n.º 5
0
 def setUp(self):
     self.len = 24
     self.np_x = np.random.randn(self.len).astype(np.float32)
     self.np_x_scale = np.random.randn(1).astype(np.float32)
     self.np_x_zero_point = np.random.randn(1).astype(np.float32)
     self.dc_x = dc.array(list(self.np_x))
     self.dc_x_scale = dc.array(list(self.np_x_scale))
     self.dc_x_zero_point = dc.array(list(self.np_x_zero_point))
Ejemplo n.º 6
0
 def setUp(self):
     self.len = 12
     self.np_a = np.random.randint(12, size=self.len)
     self.np_b = np.random.randint(12, size=self.len)
     self.dc_a = dc.array(list(self.np_a)).asTypeInt()
     self.dc_b = dc.array(list(self.np_b)).asTypeInt()
     self.a_zero_point = dc.array(0).asTypeInt()
     self.b_zero_point = dc.array(0).asTypeInt()
Ejemplo n.º 7
0
 def setUp(self):
     self.len = 12
     self.np_a = np.random.randn(self.len).astype(np.float32)
     self.np_b = np.random.randn(self.len).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a));
     
     self.dc_b = dc.array(list(self.np_b));
     self.lst=[]
     self.lst.append(list(self.dc_a))
     self.lst.append(list(self.dc_b))
Ejemplo n.º 8
0
    def setUp(self):
        self.len = 12
        self.np_a = np.random.randn(self.len).astype(np.int)
        self.np_b = np.random.randn(self.len).astype(np.int)
        #self.np_a = np.arange(self.len).astype(np.float32)
        #self.np_b = np.arange(self.len).astype(np.float32)

        self.dc_a = dc.array(list(self.np_a))
        # dc.array gives float not int like-> [0.00000 1.000000]
        self.dc_b = dc.array(list(self.np_b))
Ejemplo n.º 9
0
 def setUp(self):
     self.len_a_b = 12
     self.len_c = 9
     self.alpha = 0.5
     self.beta = 0.5
     self.np_a = np.random.randn(self.len_a_b).astype(np.float32)
     self.np_b = np.random.randn(self.len_a_b).astype(np.float32)
     self.np_c = np.random.randn(self.len_c).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a))
     self.dc_b = dc.array(list(self.np_b))
     self.dc_c = dc.array(list(self.np_c))
Ejemplo n.º 10
0
 def setUp(self):
     self.len1 = 20
     self.len2 = 40
     self.np_a = np.random.randn(self.len1).astype(np.float32)
     self.np_b = np.random.randn(self.len2).astype(np.float32)
     self.np_c = np.random.randn(self.len1).astype(np.float32)
     self.np_d = np.random.randn(self.len2).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a))
     self.dc_b = dc.array(list(self.np_b))
     self.dc_c = dc.array(list(self.np_c))
     self.dc_d = dc.array(list(self.np_d))
     self.err = "operands could not be broadcast together with shapes (2,5,2) (4,5,2)   "
Ejemplo n.º 11
0
    def setUp(self):
        self.len = 24

        self.np_a = np.random.randn(self.len).astype(np.float32)
        self.dc_a = dc.array(list(self.np_a))

        self.np_slope_1 = np.random.randn(1).astype(np.float32)
        self.dc_slope_1 = dc.array(list(self.np_slope_1))

        self.np_slope = np.random.randn(self.len).astype(np.float32)
        self.dc_slope = dc.array(list(self.np_slope))

        self.prelu_true = prelu_util(self.np_a, self.np_slope)
        self.prelu_true_1 = prelu_util(self.np_a, self.np_slope_1)
Ejemplo n.º 12
0
 def test_InstanceNormalization4D(self):
     np_a = np.reshape(self.np_a, (2, 2, 2, 3))
     dc_a = dc.reshape(self.dc_a, (2, 2, 2, 3))
     scale = np.reshape(self.scale[0:2], (1, 2, 1, 1))
     B = np.reshape(self.B[0:2], (1, 2, 1, 1))
     np_a_mean = np_a.mean(axis=(0, 2, 3), keepdims=True)
     np_a_var = np_a.var(axis=(0, 2, 3), keepdims=True)
     npr = scale * (np_a - np_a_mean) / np.sqrt(np_a_var + self.epsilon) + B
     dcr = dc.instancenormalization(
         dc_a, dc.reshape(dc.array(list(self.scale[0:2])), (2)),
         dc.reshape(dc.array(list(self.B[0:2])), (2)), self.epsilon)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Ejemplo n.º 13
0
 def setUp(self):
     self.len = 24
     self.np_a = np.random.randn(self.len).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a))
     self.scale = np.random.randn(4).astype(np.float32)
     self.B = np.random.randn(4).astype(np.float32)
     self.epsilon = 1e-05
Ejemplo n.º 14
0
 def setUp(self):
     np.seterr(
         all="ignore")  # Numpy raises warnings while calling np.sqrt()
     self.len = 24
     self.np_a = np.random.randn(self.len).astype(np.float64)
     # self.np_a += 2 * self.np_a
     self.dc_a = dc.array(list(self.np_a))
Ejemplo n.º 15
0
 def setUp(self):
     self.len = 24
     self.np_a = np.random.randn(self.len).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a))
     self.alpha = 0.0001
     self.beta = 0.75
     self.bias = 1.0
     self.size = 2
Ejemplo n.º 16
0
 def setUp(self):
     self.len = 24
     self.np_a = np.random.randn(self.len)
     self.np_a.ravel()[np.random.choice(self.np_a.size, 5,
                                        replace=False)] = np.inf
     self.np_a.ravel()[np.random.choice(self.np_a.size, 5,
                                        replace=False)] = -np.inf
     self.dc_a = dc.array(list(self.np_a))
Ejemplo n.º 17
0
 def test_Slice4D (self):
     len   = np.sqrt(np.sqrt(self.len)).astype(int)
     np_a  = np.reshape(self.np_a, (len, len, len, len))
     # dc_a  = dc.reshape(self.dc_a, (len, len, len))
     dc_a  = dc.reshape(self.dc_a, (8, 8, 8, 8))
     np_start  = np.random.randint(len/2)
     np_end    = np.random.randint(len/2 + 1, len)
     np_step   = np.random.randint(1, len-1)
     np_axes   = 1
     np_start2 = np.random.randint(len/2)
     np_end2   = np.random.randint(len/2 + 1, len)
     np_step2  = np.random.randint(1, len-1)
     np_axes2  = 2
     np_start3 = np.random.randint(len/2)
     np_end3   = np.random.randint(len/2 + 1, len)
     np_step3  = np.random.randint(1, len-1)
     np_axes3  = 3
     np_start4 = np.random.randint(len/2)
     np_end4   = np.random.randint(len/2 + 1, len)
     np_step4  = np.random.randint(1, len-1)
     np_axes4  = 4
     dc_start  = dc.array(4).asTypeULong()
     dc_end    = dc.array(4).asTypeULong()
     dc_axes   = dc.array(4).asTypeInt()
     dc_step   = dc.array(4).asTypeULong()
     dc_start[0] = np_start
     dc_end[0]   = np_end
     dc_axes[0]  = np_axes  
     dc_step[0]  = np_step 
     dc_start[1] = np_start2
     dc_end[1]   = np_end2
     dc_axes[1]  = np_axes2  
     dc_step[1]  = np_step2 
     dc_start[2] = np_start3
     dc_end[2]   = np_end3
     dc_axes[2]  = np_axes3  
     dc_step[2]  = np_step3 
     dc_start[3] = np_start4
     dc_end[3]   = np_end4
     dc_axes[3]  = np_axes4  
     dc_step[3]  = np_step4 
     npr   = np_a[np_start:np_end:np_step, np_start2:np_end2:np_step2, np_start3:np_end3:np_step3, np_start4:np_end4:np_step4]
     dcr   = dc.slice(dc_a, dc_start, dc_end, dc_axes, dc_step)
     np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32),
             rtol=1e-3, atol=1e-3)
Ejemplo n.º 18
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()
Ejemplo n.º 19
0
    def test_Broadcast (self):
        np_a = np.reshape(self.np_a, (2,2,2,3))
        np_b = self.np_b[0:3]
        dc_a = dc.reshape(self.dc_a, (2,2,2,3));
        dc_b = dc.array(list(np_b));

        # github issue # 31, resolved.
        npr = np.subtract(np_a, np_b);
        dcr = dc.sub(dc_a, dc_b);
        np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
Ejemplo n.º 20
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())
Ejemplo n.º 21
0
 def test_Slice1D (self):
     len   = self.len
     np_a  = self.np_a
     dc_a  = self.dc_a
     np_start = np.random.randint(len/2)
     np_end   = np.random.randint(len/2 + 1, len)
     np_step  = np.random.randint(1, len-1)
     np_axes  = 1
     dc_start = dc.array(1).asTypeULong()
     dc_end   = dc.array(1).asTypeULong()
     dc_axes  = dc.array(1).asTypeInt()
     dc_step  = dc.array(1).asTypeULong()
     dc_start[0] = np_start
     dc_end[0]   = np_end
     dc_axes[0]  = np_axes  
     dc_step[0]  = np_step 
     npr   = np_a[np_start:np_end:np_step]
     dcr   = dc.slice(dc_a, dc_start, dc_end, dc_axes, dc_step)
     np.testing.assert_allclose(npr, np.array(dcr.data()).astype(np.float32),
             rtol=1e-3, atol=1e-3)
Ejemplo n.º 22
0
    def setUp(self):
        self.len = 24
        self.np_a = np.random.randn(self.len).astype(np.float32)
        self.dc_a = dc.array(list(self.np_a))

        def test_GlobalAveragePool2D (self):
            np_a = np.reshape(self.np_a, (4,6))
            dc_a = dc.reshape(self.dc_a, (4,6))
            if(np.ndim(np_a)<=2):
                npr = np_a
            else:
                spatial_shape = np.ndim(np_a) -2
                npr = np.average(np_a, axis=tuple(range(2, spatial_shape + 2)))
                for _ in range(spatial_shape):
                    npr = np.expand_dims(npr, -1)
            dcr = dc.global_average_pool(dc_a)
            np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32),
                    rtol=1e-3, atol=1e-3)
Ejemplo n.º 23
0
    def setUp(self):
        self.len = 48

        self.np_bool_a = np.random.randn(self.len).astype(np.bool)
        self.np_bool_b = np.random.randn(self.len).astype(np.bool)
        self.dc_bool_a = dc.array(list(self.np_bool_a))
        self.dc_bool_b = dc.array(list(self.np_bool_b))

        self.np_int_a = np.random.randn(self.len).astype(np.int)
        self.np_int_b = np.random.randn(self.len).astype(np.int)
        self.dc_int_a = dc.array(list(self.np_int_a))
        self.dc_int_b = dc.array(list(self.np_int_b))

        self.np_float_a = np.random.randn(self.len).astype(np.float32)
        self.np_float_b = np.random.randn(self.len).astype(np.float32)
        self.dc_float_a = dc.array(list(self.np_float_a))
        self.dc_float_b = dc.array(list(self.np_float_b))

        self.np_double_a = np.random.randn(self.len).astype(np.float64)
        self.np_double_b = np.random.randn(self.len).astype(np.float64)
        self.dc_double_a = dc.array(list(self.np_double_a))
        self.dc_double_b = dc.array(list(self.np_double_b))
Ejemplo n.º 24
0
    def setUp(self):
        self.len_a_b = 48
        self.len_c = 64
        self.alpha = 0.5
        self.beta = 0.5

        self.np_float_a = np.random.randn(self.len_a_b).astype(np.float32)
        self.np_float_b = np.random.randn(self.len_a_b).astype(np.float32)
        self.np_float_c = np.random.randn(self.len_c).astype(np.float32)
        self.dc_float_a = dc.array(list(self.np_float_a))
        self.dc_float_b = dc.array(list(self.np_float_b))
        self.dc_float_c = dc.array(list(self.np_float_c))

        self.np_double_a = np.random.randn(self.len_a_b).astype(np.double)
        self.np_double_b = np.random.randn(self.len_a_b).astype(np.double)
        self.np_double_c = np.random.randn(self.len_c).astype(np.double)
        self.dc_double_a = dc.array(list(self.np_double_a))
        self.dc_double_b = dc.array(list(self.np_double_b))
        self.dc_double_c = dc.array(list(self.np_double_c))
Ejemplo n.º 25
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)
Ejemplo n.º 26
0
 def setUp(self):
     self.len = 48
     self.k = np.random.randint(low=-10, high=10)
     # self.k = 10
     self.np_a = np.random.randn(self.len).astype(np.float32)
     self.dc_a = dc.array(list(self.np_a))
Ejemplo n.º 27
0
 def setUp(self):
     self.len = 24
     self.np_a = np.random.randint(24, size=self.len)
     self.np_b = np.random.randint(24, size=self.len)
     self.dc_a = dc.array(list(self.np_a))
     self.dc_b = dc.array(list(self.np_b))
Ejemplo n.º 28
0
 def setUp(self):
     self.len = 24
     self.np_a = np.random.randint(low=2, high=10000, size=self.len)
     self.dc_a = dc.array(list(self.np_a))
Ejemplo n.º 29
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))
Ejemplo n.º 30
0
t3 = dc.make_tensor(2,3,4)
#print("old shape", t1.shape())
new_shape = dc.ivec([2,12])
t3.reshape(new_shape)
#print("new shape", t1.shape())

#t4 = dc.thresholded_relu(t1);
#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)