Пример #1
0
 def test_Gemm2D_float_2(self):
     shape_a = (8, 6)
     shape_b = (8, 6)
     shape_c = (8, 8)
     transA = 0
     transB = 1
     np_float_a = np.reshape(self.np_float_a, shape_a)
     np_float_b = np.reshape(self.np_float_b, shape_b)
     np_float_c = np.reshape(self.np_float_c, shape_c)
     dc_float_a = dc.reshape(self.dc_float_a, shape_a)
     dc_float_b = dc.reshape(self.dc_float_b, shape_b)
     dc_float_c = dc.reshape(self.dc_float_c, shape_c)
     npr = temp_gemm(np_float_a, np_float_b, np_float_c, self.alpha,
                     self.beta, transA, transB)
     dcr = dc.gemm(dc_float_a, dc_float_b, dc_float_c, self.alpha,
                   self.beta, transA, transB)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Пример #2
0
 def test_Gemm2D_double_3(self):
     shape_a = (6, 8)
     shape_b = (6, 8)
     shape_c = (8, 8)
     transA = 1
     transB = 0
     np_double_a = np.reshape(self.np_double_a, shape_a)
     np_double_b = np.reshape(self.np_double_b, shape_b)
     np_double_c = np.reshape(self.np_double_c, shape_c)
     dc_double_a = dc.reshape(self.dc_double_a, shape_a)
     dc_double_b = dc.reshape(self.dc_double_b, shape_b)
     dc_double_c = dc.reshape(self.dc_double_c, shape_c)
     npr = temp_gemm(np_double_a, np_double_b, np_double_c, self.alpha,
                     self.beta, transA, transB)
     dcr = dc.gemm(dc_double_a, dc_double_b, dc_double_c, self.alpha,
                   self.beta, transA, transB)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.double),
                                rtol=1e-3,
                                atol=1e-3)
Пример #3
0
 def test_Hardmax2D_2(self):
     np_a = np.reshape(self.np_a, (3, 8))
     dc_a = dc.reshape(self.dc_a, (3, 8))
     self.coerce(np_a)
     np_a = np.reshape(np_a, (self.axis1, self.axis2))
     npr = (np_a.max(0, keepdims=1) == np_a).astype(float)
     dcr = dc.hardmax(dc_a, self.axis)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Пример #4
0
 def test_Flatten2D_bool(self):
     axis = 2
     shape = (8, 6)
     np_bool_a = np.reshape(self.np_bool_a, shape)
     dc_bool_a = dc.reshape(self.dc_bool_a, shape)
     npr = temp_flatten(np_bool_a, shape, axis)
     dcr = dc.flatten(dc_bool_a, axis)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.bool),
                                rtol=1e-3,
                                atol=1e-3)
     np.testing.assert_equal(npr.shape, dcr.shape())
Пример #5
0
 def test_Flatten4D_double(self):
     axis = 3
     shape = (4, 2, 2, 3)
     np_double_a = np.reshape(self.np_double_a, shape)
     dc_double_a = dc.reshape(self.dc_double_a, shape)
     npr = temp_flatten(np_double_a, shape, axis)
     dcr = dc.flatten(dc_double_a, axis)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float64),
                                rtol=1e-3,
                                atol=1e-3)
     np.testing.assert_equal(npr.shape, dcr.shape())
Пример #6
0
 def test_Flatten3D_float(self):
     axis = 2
     shape = (4, 4, 3)
     np_float_a = np.reshape(self.np_float_a, shape)
     dc_float_a = dc.reshape(self.dc_float_a, shape)
     npr = temp_flatten(np_float_a, shape, axis)
     dcr = dc.flatten(dc_float_a, axis)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
     np.testing.assert_equal(npr.shape, dcr.shape())
Пример #7
0
 def test_GlobalMaxPool4D_1(self):
     np_a = np.reshape(self.np_a, (2, 2, 2, 3))
     dc_a = dc.reshape(self.dc_a, (2, 2, 2, 3))
     spatial_shape = np.ndim(np_a) - 2
     npr = np.max(np_a, axis=tuple(range(2, spatial_shape + 2)))
     for _ in range(spatial_shape):
         npr = np.expand_dims(npr, -1)
     dcr = dc.global_max_pool(dc_a)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Пример #8
0
    def test_Softmax2D(self):
        np_a = np.reshape(self.np_a, (6, 4))
        dc_a = dc.reshape(self.dc_a, (6, 4))
        self.coerce(np_a)
        np_a = np.reshape(np_a, (self.axis1, self.axis2))
        npr = softmax_2d(np_a)

        dcr = dc.softmax(dc_a, self.axis)
        np.testing.assert_allclose(npr.flatten(),
                                   np.array(dcr.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)
Пример #9
0
 def test_error_message(self):
     dc_a = dc.reshape(self.dc_a, (2, 5, 2))
     dc_b = dc.reshape(self.dc_b, (4, 5, 2))
     np_a = np.reshape(self.np_a, (2, 5, 2))
     np_b = np.reshape(self.np_b, (4, 5, 2))
     try:
         np_sum = np.add(np_a, np_b)
     except:
         type, val, tb = sys.exc_info()
         np_err = val.__str__()
         assert (np_err[0:65] == self.err[0:65]
                 ), "ASSERT FAILED for numpy error message"
     try:
         dc_sum = dc.add(dc_a, dc_b)
     except:
         type, val, tb = sys.exc_info()
         dc_err = val.__str__()
         assert (dc_err[0:65] == self.err[0:65]
                 ), "ASSERT FAILED for dc error message"
         assert (dc_err[0:65] == np_err[0:65]
                 ), "ASSERT FAILED for matching numpy and dc error message"
Пример #10
0
 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)
