def testAcosh2ndDerivative(self): w1 = np.random.rand(2, 2)*10 + 1.1 x = np.random.rand(2, 2) + 1 self.w1_torch = torch.tensor(w1, dtype=torch.float, requires_grad=True) self.x_torch = torch.tensor(x, dtype=torch.float) y_torch = (self.x_torch*self.w1_torch).acosh() dy_dw1_torch = grad_torch(y_torch, self.w1_torch, grad_outputs=ones_like_torch(y_torch), create_graph=True, retain_graph=True)[0] d2y_dw12_torch = grad_torch(dy_dw1_torch, self.w1_torch, grad_outputs=ones_like_torch(dy_dw1_torch))[0] cxt = tc.Context() self.w1_tc = tc.ml.optimizer.Variable.load(w1.shape, w1.flatten().tolist(), tc.F32) self.x_tc = tc.tensor.Dense.load(x.shape, x.flatten().tolist(), tc.F32) y_tc = (self.x_tc*self.w1_tc).acosh() _dy_dw1_tc = grad_tc(y_tc, ones_like_tc(y_tc), self.w1_tc) _d2y_dw2_tc = grad_tc(_dy_dw1_tc, ones_like_tc(_dy_dw1_tc), self.w1_tc) cxt.map = tc.Map({'the_first_derivative': _dy_dw1_tc, 'the_second_derivative': _d2y_dw2_tc}) result = HOST.post(ENDPOINT, cxt) dy_dw1_tc = result['the_first_derivative'] d2y_dw2_tc = result['the_second_derivative'] self.assertAllClose(dy_dw1_torch, dy_dw1_tc) self.assertAllClose(d2y_dw12_torch, d2y_dw2_tc)
def testAtanh2ndDerivative(self): w_torch = self.w1_torch.atanh() y_torch = self.x_torch*w_torch dy_dw1_torch = grad_torch(y_torch, self.w1_torch, grad_outputs=ones_like_torch(y_torch), create_graph=True, retain_graph=True)[0] d2y_dw12_torch = grad_torch(dy_dw1_torch, self.w1_torch, grad_outputs=ones_like_torch(dy_dw1_torch))[0] cxt = tc.Context() w_tc = self.w1_tc.atanh() y_tc = self.x_tc*w_tc _dy_dw1_tc = grad_tc(y_tc, ones_like_tc(y_tc), self.w1_tc) _d2y_dw2_tc = grad_tc(_dy_dw1_tc, ones_like_tc(_dy_dw1_tc), self.w1_tc) cxt.map = tc.Map({'the_first_derivative': _dy_dw1_tc, 'the_second_derivative': _d2y_dw2_tc}) result = HOST.post(ENDPOINT, cxt) dy_dw1_tc = result['the_first_derivative'] d2y_dw2_tc = result['the_second_derivative'] self.assertAllClose(dy_dw1_torch, dy_dw1_tc, 0.01) self.assertAllClose(d2y_dw12_torch, d2y_dw2_tc, 0.01)
def testMatMul2ndDerivative(self): y_torch = [email protected]_torch**2 + self.b1_torch y2_torch = ([email protected]_torch + self.b2_torch)**2 dy_dw1_torch = grad_torch(y2_torch, self.w1_torch, grad_outputs=ones_like_torch(y2_torch), create_graph=True, retain_graph=True)[0] d2y_dw12_torch = grad_torch(dy_dw1_torch, self.w1_torch, grad_outputs=ones_like_torch(dy_dw1_torch))[0] cxt = tc.Context() y_tc = [email protected]_tc**2 + self.b1_tc y_2tc = ([email protected]_tc + self.b2_tc)**2 _dy_dw1_tc = grad_tc(y_2tc, ones_like_tc(y_2tc), self.w1_tc) _d2y_dw2_tc = grad_tc(_dy_dw1_tc, ones_like_tc(_dy_dw1_tc), self.w1_tc) cxt.map = tc.Map({'the_first_derivative': _dy_dw1_tc, 'the_second_derivative': _d2y_dw2_tc}) result = HOST.post(ENDPOINT, cxt) dy_dw1_tc = result['the_first_derivative'] d2y_dw2_tc = result['the_second_derivative'] self.assertAllClose(dy_dw1_torch, dy_dw1_tc) self.assertAllClose(d2y_dw12_torch, d2y_dw2_tc)
def testParallelSVD_NgtM(self): num_matrices = 10 n = 3 m = 2 shape = [num_matrices, n, m] matrices = np.random.random(np.product(shape)).reshape(shape) tensor = tc.tensor.Dense.load(shape, matrices.flatten().tolist(), tc.F32) start = time.time() result = self.host.post( LIB_URI.append("svd"), tc.Map(A=tensor, l=n, epsilon=1e-7, max_iter=30)) elapsed = time.time() - start print(f"{num_matrices}x{n}x{m} SVD ran in {elapsed}s") U, s, V = result U = load_np(U) s = load_np(s) V = load_np(V) for i in range(num_matrices): expected = matrices[i] actual = (U[i], s[i], V[i]) self._check_svd(expected, actual)
def testSlogdet(self): x = np.random.randint(-100, 100, 64).reshape(4, 4, 4) expected_sign, expected_logdet = np.linalg.slogdet(x) x = tc.tensor.Dense.load(x.shape, x.flatten().tolist(), tc.F32) actual_sign, actual_logdet = self.host.post(LIB_URI.append("slogdet"), tc.Map(x=x)) actual_sign = load_np(actual_sign) actual_logdet = load_np(actual_logdet) self.assertTrue((actual_sign == expected_sign).all()) self.assertTrue((abs((actual_logdet - expected_logdet)) < 1e-4).all())
def testDeleteSlice(self): count = 50 values = [[v] for v in range(count)] keys = [[num2words(i)] for i in range(count)] remaining = sorted([k + v for k, v in zip(keys, values) if v[0] >= 40]) cxt = tc.Context() cxt.table = tc.table.Table(SCHEMA) cxt.inserts = [cxt.table.insert(k, v) for k, v in zip(keys, values)] cxt.delete = tc.After(cxt.inserts, cxt.table.delete(tc.Map(views=slice(40)))) cxt.result = tc.After(cxt.delete, cxt.table) result = self.host.post(ENDPOINT, cxt) self.assertEqual(result, expected(SCHEMA, remaining))
def testMatrixSVD_NltM(self): n = 4 m = 5 matrix = np.random.random(n * m).reshape(n, m) tensor = tc.tensor.Dense.load((n, m), matrix.flatten().tolist(), tc.F32) start = time.time() result = self.host.post( LIB_URI.append("svd"), tc.Map(A=tensor, l=n, epsilon=tc.F32(1e-7), max_iter=30)) elapsed = time.time() - start print(f"{n}x{m} SVD ran in {elapsed}s") U, s, V = result actual = (load_np(U), load_np(s), load_np(V)) self._check_svd(matrix, actual)