In [2]:
import numpy as np
import tensorflow as tf
tf.reset_default_graph()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.01)
In [3]:
N = 81
M_local = np.random.randn(N,N).astype(np.float32)
x_local = np.random.randn(N).astype(np.float32)
In [4]:
print(M_local.nbytes/1024**3,"GB")
In [5]:
with tf.device("/gpu:0"):
M = tf.Variable(M_local, name="M")
x = tf.Variable(x_local, name="x")
In [6]:
M = tf.Variable(tf.random_normal([N, N], stddev=0.5,dtype=tf.float64), name="M")
x = tf.Variable(tf.random_normal([N],stddev=0.1,dtype=tf.float64), name="x")
In [7]:
init_op = tf.global_variables_initializer()
In [8]:
M.get_shape(),x.get_shape(),M.dtype
Out[8]:
In [9]:
with tf.device("/gpu:0"):
#c = M*x
c = tf.matmul(M,tf.reshape(x, [ N,-1]))
In [10]:
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,log_device_placement=True)) as sess:
sess.run(init_op)
M_local = sess.run(M)
x_local = sess.run(x)
c_local = sess.run(c)
In [11]:
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,log_device_placement=True)) as sess:
sess.run(init_op)
%time c_local = sess.run(c)
In [12]:
c_local.shape,np.dot(M_local,x_local).shape
Out[12]:
In [ ]:
In [13]:
np.max(np.abs(np.dot(M_local,x_local) - c_local.flatten()))
Out[13]:
In [14]:
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
sess.run(init_op)
sol = sess.run(tf.matrix_solve(M,tf.reshape(x, [ N,-1])))
M_local = sess.run(M)
x_local = sess.run(x)
In [15]:
np.ravel(sol)[:4]
Out[15]:
In [16]:
np.linalg.solve(M_local,x_local)[:4]
Out[16]:
In [ ]: