def test_batch_svd(self): m = 9 n = 5 q = 7 k = 5 for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((q, m, n)) A = ctf.astensor(A, dtype=dt) [U, S, VT] = ctf.svd_batch(A, k) for i in range(q): self.assertTrue( allclose(A[i], ctf.dot(U[i], ctf.dot(ctf.diag(S[i]), VT[i])))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i]))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i], VT[i].T()))) m = 7 n = 8 q = 2 k = 3 for dt in [np.float32, np.float64, np.complex64, np.complex128]: A = ctf.random.random((q, m, n)) A = ctf.astensor(A, dtype=dt) [U, S, VT] = ctf.svd_batch(A, k) for i in range(q): [nU, nS, nVT] = np.linalg.svd(A[i].to_nparray()) self.assertTrue( allclose( np.dot(nU[:, :k], np.dot(np.diag(nS[:k]), nVT[:k, :])), ctf.dot(U[i], ctf.dot(ctf.diag(S[i]), VT[i])))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i]))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i], VT[i].T())))
def test__sub__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1-.5, a0-.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1-a2, a0-(a0*.2+1j))) self.assertTrue(allclose(a1-a0, a0-a0))
def test__pow__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1**.5, a0**.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1**a2, a0**(a0*.2+1j))) self.assertTrue(allclose(a1**a0, a0**a0))
def test__div__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1/.5, a0/.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1/a2, a0/(a0*.2+1j))) self.assertTrue(allclose(a1/a0, a0/a0))
def test_power(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.power(a1, .5), a0**.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(ctf.power(a1, a2), a0**(a0*.2+1j))) self.assertTrue(allclose(ctf.power(a1, a0), a0**a0))
def test_hstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9,)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) a1 = numpy.ones((2,4)) a2 = ctf.astensor(numpy.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) na2 = numpy.ones((2,5))+0j self.assertTrue(ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1,na2)))) a1 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5,)) a1 = ctf.astensor(numpy.ones((2,4,2))) a2 = ctf.astensor(numpy.ones((2,5,2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9,2))
def test_hstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9, )) a1 = ctf.astensor(numpy.ones((2, 4))) a2 = ctf.astensor(numpy.ones((2, 5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) a1 = ctf.astensor(numpy.ones((2, 4))) a2 = ctf.astensor(numpy.ones((2, 5)) + 0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) a1 = numpy.ones((2, 4)) a2 = ctf.astensor(numpy.ones((2, 5)) + 0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9)) na2 = numpy.ones((2, 5)) + 0j self.assertTrue( ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1, na2)))) a1 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5, )) a1 = ctf.astensor(numpy.ones((2, 4, 2))) a2 = ctf.astensor(numpy.ones((2, 5, 2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2, 9, 2))
def test_einsum_mix_types(self): a0 = numpy.random.random((5, 1, 4, 2, 3)).astype(numpy.complex) + 1j b0 = numpy.random.random((5, 1, 11)).astype(numpy.float32) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c0 = numpy.einsum('ijklm,ijn->', a0, b0) c1 = ctf.einsum('ijklm,ijn->', a1, b1) self.assertTrue(allclose(c0, c1))
def test_complex(self): a0 = numpy.arange(27.).reshape(3, 3, 3) b0 = numpy.arange(27.).reshape(3, 3, 3) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) .2 * b1.i("ijk") << .7 * a1.i("kij") b0 = .2 * b0 + .7 * a0.transpose([1, 2, 0]) self.assertTrue(allclose(b0, b1))
def getDenseOmega(T,U,V,W,regParam,omega,I,J,K,r,idx,string): if (string =="i"): omega_curr = ctf.to_nparray(omega[idx,:,:].reshape(J*K)) omega_sum = np.cumsum(omega_curr).tolist() omega_sum.insert(0,0) del omega_sum[-1] #print("omega prefix sum: ", omega_sum) l = [] for x,y in enumerate(omega_sum): if omega_curr[x] != 0: l.append((x,int(y))) #print(l) num_nonzero = len(l) # form dense omega matrix temp = np.zeros((J*K,len(l))) for x,y in l: temp[x][y] = 1 #print("omega_dense: ", omega_dense) omega_dense = ctf.astensor(temp) #print("before", (omega_dense, omega_dense.shape)) omega_dense = omega_dense.reshape(J,K,num_nonzero) #print("after", (omega_dense, omega_dense.shape)) if (string =="j"): omega_curr = ctf.to_nparray(omega[:,idx,:].reshape(I*K)) omega_sum = np.cumsum(omega_curr).tolist() omega_sum.insert(0,0) del omega_sum[-1] l = [] for x,y in enumerate(omega_sum): if omega_curr[x] != 0: l.append((x,int(y))) num_nonzero = len(l) temp = np.zeros((I*K,len(l))) for x,y in l: temp[x,y] = 1 omega_dense = ctf.astensor(temp) omega_dense = omega_dense.reshape(I,K,num_nonzero) if (string =="k"): omega_curr = ctf.to_nparray(omega[:,:,idx].reshape(I*J)) omega_sum = np.cumsum(omega_curr).tolist() omega_sum.insert(0,0) del omega_sum[-1] l = [] for x,y in enumerate(omega_sum): if omega_curr[x] != 0: l.append((x,int(y))) num_nonzero = len(l) temp = np.zeros((I*J,len(l))) for x,y in l: temp[x][y] = 1 omega_dense = ctf.astensor(temp) omega_dense = omega_dense.reshape(I,J,num_nonzero) return num_nonzero,omega_dense
def test_dot_1d(self): a1 = numpy.ones(4) self.assertTrue( allclose(ctf.dot(ctf.astensor(a1), a1), numpy.dot(a1, a1))) self.assertTrue( allclose(ctf.dot(a1 + 1j, ctf.astensor(a1)), numpy.dot(a1 + 1j, a1))) a2 = ctf.astensor(a1).dot(a1 + 0j) self.assertTrue(a2.dtype == numpy.complex128)
def test_diagonal(self): a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.astensor([0,4,8]))) self.assertTrue(ctf.all(a1 == ctf.diagonal(numpy.arange(9).reshape(3,3)))) try: a1.diagonal() except ValueError: # a1 needs to be 2d array pass
def test_einsum_mix_types(self): a0 = numpy.random.random((5, 1, 4, 2, 3)).astype(numpy.complex)+1j b0 = numpy.random.random((5, 1, 11)).astype(numpy.float32) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c0 = numpy.einsum('ijklm,ijn->', a0, b0) c1 = ctf.einsum('ijklm,ijn->', a1, b1).to_nparray() self.assertTrue(c1.dtype == numpy.complex) self.assertTrue(allclose(c0, c1))
def test_TTTP_vec(self): A = numpy.random.random((4, 3, 5)) u = numpy.random.random((4, )) v = numpy.random.random((5, )) ans = numpy.einsum("ijk,i,k->ijk", A, u, v) cA = ctf.astensor(A) cu = ctf.astensor(u) cv = ctf.astensor(v) cans = ctf.TTTP(cA, [cu, None, cv]) self.assertTrue(allclose(ans, cans))
def energy(self, t1, t2, eris): nocc, nvir = t1.shape e = einsum('ia,ia->', eris.fov, t1) * 2 t1 = ctf.astensor(t1) t2 = ctf.astensor(t2) e += einsum('ijab,iajb->', t2, eris.ovov) * 2 e -= einsum('ijab,ibja->', t2, eris.ovov) e += einsum('iajb,ia,jb->', eris.ovov, t1, t1) * 2 e -= einsum('jaib,ia,jb->', eris.ovov, t1, t1) return e.to_nparray()
def prop_back(self, A, X, y, mask): gradients = {} preds = self.forward(A, X) if self.package != "ctf": zero_indices = np.logical_not(mask) preds[zero_indices] = 0 y[zero_indices] = 0 softmax_loss = softmax_grad(preds, y) gradients["W" + str(self.num_layers)] = self.layer_output_nonlinear[ "layer" + str(self.num_layers - 1)].T @ A.T @ softmax_loss last_loss = softmax_loss for i in range(self.num_layers - 1, 0, -1): out_grad = A.T @ last_loss @ self.params["W" + str(i + 1)].T grad = out_grad * self.nonlinearity_grad( self.layer_output["layer" + str(i)]) gradients["W" + str(i)] = self.layer_output[ "layer" + str(i - 1)].T @ A.T @ grad else: zero_indices = np.logical_not(mask) preds = ctf.to_nparray(preds) y = ctf.to_nparray(y) preds[zero_indices] = 0 y[zero_indices] = 0 preds = ctf.astensor(preds) y = ctf.astensor(y) softmax_loss = softmax_grad(preds, y) gradients["W" + str(self.num_layers)] = self.layer_output_nonlinear[ "layer" + str(self.num_layers - 1)].T() @ A.T() @ softmax_loss last_loss = softmax_loss for i in range(self.num_layers - 1, 0, -1): out_grad = A.T() @ last_loss @ self.params["W" + str(i + 1)].T() grad = out_grad * self.nonlinearity_grad( self.layer_output["layer" + str(i)]) gradients["W" + str(i)] = self.layer_output[ "layer" + str(i - 1)].T() @ A.T() @ grad print(gradients["W" + str(i)].sp) for i in range(self.num_layers): gradients["W" + str(i + 1)] += self.weight_decay * self.params["W" + str(i + 1)] return gradients
def getOmegaOld(T): if not T.sp: omegactf = ((T != 0) * ctf.astensor(1.)) else: omegactf = T / T return omegactf
def astensor(self, obj, dtype=None): if isinstance(obj, self.tensor): return obj.astype(dtype) elif isinstance(obj, ctf.tensor): return self.tensor(obj.astype(dtype)) else: return self.tensor(ctf.astensor(obj, dtype=dtype))
def function_tensor(I, J, K, sparsity): # N = 5 # n = 51 # L = 100 # nsample = 10*N*n*L #10nNL = 255000 T = ctf.tensor((I, J, K)) T2 = ctf.tensor((I, J, K)) T.fill_sp_random(1, 1, sparsity) # T = ctf.exp(-1 * ctf.power(ctf.power(T,2),0.5)) # x = exp(-sqrt(x^2)) sizes = [I, J, K] index = ["i", "j", "k"] for i in range(3): n = sizes[i] v = np.linspace(-1, 1, n) # v = np.arange(1,n+1) v = ctf.astensor(v**2) v2 = ctf.tensor(n) v2 = v T2.i("ijk") << T.i("ijk") * v2.i(index[i]) T2 = ctf.power(T2, 0.5) T2 = (-1.0) * T2 # T2 = ctf.exp(T2) return T2
def function_tensor(I, J, K, sparsity): # N = 5 # n = 51 # L = 100 # nsample = 10*N*n*L #10nNL = 255000 T = ctf.tensor((I, J, K), sp=True) T2 = ctf.tensor((I, J, K), sp=True) T.fill_sp_random(1, 1, sparsity) # T = ctf.exp(-1 * ctf.power(ctf.power(T,2),0.5)) # x = exp(-sqrt(x^2)) sizes = [I, J, K] index = ["i", "j", "k"] for i in range(3): n = sizes[i] v = np.linspace(-1, 1, n) # v = np.arange(1,n+1) v = ctf.astensor(v**2) v2 = ctf.tensor(n, sp=True) v2 = v T2.i("ijk") << T.i("ijk") * v2.i(index[i]) # T2 = ctf.power(T2, 0.5) [inds, data] = T2.read_local_nnz() data[:] **= .5 data[:] *= -1. T2 = ctf.tensor(T2.shape, sp=True) T2.write(inds, data) return T2
def test_take(self): a0 = numpy.arange(24.).reshape(4,3,2) a1 = ctf.astensor(a0) self.assertEqual(ctf.take(a0, numpy.array([0,3]), axis=0).shape, (2,3,2)) self.assertEqual(ctf.take(a1, [2], axis=1).shape, (4,1,2)) self.assertEqual(a1.take([0], axis=-1).shape, (4,3,1)) self.assertEqual(a1.transpose().take([1], axis=0).shape, (1,3,4))
def getOmega_old(T, I, J, K): if (T.sp == False): omegactf = ((T > 0) * ctf.astensor(1.)) else: omegactf = T / T return omegactf
def test_qr(self): m = 8 n = 4 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
def test_reshape(self): a = ctf.random.random((1,10,10,10)) self.assertTrue(ctf.all(a.reshape((10,100)) == a.to_nparray().reshape((10,100)))) base_shapes = [(2,3,4,5),(3,10,4),(1,3,10,4),(2,3,4,1,5)] for shape in base_shapes: a0 = numpy.arange(120).reshape(shape) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(ctf.reshape(a1,(2,3,4,5)) ==a0.reshape(2,3,4,5))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,10,4))==a0.reshape(1,3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,1,10,4))==a0.reshape(1,3,1,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(1,3,1,1,10,4))==a0.reshape(1,3,1,1,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4,1))==a0.reshape(3,10,4,1))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2))) with self.assertRaises(ValueError): a1.reshape((1,2))
def getDenseOmega_all(T, U, V, W, regParam, omega, I, J, K, r, string): if (string == "i"): omega_dense = [] for idx in range(I): omega_curr = ctf.to_nparray(omega[idx, :, :].reshape(J * K)) omega_sum = np.cumsum(omega_curr).tolist() omega_sum.insert(0, 0) del omega_sum[-1] #print("omega prefix sum: ", omega_sum) l = [] for x, y in enumerate(omega_sum): if omega_curr[x] != 0: l.append((x, int(y))) #print(l) num_nonzero = len(l) # form dense omega matrix #temp = np.zeros((J*K,len(l))) temp = np.zeros((J * K, J * K)) for x, y in l: temp[x][y] = 1 #print("omega_dense: ", omega_dense) temp = np.reshape(temp, (J, K, J * K)) omega_dense.append(temp) omega_dense = ctf.astensor(np.stack(omega_dense, axis=3)) #print("omega_dense shape: ", omega_dense.shape) return num_nonzero, omega_dense
def test__mul__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1*.5, a0*.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1*a2, a0*(a0*.2+1j))) self.assertTrue(allclose(a1*a0, a0*a0)) a2 = numpy.arange(6.).reshape(3,2) self.assertTrue(allclose(a1*a2, a0*a2)) a0 = ctf.astensor(numpy.arange(4.)) a1 = ctf.astensor(numpy.arange(3.)) self.assertTrue((a0.reshape(4,1)*a1).shape == (4,3)) self.assertTrue((a1*a0.reshape(4,1)).shape == (4,3)) self.assertTrue((a1.reshape(1,3)*a0.reshape(4,1)).shape == (4,3)) self.assertTrue((a1.reshape(1,1,3)*a0.reshape(4,1)).shape == (1,4,3)) self.assertTrue((a1.reshape(1,1,3)*a0.reshape(4,1,1)).shape == (4,1,3))
def test_qr(self): m = 8 n = 4 for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
def test_take(self): a0 = numpy.arange(24.).reshape(4, 3, 2) a1 = ctf.astensor(a0) self.assertEqual( ctf.take(a0, numpy.array([0, 3]), axis=0).shape, (2, 3, 2)) self.assertEqual(ctf.take(a1, [2], axis=1).shape, (4, 1, 2)) self.assertEqual(a1.take([0], axis=-1).shape, (4, 3, 1))
def test_transpose_reshape(self): a0 = numpy.arange(120).reshape(2, 3, 4, 5) a1 = ctf.astensor(a0) a0 = a0.transpose(3, 0, 2, 1) a1 = a1.transpose(3, 0, 2, 1) self.assertTrue(ctf.all(ctf.reshape(a1, (6, 20)) == a0.reshape(6, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, 5, 4)) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(ctf.reshape(a1, (6, -1)) == a0.reshape(6, -1))) self.assertTrue( ctf.all(ctf.reshape(a1, (-1, 20)) == a0.reshape(-1, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, -1, 4)) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, -1, 2)) == a0.reshape(3, -1, 2))) self.assertTrue(ctf.all(a1.reshape(6, 20) == a0.reshape(6, 20))) self.assertTrue(ctf.all(a1.reshape(6, 5, 4) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(a1.reshape((3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(a1.reshape((6, -1)) == a0.reshape(6, -1))) self.assertTrue(ctf.all(a1.reshape(-1, 20) == a0.reshape(-1, 20))) self.assertTrue(ctf.all(a1.reshape(6, -1, 4) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(a1.reshape((3, -1, 2)) == a0.reshape(3, -1, 2)))
def test_reshape(self): a0 = numpy.arange(120).reshape(2, 3, 4, 5) a1 = ctf.astensor(a0) self.assertTrue( ctf.all(ctf.reshape(a1, (2, 3, 4, 5)) == a0.reshape(2, 3, 4, 5))) self.assertTrue(ctf.all(ctf.reshape(a1, (6, 20)) == a0.reshape(6, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, 5, 4)) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(ctf.reshape(a1, (6, -1)) == a0.reshape(6, -1))) self.assertTrue( ctf.all(ctf.reshape(a1, (-1, 20)) == a0.reshape(-1, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, -1, 4)) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, -1, 2)) == a0.reshape(3, -1, 2))) self.assertTrue(ctf.all(a1.reshape(6, 20) == a0.reshape(6, 20))) self.assertTrue(ctf.all(a1.reshape(6, 5, 4) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(a1.reshape((3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(a1.reshape((6, -1)) == a0.reshape(6, -1))) self.assertTrue(ctf.all(a1.reshape(-1, 20) == a0.reshape(-1, 20))) self.assertTrue(ctf.all(a1.reshape(6, -1, 4) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(a1.reshape((3, -1, 2)) == a0.reshape(3, -1, 2))) with self.assertRaises(ValueError): a1.reshape((1, 2))
def forward(self, A, X, train=True): if train == True: layer_output = self.layer_output layer_output_nonlinear = self.layer_output_nonlinear else: temp_layer_output = {} temp_layer_output_nonlinear = {} layer_output = temp_layer_output layer_output_nonlinear = temp_layer_output_nonlinear if self.package != "ctf": layer_output["layer" + str(0)] = X for i in range(self.num_layers): layer_output["layer" + str(i + 1)] = A @ layer_output[ "layer" + str(i)] @ self.params["W" + str(i + 1)] if i != self.num_layers - 1: layer_output_nonlinear["layer" + str(i + 1)] = self.nonlinearity( layer_output["layer" + str(i + 1)]) else: layer_output_nonlinear["layer" + str(i + 1)] = softmax( layer_output["layer" + str(i + 1)]) return layer_output_nonlinear["layer" + str(self.num_layers)] else: # when using the package ctf layer_output["layer" + str(0)] = ctf.astensor(X) for i in range(self.num_layers): layer_output["layer" + str(i + 1)] = A @ layer_output[ "layer" + str(i)] @ self.params["W" + str(i + 1)] if i != self.num_layers - 1: layer_output_nonlinear["layer" + str(i + 1)] = self.nonlinearity( layer_output["layer" + str(i + 1)]) else: temp_array = ctf.to_nparray(layer_output["layer" + str(i + 1)]) layer_output_nonlinear["layer" + str(i + 1)] = softmax(temp_array) layer_output_nonlinear["layer" + str(i + 1)] = ctf.astensor( layer_output_nonlinear["layer" + str(i + 1)]) # print(layer_output_nonlinear["layer" + str(self.num_layers)].sp) return layer_output_nonlinear["layer" + str(self.num_layers)]
def test_diag(self): a0 = ctf.astensor(numpy.arange(9).reshape(3, 3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.diag(a0))) self.assertTrue( ctf.all( ctf.diag(a1) == numpy.diag( numpy.arange(9).reshape(3, 3).diagonal())))
def updateOmega(I,J,K,sparsity): ''' Gets a random subset of rows for each U,V,W iteration ''' Actf = ctf.tensor((I,J,K),sp=True) Actf.fill_sp_random(0,1,sparsity) omegactf = ((Actf > 0)*ctf.astensor(1.)) return omegactf
def test_einsum_views(self): a0 = numpy.arange(27.).reshape(3,3,3) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum("jii->ij", a1), numpy.einsum("jii->ij", a0))) self.assertTrue(allclose(ctf.einsum("iii->i", a1), numpy.einsum("iii->i", a0))) self.assertTrue(allclose(ctf.einsum("iii", a1), numpy.einsum("iii", a0))) a0 = numpy.arange(6.) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum("i,i,i->i", a1, a1, a1), numpy.einsum("i,i,i->i", a0, a0, a0))) # swap axes a0 = numpy.arange(24.).reshape(4,3,2) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum("ijk->kji", a1), numpy.einsum("ijk->kji", a0)))
def createOmega(I,J,K,sparsity): print(I,J,K) #Actf = ctf.tensor((I,J,K),sp=True) #Actf.fill_sp_random(0,1,sparsity) Actf = ctf.tensor((I,J,K)) Actf.fill_random(0,1) omegactf = ((Actf > 0)*ctf.astensor(1.)) return omegactf
def test_einsum_misc(self): # The iterator had an issue with buffering this reduction a0 = numpy.ones((5, 12, 4, 2, 3)) b0 = numpy.ones((5, 12, 11)) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum('ijklm,ijn->', a1, b1), numpy.einsum('ijklm,ijn->', a0, b0))) self.assertTrue(allclose(ctf.einsum('ijklm,ijn,ijn->', a1, b1, b1), #numpy.einsum('ijklm,ijn,ijn->', a0, b0, b0))) numpy.einsum('ijklm,ijn->', a0, b0))) # inner loop implementation a0 = numpy.arange(1., 3.) b0 = numpy.arange(1., 5.).reshape(2, 2) c0 = numpy.arange(1., 9.).reshape(4, 2) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c1 = ctf.astensor(c0) self.assertTrue(allclose(ctf.einsum('x,yx,zx->xzy', a1, b1, c1), numpy.einsum('x,yx,zx->xzy', a0, b0, c0))) a0 = numpy.random.normal(0, 1, (5, 5, 5, 5)) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum('aabb->ab', a1), numpy.einsum('aabb->ab', a0))) a0 = numpy.arange(25.).reshape(5, 5) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum('mi,mi,mi->m', a1, a1, a1), numpy.einsum('mi,mi,mi->m', a0, a0, a0)))
def test_ravel(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.ravel()==a0.ravel())) #self.assertTrue(ctf.all(a1.transpose(0,1,3,2).ravel()==a0.transpose(0,1,3,2).ravel())) self.assertTrue(ctf.all(a1.transpose(0,1,3,2).ravel()==a0.transpose(0,1,3,2).ravel())) self.assertTrue(ctf.all(a1.transpose(0,2,1,3).ravel()==a0.transpose(0,2,1,3).ravel())) self.assertTrue(ctf.all(a1.transpose(3,2,0,1).ravel()==a0.transpose(3,2,0,1).ravel())) self.assertTrue(ctf.all(a1.transpose(2,1,0,3).ravel()==a0.transpose(2,1,0,3).ravel()))
def test__getitem__(self): a0 = numpy.arange(12.).reshape(4,3) a1 = ctf.astensor(a0) self.assertTrue(allclose(a1[3], a0[3])) self.assertEqual(a1[(3,1)], a1[3,1]) self.assertTrue(a1[1:3:2].shape == (1,3)) self.assertTrue(a1[:,1:].shape == (4,2)) self.assertTrue(a1[:,:1].shape == (4,1)) self.assertTrue(allclose(a1[[3,1]], a0[[3,1]])) self.assertTrue(allclose(a1[:,[2,1]], a0[:,[2,1]])) self.assertTrue(allclose(a1[1:3,2:3], a0[1:3,2:3])) self.assertTrue(allclose(a1[1:3,2:5], a0[1:3,2:3])) self.assertTrue(allclose(a1[1:-2], a0[1:-2])) with self.assertRaises(IndexError): a1[[3,4]] a0 = numpy.arange(60.).reshape(5,4,3) a1 = ctf.astensor(a0) self.assertTrue(allclose(a1[1:3,:,2:], a0[1:3,:,2:]))
def test__iadd__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 += .5 self.assertTrue(allclose(a1, a0+.5)) a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() with self.assertRaises(TypeError): a1 += a0*.2+1j a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a2 = numpy.arange(6.).reshape(3,2) a1 += a2 self.assertTrue(allclose(a1, a0+a2)) a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a2 = numpy.arange(2.) a1 += a2 self.assertTrue(allclose(a1, a0+a2))
def test_dot_2d(self): a1 = numpy.random.random(4) a2 = numpy.random.random((4,3)) self.assertTrue(ctf.dot(ctf.astensor(a1), ctf.astensor(a2)).shape == (3,)) self.assertTrue(allclose(ctf.dot(a1, ctf.astensor(a2)), numpy.dot(a1, a2))) self.assertTrue(ctf.dot(ctf.astensor(a2).T(), a1).shape == (3,)) self.assertTrue(allclose(ctf.dot(ctf.astensor(a2).T(), a1), numpy.dot(a2.T, a1))) with self.assertRaises(ValueError): ctf.dot(a2, a2) self.assertTrue(allclose(ctf.dot(ctf.astensor(a2).T(), a2), numpy.dot(a2.T, a2))) self.assertTrue(allclose(ctf.astensor(a2).dot(a2.T), a2.dot(a2.T)))
def test_vstack(self): a1 = ctf.astensor(numpy.ones(4)) a2 = ctf.astensor(numpy.ones(4)) self.assertTrue(ctf.vstack((a1, a2)).shape == (2,4)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((3,4))) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4)) a1 = ctf.astensor(numpy.ones((2,4))) a2 = ctf.astensor(numpy.ones((3,4))+0j) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4)) self.assertTrue(ctf.vstack((a1, a2)).dtype == numpy.complex128) a1 = ctf.astensor(numpy.ones((4,1))) self.assertTrue(ctf.vstack((a1, 1.5)).shape == (5,1)) a1 = ctf.astensor(numpy.ones((2,4,2))) a2 = ctf.astensor(numpy.ones((3,4,2))) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4,2))
def test_astensor(self): # astensor converts python object to ctf tensor a0 = ctf.astensor((1,2,3)) a0 = ctf.astensor([1,2.,3]) a0 = ctf.astensor([(1,2), (3,4)]) a0 = ctf.astensor(numpy.arange(3)) a0 = ctf.astensor([numpy.array((1,2)), numpy.array((3,4))+1j]) a1 = ctf.astensor(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.asarray(a1) # self.assertTrue(ctf.asarray(a0).__class__ == ctf.astensor(a0).__class__) a0 = ctf.astensor([1,2.,3], dtype='D') self.assertTrue(a0.dtype == numpy.complex128) with self.assertRaises(TypeError): ctf.astensor([1j,2j], dtype='d') a0 = numpy.arange(4.).reshape(2,2) a1 = ctf.to_nparray(ctf.from_nparray(a0)) self.assertTrue(ctf.all(a0==a1)) try: a1 = ctf.from_nparray(a1).to_nparray() self.assertTrue(ctf.all(a0==a1)) except AttributeError: pass a0 = ctf.from_nparray(numpy.arange(3)) a1 = ctf.from_nparray(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) a0 = numpy.arange(6).reshape(2,3) a1 = ctf.array(a0) self.assertTrue(ctf.all(a0==a1)) self.assertTrue(ctf.all(a1==a0)) a1 = ctf.array(a0, copy=False) self.assertTrue(ctf.all(a1==0))
def test_reshape(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2))) with self.assertRaises(ValueError): a1.reshape((1,2))
def test_transpose_reshape(self): a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) a0 = a0.transpose(3,0,2,1) a1 = a1.transpose(3,0,2,1) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,5,4)) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(ctf.reshape(a1,(-1,20)) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,-1,4))==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(ctf.reshape(a1,(3,-1,2))==a0.reshape(3,-1,2))) self.assertTrue(ctf.all(a1.reshape(6,20) ==a0.reshape(6,20))) self.assertTrue(ctf.all(a1.reshape(6,5,4) ==a0.reshape(6,5,4))) self.assertTrue(ctf.all(a1.reshape((3,10,4))==a0.reshape(3,10,4))) self.assertTrue(ctf.all(a1.reshape((6,-1)) ==a0.reshape(6,-1))) self.assertTrue(ctf.all(a1.reshape(-1,20) ==a0.reshape(-1,20))) self.assertTrue(ctf.all(a1.reshape(6,-1,4) ==a0.reshape(6,-1,4))) self.assertTrue(ctf.all(a1.reshape((3,-1,2))==a0.reshape(3,-1,2)))
def test_svd(self): m = 9 n = 5 k = 5 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [U,S,VT]=ctf.svd(A,k) [U1,S1,VT1]=la.svd(ctf.to_nparray(A),full_matrices=False) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T()))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(VT, ctf.conj(VT.T())))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(VT, ctf.conj(VT.T()))))
def test_transpose_astensor(self): a0 = numpy.arange(6).reshape(2,3) a1 = ctf.astensor(a0.T) #self.assertTrue(ctf.all(a1==a0)) a1 = ctf.astensor(a1.T()) self.assertTrue(ctf.all(a1==a0)) a0 = numpy.arange(120).reshape(2,3,4,5) a1 = a0.transpose(0,1,3,2) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(0,2,1,3) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(3,2,0,1) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(2,1,0,3) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a0 = numpy.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.transpose(0,1,3,2)==a0.transpose(0,1,3,2))) self.assertTrue(ctf.all(a1.transpose(0,2,1,3)==a0.transpose(0,2,1,3))) self.assertTrue(ctf.all(a1.transpose(3,2,0,1)==a0.transpose(3,2,0,1))) self.assertTrue(ctf.all(a1.transpose(2,1,0,3)==a0.transpose(2,1,0,3)))
def test_set_item(self): a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[:] = b0 a1[:] = b1 self.assertTrue(allclose(a1, a0)) a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[1:,1] = b0 a1[1:,1] = b1 self.assertTrue(allclose(a1, a0)) a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[2:,:,1] = b0[:,1] a1[2:,:,1] = b1[:,1] self.assertTrue(allclose(a1, a0))
def test_get_item(self): a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0 = a0[:] b1 = a1[:] self.assertTrue(allclose(b1, b0)) a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0 = a0[1:,1] b1 = a1[1:,1] self.assertTrue(allclose(b1, b0)) a0 = numpy.arange(24.).reshape(4,3,2) + 400. b0 = numpy.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0[:,1] = a0[2,:,1] b1[:,1] = a1[2,:,1] self.assertTrue(allclose(b1, b0))
def test__getitem__(self): a0 = numpy.arange(12.).reshape(4,3) a1 = ctf.astensor(a0) self.assertTrue(a1.shape == (4,3)) self.assertTrue(a1[1].shape == (3,))
def test_dot_1d(self): a1 = numpy.ones(4) self.assertTrue(allclose(ctf.dot(ctf.astensor(a1), a1), numpy.dot(a1, a1))) self.assertTrue(allclose(ctf.dot(a1+1j, ctf.astensor(a1)), numpy.dot(a1+1j, a1))) a2 = ctf.astensor(a1).dot(a1+0j) self.assertTrue(a2.dtype == numpy.complex128)
def test__ipow__(self): a0 = numpy.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 **= .5 self.assertTrue(allclose(a1, a0**.5))
def test_einsum_sums(self): # outer(a,b) for n in range(1, 17): a0 = numpy.arange(3, dtype=numpy.double)+1 b0 = numpy.arange(n, dtype=numpy.double)+1 a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum("i,j", a1, b1), numpy.outer(a0, b0))) # matvec(a,b) / a.dot(b) where a is matrix, b is vector for n in range(1, 17): a0 = numpy.arange(4*n, dtype=numpy.double).reshape(n, 4) b0 = numpy.arange(n, dtype=numpy.double) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum("ji,j", a1, b1), numpy.dot(b0.T, a0))) self.assertTrue(allclose(ctf.einsum("ji,j->", a1, b1), numpy.dot(b0.T, a0).sum())) # matmat(a,b) / a.dot(b) where a is matrix, b is matrix for n in range(1, 17): a0 = numpy.arange(4*n, dtype=numpy.double).reshape(n, 4) b0 = numpy.arange(6*n, dtype=numpy.double).reshape(n, 6) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum("ji,jk", a1, b1), numpy.dot(a0.T, b0))) self.assertTrue(allclose(ctf.einsum("ji,jk->", a1, b1), numpy.dot(a0.T, b0).sum())) # matrix triple product (note this is not currently an efficient # way to multiply 3 matrices) a0 = numpy.arange(12.).reshape(3, 4) b0 = numpy.arange(20.).reshape(4, 5) c0 = numpy.arange(30.).reshape(5, 6) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c1 = ctf.astensor(c0) self.assertTrue(allclose(ctf.einsum("ij,jk,kl", a1, b1, c1), numpy.einsum("ij,jk,kl", a0, b0, c0))) # tensordot(a, b) a0 = numpy.arange(27.).reshape(3, 3, 3) b0 = numpy.arange(27.).reshape(3, 3, 3) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum("ijk, jli -> kl", a1, b1), numpy.einsum("ijk, jli -> kl", a0, b0))) self.assertTrue(allclose(ctf.einsum("ijk, jli -> lk", a1, b1), numpy.einsum("ijk, jli -> lk", a0, b0))) self.assertTrue(allclose(ctf.einsum("ikj, jli -> kl", a1, b1), numpy.einsum("ikj, jli -> kl", a0, b0))) self.assertTrue(allclose(ctf.einsum("kij, lij -> lk", a1, b1), numpy.einsum("kij, lij -> lk", a0, b0)))
def test_diag(self): a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.diag(a0))) self.assertTrue(ctf.all(ctf.diag(a1) == numpy.diag(numpy.arange(9).reshape(3,3).diagonal())))
def test_trace(self): a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) a1 = a0.trace() self.assertEqual(a1, 12) self.assertEqual(ctf.trace(numpy.arange(9).reshape(3,3)), 12)