我正在尝试使用笔记本电脑的gpu使用tensorflow对模拟数据集(代码部分#1 )执行线性回归(代码部分#2 )。我的问题在于,根据成本函数块中y_pred
的定义方式,我在gpu上运行该块时得到InternalError
。然而在cpu上运行它很好。回溯显示在此问题的末尾( Traceback )。
所以问题是:这个内部错误的原因是什么?我该如何解决这个问题,以便我可以使用tf.matmul?
规格:笔记本电脑运行的是Windows 10 64bit,并配有GeForce 940MX卡和Intel HD图形630.在软件方面,笔记本电脑正在运行cuda 9.0和相应的cudnn。
代码#1 :
import matplotlib.pylab as plt
import tensorflow as tf
import numpy as np
# generate some linear train and test data
X_train = np.linspace(0,1,100).reshape((-1,1))
t_train = X_train.dot([1])
t_train = t_train.reshape((-1,1))
X_test = np.linspace(0,1,150).reshape((-1,1))
t_test = X_test.dot([1])
t_tets = t_test.reshape((-1,1))
N, D = X_train.shape
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X_train, t_train, '.', label="train")
ax.plot(X_test, t_test, '.', label="test")
plt.legend()
plt.show()
代码#2 :
tf.reset_default_graph()
cpu = "/cpu:0"
gpu = "/gpu:0"
# placeholders and weights variable
with tf.device(cpu):
X = tf.placeholder(tf.float32, shape=(None,D), name="X")
y = tf.placeholder(tf.float32, shape=(None,1), name="y")
w = tf.Variable(tf.ones([D,1])*10, name="w")
learning_rate = .1
# cost function
with tf.device(gpu):
y_pred = tf.matmul(X,w) # does not work
#y_pred = X*w # works
error = y_pred - y
cost = tf.reduce_mean(tf.square(error))
# update rule
with tf.device(cpu):
gradient = tf.gradients(cost, [w])[0]
update = tf.assign(w, w - learning_rate * gradient)
init = tf.global_variables_initializer()
n_epochs = 500
n_print = 50
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
sess.run(update, feed_dict={X:X_train, y:t_train})
if epoch % n_print == 0:
print("epoch",epoch,
": cost",cost.eval(feed_dict={X:X_train, y:t_train}),
"w",w.eval())
代码#2中由tf.matmul引起的回溯:
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1360 try:
-> 1361 return fn(*args)
1362 except errors.OpError as e:
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1339 return tf_session.TF_Run(session, options, feed_dict, fetch_list,
-> 1340 target_list, status, run_metadata)
1341
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
515 compat.as_text(c_api.TF_Message(self.status.status)),
--> 516 c_api.TF_GetCode(self.status.status))
517 # Delete the underlying status object from memory otherwise it stays alive
InternalError: Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
InternalError Traceback (most recent call last)
<ipython-input-4-0375a84ed72a> in <module>()
11 for epoch in range(n_epochs):
12
---> 13 sess.run(update, feed_dict={X:X_train, y:t_train})
14
15 if epoch % n_print == 0:
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
903 try:
904 result = self._run(None, fetches, feed_dict, options_ptr,
--> 905 run_metadata_ptr)
906 if run_metadata:
907 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 if final_fetches or final_targets or (handle and feed_dict_tensor):
1136 results = self._do_run(handle, final_targets, final_fetches,
-> 1137 feed_dict_tensor, options, run_metadata)
1138 else:
1139 results = []
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1353 if handle is None:
1354 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1355 options, run_metadata)
1356 else:
1357 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1372 except KeyError:
1373 pass
-> 1374 raise type(e)(node_def, op, message)
1375
1376 def _extend_graph(self):
InternalError: Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'MatMul', defined at:
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
app.launch_new_instance()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-9dfe23bd6608>", line 16, in <module>
y_pred = tf.matmul(X,w) # does not work
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\ops\math_ops.py", line 2064, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 2790, in _mat_mul
name=name)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 3271, in create_op
op_def=op_def)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InternalError (see above for traceback): Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]