tensorflw-gpu 运行 。py程序出现gpu不匹配的问题

安装好了tensorflow-gpu版本,然后程序中写好了 with tf.device('/gpu:0'):   但是python3 .py程序时还是有错误。

报错为:

2018-04-24 12:58:02.460531: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-04-24 12:58:02.463399: E tensorflow/stream_executor/cuda/cuda_driver.cc:406] failed call to cuInit: CUDA_ERROR_NO_DEVICE
2018-04-24 12:58:02.463444: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:158] retrieving CUDA diagnostic information for host: ip-172-31-23-4
2018-04-24 12:58:02.463455: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:165] hostname: ip-172-31-23-4
2018-04-24 12:58:02.463484: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:189] libcuda reported version is: 384.111.0
2018-04-24 12:58:02.463519: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:193] kernel reported version is: 384.111.0
2018-04-24 12:58:02.463527: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:300] kernel version seems to match DSO: 384.111.0

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'gradients/Mean_grad/Prod_1': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
[[Node: gradients/Mean_grad/Prod_1 = Prod[T=DT_INT32, Tidx=DT_INT32, keep_dims=false, _device="/device:GPU:0"](gradients/Mean_grad/Shape_2, gradients/Mean_grad/Const_1)]]

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'gradients/Mean_grad/Prod_1': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
[[Node: gradients/Mean_grad/Prod_1 = Prod[T=DT_INT32, Tidx=DT_INT32, keep_dims=false, _device="/device:GPU:0"](gradients/Mean_grad/Shape_2, gradients/Mean_grad/Const_1)]]

分析下错误:大概就是说找不到gpu设备,无法分配设备。

网上找了下相关问题:有两种解决方案

方案一 代码中添加:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

方案二

with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:

推荐方案1,因为方案二会打印一堆gpu信息

猜你喜欢

转载自www.cnblogs.com/smartwhite/p/8933859.html