Пример #11
0
 def test_GlobalLpPool3D_3(self):
     np_a = np.reshape(self.np_a, (8, 1, 3))
     dc_a = dc.reshape(self.dc_a, (8, 1, 3))
     np_a = np.reshape(np_a, (np_a.shape[0], np_a.shape[1], end_axis(np_a)))
     npr = np.linalg.norm(np_a, ord=self.p, axis=2)
     spatial_shape = np.ndim(np_a) - 2
     for _ in range(spatial_shape):
         npr = np.expand_dims(npr, -1)
     dcr = dc.global_lp_pool(dc_a, self.p)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Пример #12
0
    def setUp(self):
        self.num_directions = 1
        self.seq_length = 1
        self.input_size = 4
        self.batch_size = 3
        self.hidden_size = 5

        self.np_x = np.random.randn(self.seq_length * self.batch_size * self.input_size).astype(np.float32)
        self.dc_x = dc.array(list(self.np_x))

        self.np_w = np.random.randn(self.num_directions * 4 * self.hidden_size * self.input_size).astype(np.float32)
        self.dc_w = dc.array(list(self.np_w))

        self.np_r = np.random.randn(self.num_directions * 4 * self.hidden_size * self.hidden_size).astype(np.float32)
        self.dc_r = dc.array(list(self.np_r))

        self.np_b = np.random.randn(self.num_directions * 8 * self.hidden_size).astype(np.float32)
        self.dc_b = dc.array(list(self.np_b))

        self.np_h = np.random.randn(self.num_directions * self.batch_size * self.hidden_size).astype(np.float32)
        self.dc_h = dc.array(list(self.np_h))

        self.np_c = np.random.randn(self.num_directions * self.batch_size * self.hidden_size).astype(np.float32)
        self.dc_c = dc.array(list(self.np_c))

        self.np_p = np.random.randn(self.num_directions * 3 * self.hidden_size).astype(np.float32)
        self.dc_p = dc.array(list(self.np_p))





        self.np_x = np.reshape(self.np_x, (self.seq_length, self.batch_size, self.input_size))
        self.dc_x = dc.reshape(self.dc_x, (self.seq_length, self.batch_size, self.input_size))

        self.np_w = np.reshape(self.np_w, (self.num_directions, 4 * self.hidden_size, self.input_size))
        self.dc_w = dc.reshape(self.dc_w, (self.num_directions, 4 * self.hidden_size, self.input_size))

        self.np_r = np.reshape(self.np_r, (self.num_directions, 4 * self.hidden_size, self.hidden_size))
        self.dc_r = dc.reshape(self.dc_r, (self.num_directions, 4 * self.hidden_size, self.hidden_size))

        self.np_b = np.reshape(self.np_b, (self.num_directions, 8 * self.hidden_size))
        self.dc_b = dc.reshape(self.dc_b, (self.num_directions, 8 * self.hidden_size))
        
        self.np_h = np.reshape(self.np_h, (self.num_directions, self.batch_size, self.hidden_size))
        self.dc_h = dc.reshape(self.dc_h, (self.num_directions, self.batch_size, self.hidden_size))

        self.np_c = np.reshape(self.np_c, (self.num_directions, self.batch_size, self.hidden_size))
        self.dc_c = dc.reshape(self.dc_c, (self.num_directions, self.batch_size, self.hidden_size))

        self.np_p = np.reshape(self.np_p, (self.num_directions, 3 * self.hidden_size))
        self.dc_p = dc.reshape(self.dc_p, (self.num_directions, 3 * self.hidden_size))
Пример #13
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)
Пример #14
0
 def test_LRN3D_2(self):
     np_a = np.reshape(self.np_a, (2, 2, 6))
     dc_a = dc.reshape(self.dc_a, (2, 2, 6))
     square_sum = np.zeros((2, 2, 6)).astype(np.float32)
     for n, c, h in np.ndindex(np_a.shape):
         square_sum[n, c, h] = sum(
             np_a[n,
                  max(0, c - int(math.floor((self.size - 1) / 2))
                      ):min(5, c + int(math.ceil((self.size - 1) / 2)) + 1),
                  h]**2)
     npr = np_a / ((self.bias +
                    (self.alpha / self.size) * square_sum)**self.beta)
     dcr = dc.lrn(dc_a, self.size, self.alpha, self.beta, self.bias)
     np.testing.assert_allclose(npr.flatten(),
                                np.array(dcr.data()).astype(np.float32),
                                rtol=1e-3,
                                atol=1e-3)
Пример #15
0
    def test_ReduceL2_3D(self):
        np_a = np.reshape(self.np_a, (2, 4, 3))
        dc_a = dc.reshape(self.dc_a, (2, 4, 3))

        npr = np.sqrt(np.sum(a=np.square(np_a), axis=(0), keepdims=False))
        dcr = dc.reduce_L2(dc_a, [0])
        np.testing.assert_allclose(npr.flatten(),
                                   np.array(dcr.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)

        npr2 = np.sqrt(np.sum(a=np.square(np_a), axis=(0, 2), keepdims=False))
        dcr2 = dc.reduce_L2(dc_a, [0, 2])
        np.testing.assert_allclose(npr2.flatten(),
                                   np.array(dcr2.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)

        npr3 = np.sqrt(
            np.sum(a=np.square(np_a), axis=(0, 1, 2), keepdims=False))
        dcr3 = dc.reduce_L2(dc_a, [])
        np.testing.assert_allclose(npr3.flatten(),
                                   np.array(dcr3.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)

        npr4 = np.sqrt(np.sum(a=np.square(np_a), axis=(0), keepdims=True))
        dcr4 = dc.reduce_L2(dc_a, [0], 1)
        np.testing.assert_allclose(npr4.flatten(),
                                   np.array(dcr4.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)

        npr5 = np.sqrt(np.sum(a=np.square(np_a), axis=(0, 1, 2),
                              keepdims=True))
        dcr5 = dc.reduce_L2(dc_a, [], 1)
        np.testing.assert_allclose(npr5.flatten(),
                                   np.array(dcr5.data()).astype(np.float32),
                                   rtol=1e-3,
                                   atol=1e-3)
Пример #16
0
 def test_Slice3D (self):
     len   = np.cbrt(self.len).astype(int)
     np_a  = np.reshape(self.np_a, (len, len, len))
     # dc_a  = dc.reshape(self.dc_a, (len, len, len))
     dc_a  = dc.reshape(self.dc_a, (16, 16, 16))
     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   = 0
     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  = 1
     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  = 2
     dc_start  = dc.array(3).asTypeInt()
     dc_end    = dc.array(3).asTypeInt()
     dc_axes   = dc.array(3).asTypeInt()
     dc_step   = dc.array(3).asTypeInt()
     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
     npr   = np_a[np_start:np_end:np_step, np_start2:np_end2:np_step2, np_start3:np_end3:np_step3]
     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)
Пример #17
0
def test_non_binary():

    t1 = dc.array(2, 3)
    t2 = dc.array(3, 2)

    mul = dc.matmul(t1, t2)
    #print ("multiplication : " , mul.to_string())

    t3 = dc.array(2, 3, 4)
    #print("old shape", t1.shape())
    new_shape = dc.vectorSizeT([2, 12])
    t3.reshape(new_shape)
    t3.reshape(4, 6)
    t3.reshape((4, 6))
    t3.reshape([4, 6])
    #print("new shape", t1.shape())

    py_list = list(t3)
    # convert tensor to python list
    py_tuple = tuple(t3)
    # convert tensor to python tuple
    np_ary = t3.numpy()
    # convert to numpy array

    #t4 = dc.thresholded_relu(t1);
    #print("relu", t4.to_string())

    #replace first few values in tensor with new values.
    data = dc.vectorFloat([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]]).asTypeInt()
    #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)

    dc.reshape(arr2D, (1, 4))

    #3D MatMul Test1
    a = dc.array(2, 2, 2)
    b = dc.array(2, 2, 2)
    adata = dc.vectorFloat([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
    bdata = dc.vectorFloat([8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0])
    a.load(adata)
    b.load(bdata)
    test_multiply(a, b)

    #3D MatMul Test2
    a = dc.array(2, 2, 3)
    b = dc.array(2, 3, 2)
    adata = dc.vectorFloat(
        [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0])
    bdata = dc.vectorFloat(
        [12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0])
    a.load(adata)
    b.load(bdata)
    test_multiply(a, b)
Пример #18
0
 def test_IsNaN2D_3 (self):
     np_a = np.reshape(self.np_a, (12,2))
     dc_a = dc.reshape(self.dc_a, (12,2))
     npr = np.isnan(np_a)
     dcr = dc.isnan(dc_a)
     np.testing.assert_array_equal(npr.flatten(), np.array(dcr.data()))
Пример #19
0
 def test_IsInf2D_3(self):
     np_a = np.reshape(self.np_a, (12, 2))
     dc_a = dc.reshape(self.dc_a, (12, 2))
     npr = Isinf(np_a, self.detect_positive, self.detect_negative)
     dcr = dc.isinf(dc_a, self.detect_positive, self.detect_negative)
     np.testing.assert_array_equal(npr.flatten(), np.array(dcr.data()))
Пример #20
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))
Пример #21
0
    def MatMul3D (self):
        dc_a = dc.reshape(self.dc_a, (self.N, self.N))
        dc_b = dc.reshape(self.dc_b, (self.N, self.N))

        dcr = dc.matmul(dc_a, dc_b);