def LS_SVD(Z,factor,r,Tbar,regParam,idx): [U_,S_,VT_] = ctf.svd(Z) S_ = S_/(S_*S_ + regParam*ctf.ones(S_.shape)) factor.set_zero() factor.i("r") << VT_.i("kr")*S_.i("k")*U_.i("tk")*Tbar.i("t") return factor
def Kressner(A, b, factor, r, regParam): [U_, S_, VT_] = ctf.svd(A) S_ = 1 / (S_ + regParam * ctf.ones(S_.shape)) factor.set_zero() factor.i("r") << VT_.i("kr") * S_.i("k") * U_.i("tk") * b.i("t") return factor
def bench(n, b, niter): A = ctf.ones((n, b, n, b)) B = ctf.ones((n, b, n, b)) ctf.random.seed(42) A = ctf.random.random((n, b, n, b)) B = ctf.random.random((n, b, n, b)) A = (A + A.transpose([2, 1, 0, 3])).reshape((n * b, n * b)) B = (B + B.transpose([2, 1, 0, 3])).reshape((n * b, n * b)) Ac = A.copy() Bc = B.copy() times_naive = [] times_fast = [] print("Benchmarking naive for", niter, "iterations") for i in range(niter): t0 = time.time() C_naive = naive_gemm(A, B, n, b) t1 = time.time() ite1 = t1 - t0 print(ite1) times_naive.append(ite1) print("Benchmarking fast for", niter, "iterations") for i in range(niter): t0 = time.time() C_fast = fast_gemm(Ac, Bc, n, b) t1 = time.time() ite1 = t1 - t0 print(ite1) times_fast.append(ite1) avg_naive = np.mean(times_naive) avg_fast = np.mean(times_fast) print("Average time for naive is", avg_naive) print("Average time for fast is", avg_fast) stddev_naive = np.std(times_naive) stddev_fast = np.std(times_fast) print("95% confidence interval for naive is [", avg_naive - 2 * stddev_naive, ",", avg_naive + 2 * stddev_naive, "]") print("95% confidence interval for fast is [", avg_fast - 2 * stddev_fast, ",", avg_fast + 2 * stddev_fast, "]")
def test_int_conv(self): a = ctf.ones(2, dtype=float) b = numpy.ones(2, dtype=float) self.assertTrue(numpy.allclose(a.to_nparray(),b)) a = ctf.zeros(2, dtype=complex) a[0] = 1 self.assertTrue(numpy.allclose([a.norm2()],[1.])) a *= 2 self.assertTrue(numpy.allclose([a.norm2()],[2.]))
def Kressner(A, b, factor, r, regParam): # t = time.time() [U_, S_, VT_] = ctf.svd(A) S_ = 1 / (S_ + regParam * ctf.ones(S_.shape)) factor.set_zero() factor.i("r") << VT_.i("kr") * S_.i("k") * U_.i("tk") * b.i("t") #assert(factor.sp==1) TODO!! # if ctf.comm().rank() == 0: # print("SVD cost %f seconds" %(np.round_(time.time()- t,4))) return factor
def updateW(T,U,V,W,regParam,omega,I,J,K,r): '''Update V matrix by using the formula''' M3 = ctf.tensor((I,J,r)) M3.i("iju") << U.i("iu")*V.i("ju") [U_,S_,V_] = ctf.svd(M3.reshape((I*J,r))) #S_ = 1./S_ S_ = S_/(S_*S_ + regParam*ctf.ones(r)) W.set_zero() W.i("ku") << V_.i("vu")*S_.i("v")*U_.reshape((I,J,r)).i("ijv")*T.i("ijk") W *= normalize(W,r) return W
def updateU(T,U,V,W,regParam,omega,I,J,K,r): '''Update U matrix by using the formula''' M1 = ctf.tensor((J,K,r)) M1.i("jku") << V.i("ju")*W.i("ku") [U_,S_,V_] = ctf.svd(M1.reshape((J*K,r))) #S_ = 1./S_ S_ = S_/(S_*S_ + regParam*ctf.ones(r)) U.set_zero() U.i("iu") << V_.i("vu")*S_.i("v")*U_.reshape((J,K,r)).i("jkv")*T.i("ijk") U *= normalize(U,r) return U
def updateV(T,U,V,W,regParam,omega,I,J,K,r): '''Update V matrix by using the formula''' M2 = ctf.tensor((I,K,r)) M2.i("iku") << U.i("iu")*W.i("ku") [U_,S_,V_] = ctf.svd(M2.reshape((I*K,r))) #S_ = 1./S_ S_ = S_/(S_*S_ + regParam*ctf.ones(r)) V.set_zero() V.i("ju") << V_.i("vu")*S_.i("v")*U_.reshape((I,K,r)).i("ikv")*T.i("ijk") #normalize(V,r) V *= normalize(V,r) return V
def test(n, b): A = ctf.ones((n, b, n, b)) B = ctf.ones((n, b, n, b)) ctf.random.seed(42) A = ctf.random.random((n, b, n, b)) B = ctf.random.random((n, b, n, b)) A = (A + A.transpose([2, 1, 0, 3])).reshape((n * b, n * b)) #A = ctf.ones((n*b,n*b)) B = (B + B.transpose([2, 1, 0, 3])).reshape((n * b, n * b)) #B = ctf.ones((n*b,n*b)) Ac = A.copy() Bc = B.copy() C_naive = naive_gemm(A, B, n, b) C_fast = fast_gemm(Ac, Bc, n, b) #print("Naive method yields") #print(C_naive) #print("Fast method yields") #print(C_fast) #print("Ratio is") #print(C_fast/C_naive) err = ctf.norm(C_naive - C_fast) / ctf.norm(C_naive) print("n=", n, "b=", b) print("Relative two norm error is", err)
def ones(self, shape, dtype=float): return self.tensor(ctf.ones(shape, dtype=dtype))
def ones(shape): return ctf.ones(shape)
def test_mixtype_comp(self): a = numpy.ones(2, dtype=int) < 1.0 b = ctf.ones(2, dtype=int) < 1.0 self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.zeros(2, dtype=numpy.float32) < numpy.ones(2, dtype=numpy.float64) b = ctf.zeros(2, dtype=numpy.float32) < ctf.ones(2, dtype=numpy.float64) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) <= 1.0 b = ctf.ones(2, dtype=int) <= 1.0 self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.zeros(2, dtype=numpy.float32) <= numpy.ones(2, dtype=numpy.float64) b = ctf.zeros(2, dtype=numpy.float32) <= ctf.ones(2, dtype=numpy.float64) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) > 1.0 b = ctf.ones(2, dtype=int) > 1.0 self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.zeros(2, dtype=numpy.float32) > numpy.ones(2, dtype=numpy.float64) b = ctf.zeros(2, dtype=numpy.float32) > ctf.ones(2, dtype=numpy.float64) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) >= 1.0 b = ctf.ones(2, dtype=int) >= 1.0 self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.zeros(2, dtype=numpy.float32) >= numpy.ones(2, dtype=numpy.float64) b = ctf.zeros(2, dtype=numpy.float32) >= ctf.ones(2, dtype=numpy.float64) self.assertTrue(numpy.all(a==b.to_nparray())) a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) self.assertTrue(numpy.all(a==b.to_nparray()))
def test_complex(self): for dt in [numpy.complex64, numpy.complex128, complex]: a = abs(numpy.ones(2, dtype=dt)) b = abs(ctf.ones(2, dtype=dt)) self.assertTrue(a.dtype==b.dtype) self.assertTrue(numpy.allclose(a,b.to_nparray()))
def ones_like(tensor): return ctf.ones(CTFBackend.shape(tensor))