def test_LpNormalization2D (self): np_a = np.reshape(self.np_a, (3,3)) dc_a = dc.reshape(self.dc_a, (3,3)); npr=preprocessing.normalize(np_a,axis=1) print(npr) dcr = dc.lpnormalization(dc_a); print(dcr) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_GlobalAveragePool4D (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.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)
def test_DequantizeLinear4D(self): np_x = np.reshape(self.np_x, (2, 2, 2, 3)) dc_x = dc.reshape(self.dc_x, (2, 2, 2, 3)) npr = temp_dequantize_linear(np_x, self.np_x_scale, self.np_x_zero_point) dcr = dc.dequantize_linear(dc_x, self.dc_x_scale, self.dc_x_zero_point) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
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)
def test_LRN4D_2 (self): np_a = np.reshape(self.np_a, (2,2,1,6)) dc_a = dc.reshape(self.dc_a, (2,2,1,6)) square_sum = np.zeros((2,2,1,6)).astype(np.float32) for n, c, h,w in np.ndindex(np_a.shape): square_sum[n, c, h,w] = 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,w] ** 2) npr = np_a / ((self.bias + (self.alpha / self.size) * square_sum) ** self.beta) dcr = dc.lrn(dc_a,self.size) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_prelu_2d_broadcast(self): dc_a_reshaped = dc.reshape(self.dc_a, (6, 4)) np_test = self.prelu_true_1.copy() dc_test = dc.prelu(dc_a_reshaped, self.dc_slope_1) np.testing.assert_allclose(np_test.flatten(), np.array(dc_test.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_Log3D(self): np_a = np.reshape(self.np_a, (2, 4, 3)) dc_a = dc.reshape(self.dc_a, (2, 4, 3)) npr = np.log(np_a) dcr = dc.log(dc_a) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_LeakyRelu4D(self): np_a = np.reshape(self.np_a, (2, 2, 2, 3)) dc_a = dc.reshape(self.dc_a, (2, 2, 2, 3)) npr = self.np_a.copy() npr[self.np_a < 0] = npr[self.np_a < 0] * self.alpha dcr = dc.leakyrelu(self.dc_a, self.alpha) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_Elu2D(self): row = 8 column = 6 dc_a = dc.reshape(self.dc_a, (row, column)) npr = np.eye(row, column, self.k) dcr = dc.eye_like(dc_a, self.k) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
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)
def test_LpNormalization2D_4(self): np_a = np.reshape(self.np_a, (8, 3)) dc_a = dc.reshape(self.dc_a, (8, 3)) axis = 1 p = 1 npr = np.apply_along_axis(self.norm_1, axis, np_a) dcr = dc.lpnormalization(dc_a, p, axis) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
def test_Flatten4D(self): shape = (2, 2, 2, 3) axis = 4 np_a = np.reshape(self.np_a, shape) dc_a = dc.reshape(self.dc_a, shape) npr = temp_flatten(np_a, shape, axis) dcr = dc.flatten(dc_a, axis) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
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)
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)
def test_Hardmax3D_2(self): np_a = np.reshape(self.np_a, (2, 2, 6)) dc_a = dc.reshape(self.dc_a, (2, 2, 6)) 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)
def test_HardSigmoid3D_3(self): np_a = np.reshape(self.np_a, (4, 2, 3)) dc_a = dc.reshape(self.dc_a, (4, 2, 3)) self.alpha = 0.11 self.beta = 0.22 npr = np.clip((self.alpha * np_a + self.beta), 0.0, 1.0) dcr = dc.hardsigmoid(dc_a, self.alpha, self.beta) np.testing.assert_allclose(npr.flatten(), np.array(dcr.data()).astype(np.float32), rtol=1e-3, atol=1e-3)
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())
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())
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"
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())
def test_GlobalLpPool4D(self): np_a = np.reshape(self.np_a, (2, 2, 2, 3)) dc_a = dc.reshape(self.dc_a, (2, 2, 2, 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)
def test_GlobalLpPool2D(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.linalg.norm(np_a, ord=self.p, axis=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)
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)
arr = dc.array([1, 2]) #print(arr) arr2D = dc.array([[1, 2], [10, 20]]).astype('int') #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)) def test_multiply(a, b): c = dc.matmul(a, b) #print(c) #3D MatMul Test1 a = dc.array(2, 2, 2) b = dc.array(2, 2, 2) adata = dc.fvec([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]) bdata = dc.fvec([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)
# unittest.main() k = 2 len = 10 np_a = np.random.randn(len).astype(np.int8) dc_a = dc.array(list(np_a)) # print(np_a) # print(dc_a) row = 2 column = 5 np_b = np.reshape(np_a, (row, column)) dc_b = dc.reshape(dc_a, (row, column)) print(np_b) print(np.array(dc_b.data())) # print(np_a) # print(dc_a) npr = np.eye(row, column, k=k) dcr = dc.eye_like(dc_b, k) print(npr.flatten()) print(np.array(dcr.data())) print(npr) print(dcr)
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()))
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()))
def test_Add4D(self): np_a = np.reshape(self.np_a, (2, 2, 2, 3)) np_b = np.reshape(self.np_b, (2, 2, 2, 3)) dc_a = dc.reshape(self.dc_a, (2, 2, 2, 3)) dc_b = dc.reshape(self.dc_b, (2, 2, 2, 3))
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))
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)