diff --git a/examples/svd_tests.py b/examples/svd_tests.py index 6fca06dd2cab2a7b0b7533ecd8d440f7bd95dc6c..bcc28f24b03e6dcaa40a520ef3bd8155f664bdba 100644 --- a/examples/svd_tests.py +++ b/examples/svd_tests.py @@ -10,19 +10,33 @@ from timeit import default_timer as timer import numpy as np def test_svd(): - d = 100 - A = np.asarray(np.random.randint(1, 10,(d, d)), dtype=np.float64) + d = 1000 + A = np.asarray(np.random.randint(1, 10,(d, d)), dtype=np.float32) # scipy print("\n****************** Scipy") - from scipy import linalg as sc_linalg + from scipy.linalg import svd as sc_svd start = timer() - u_sc, s_sc, v_sc = sc_linalg.svd(A) + u_sc, s_sc, v_sc = sc_svd(A) duration = timer() - start print("SVD scipy : " + str(duration)) print(s_sc) print(type(s_sc)) + # cupy + print("\n****************** Cupy") + import cupy as cp + from cupy.linalg import svd as cp_svd + start = timer() + A_cp = cp.asarray(A) + u_cp, s_cp, v_cp = cp_svd(A_cp) + u_cp, s_cp, v_cp = u_cp.get(), s_cp.get(), v_cp.get() + duration = timer() - start + print("SVD cupy : " + str(duration)) + print(s_cp) + print(A_cp.device) + print(type(s_cp)) + # scikit-cuda print("\n****************** scikit-cuda") import pycuda.autoinit @@ -39,35 +53,49 @@ def test_svd(): print("SVD skcuda : " + str(duration)) print(s_sk) print(type(s_sk)) - - # tensorflow - print("\n****************** tensorflow") - import tensorflow as tf - from tensorflow import linalg as tf_linalg - start = timer() - with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: - s_tf, u_tf, v_tf = tf_linalg.svd(A) - s_tf, u_tf, v_tf = s_tf.eval(), u_tf.eval(), v_tf.eval() - duration = timer() - start - print("SVD tensorflow : " + str(duration)) - print(s_tf) - print(type(s_tf)) - + +# # tensorflow +# print("\n****************** tensorflow") +# import tensorflow as tf +# start = timer() +# with tf.device("/cpu:0"): +# s_tf, u_tf, v_tf = tf.svd(A, compute_uv=True) +# s_tf, u_tf, v_tf = tf.Session().run(s_tf), tf.Session().run(u_tf), tf.Session().run(v_tf) +# duration = timer() - start +# print("SVD tensorflow CPU : " + str(duration)) +# print(s_tf) +# print(type(s_tf)) +# print() +# start = timer() +# with tf.device("/gpu:0"): +# s_tf, u_tf, v_tf = tf.svd(A, compute_uv=True) +# s_tf, u_tf, v_tf = tf.Session().run(s_tf), tf.Session().run(u_tf), tf.Session().run(v_tf) +# duration = timer() - start +# print("SVD tensorflow GPU : " + str(duration)) +# print(s_tf) +# print(type(s_tf)) + # Theano print("\n****************** theano") - import theano.tensor as T - from theano import function + from theano import function, shared, tensor, config import theano.tensor.nlinalg as th_linalg - x = T.dmatrix('x') + x = tensor.dmatrix('x') u, s, v = th_linalg.svd(x) f = function([x], (u, s, v)) start = timer() u_th, s_th, v_th = f(A) duration = timer() - start + +# x = shared(np.asarray(A, config.floatX)) +# f = function([], th_linalg.svd(x)) +# start = timer() +# u_th, s_th, v_th = f() +# duration = timer() - start + print("SVD theano : " + str(duration)) print(s_th) print(type(s_th)) - - + + if __name__ == '__main__': test_svd() diff --git a/splearn/hankel.py b/splearn/hankel.py index 7318d05dc0fac23c531f7a04a9aab9f4af83e70b..24dc0cf6460e651415b87b0c69a1bbaed34e885f 100644 --- a/splearn/hankel.py +++ b/splearn/hankel.py @@ -363,8 +363,17 @@ class Hankel(object): "the smaller dimension of the Hankel Matrix (" + str(matrix_shape) + ")") if not self.sparse: + try: + import theano.tensor as T + from theano import function + import theano.tensor.nlinalg as th_linalg + x = T.dmatrix('x') + u, s, v = th_linalg.svd(x) + svd = function([x], (u, s, v)) + except : + from scipy.linalg import svd hankel = self.lhankel[0] - [u, s, v] = np.linalg.svd(hankel) + [u, s, v] = svd(hankel) u = u[:, :rank] v = v[:rank, :] # ds = np.zeros((rank, rank), dtype=complex)