自从装好tensorflow-gpu 之后其实一直没怎么用,今天跑代码的时候才发现安装的有问题:
测试代码如下:
from sklearn.datasets import load_sample_image
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
if __name__ == '__main__':
# Load sample images
china = load_sample_image("china.jpg")
flower = load_sample_image("flower.jpg")
dataset = np.array([china, flower], dtype=np.float32)
batch_size, height, width, channels = dataset.shape
# Create 2 filters
filters = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)
filters[:, 3, :, 0] = 1 # vertical line
filters[3, :, :, 1] = 1 # horizontal line
# Create a graph with input X plus a convolutional layer applying the 2 filters
X = tf.placeholder(tf.float32, shape=(None, height, width, channels))
convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME")
with tf.Session() as sess:
output = sess.run(convolution, feed_dict={X: dataset})
plt.imshow(output[0, :, :, 1], cmap="gray") # plot 1st image's 2nd feature map
plt.show()
虽然代码比较简单,但是运行却出现了问题,报了一大堆错误:
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408
UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node Conv2D_1}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_Placeholder_1_0_0/_1, Conv2D_1/filter)]]
[[{{node Conv2D_1/_3}} = _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_7_Conv2D_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
UnknownError Traceback (most recent call last)
in
18 convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME")
19 with tf.Session() as sess:
---> 20 output = sess.run(convolution, feed_dict={X: dataset})
21 plt.imshow(output[0, :, :, 1], cmap="gray") # plot 1st image's 2nd feature map
22 plt.show()
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1150 if final_fetches or final_targets or (handle and feed_dict_tensor):
1151 results = self._do_run(handle, final_targets, final_fetches,
-> 1152 feed_dict_tensor, options, run_metadata)
1153 else:
1154 results = []
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1326 if handle is None:
1327 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328 run_metadata)
1329 else:
1330 return self._do_call(_prun_fn, handle, feeds, fetches)
d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1346 pass
1347 message = error_interpolation.interpolate(message, self._graph)
-> 1348 raise type(e)(node_def, op, message)
1349
1350 def _extend_graph(self):
UnknownError: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D_1 (defined at :18) = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_Placeholder_1_0_0/_1, Conv2D_1/filter)]]
[[{{node Conv2D_1/_3}} = _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_7_Conv2D_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'Conv2D_1', defined at:
File "E:\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "E:\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel_launcher.py", line 16, in
app.launch_new_instance()
File "d:\machinelinerningproject\venv36\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\kernelapp.py", line 505, in start
self.io_loop.start()
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\platform\asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "E:\Python36\lib\asyncio\base_events.py", line 438, in run_forever
self._run_once()
File "E:\Python36\lib\asyncio\base_events.py", line 1451, in _run_once
handle._run()
File "E:\Python36\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\ioloop.py", line 758, in _run_callback
ret = callback()
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\gen.py", line 1233, in inner
self.run()
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\gen.py", line 1147, in run
yielded = self.gen.send(value)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\kernelbase.py", line 357, in process_one
yield gen.maybe_future(dispatch(*args))
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\kernelbase.py", line 267, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\kernelbase.py", line 534, in execute_request
user_expressions, allow_stdin,
File "d:\machinelinerningproject\venv36\lib\site-packages\tornado\gen.py", line 326, in wrapper
yielded = next(result)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\ipkernel.py", line 294, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "d:\machinelinerningproject\venv36\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\interactiveshell.py", line 2819, in run_cell
raw_cell, store_history, silent, shell_futures)
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\interactiveshell.py", line 2845, in _run_cell
return runner(coro)
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\async_helpers.py", line 67, in _pseudo_sync_runner
coro.send(None)
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\interactiveshell.py", line 3020, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\interactiveshell.py", line 3185, in run_ast_nodes
if (yield from self.run_code(code, result)):
File "d:\machinelinerningproject\venv36\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 18, in
convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME")
File "d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 1044, in conv2d
data_format=data_format, dilations=dilations, name=name)
File "d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
op_def=op_def)
File "d:\machinelinerningproject\venv36\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in __init__
self._traceback = tf_stack.extract_stack()
UnknownError (see above for traceback): Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D_1 (defined at :18) = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_Placeholder_1_0_0/_1, Conv2D_1/filter)]]
[[{{node Conv2D_1/_3}} = _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_7_Conv2D_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
网上搜索了一下解决方案,但是并不是很好用。最后尝试了如下方法
- 安装cuda_9.0 补丁包,链接地址:https://developer.nvidia.com/cuda-90-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
- 更新显卡驱动程序,原来为395,现在更新到了419.17