Mih*_*nde 0 python keras tensorflow
我手动安装了CUDA v9.2和相应的cuDNN以安装tensorflow gpu,但我意识到tensorflow 1.8.0需要CUDA 9.0,所以我运行了
pip install tensorflow-gpu
Run Code Online (Sandbox Code Playgroud)
从anaconda提示(基本环境)中自动安装CUDA 9.0和相应的cuDNN。我从同一命令提示符启动了Spyder。所以这是我在Python 3.6中的代码,其中我正在使用keras和tensorflow来训练8000个奇数图像-
# Convolutional Neural Networks
# Part 1 - Building the CNN
# Not important
# Part 2- Fitting the CNN to the images -
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(
'dataset/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
test_set = test_datagen.flow_from_directory(
'dataset/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
with tf.device("/gpu:0"): # Notice THIS
classifier.fit_generator(
training_set,
steps_per_epoch=8000,
epochs=25,
validation_data=test_set,
validation_steps=2000)
Run Code Online (Sandbox Code Playgroud)
注意,在最后拟合数据集之前,我将其放入
with tf.device("/gpu:0"):
Run Code Online (Sandbox Code Playgroud)
我认为这应该确保它使用GPU进行训练?我不确定,因为将“ gpu:0”更改为“ cpu:0”会给出完全相同的训练时间(每个纪元18-20分钟)。如何确保Spyder中的tensorflow使用我的GPU?
我有一个NVIDIA GTX 970,所以它的CUDA兼容。另外我正在使用python 3.6,这是一个问题吗?我应该创建一个单独的Python 3.5环境并类似地安装tensorflow-gpu并尝试吗?
创建一个图形。
with tf.device('/device:GPU:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
r = sess.run(c)
print(r)
import numpy as np
assert np.all(r == np.array([[22., 28.], [49., 64.]]))
Run Code Online (Sandbox Code Playgroud)
或访问tensorflow网站(https://www.tensorflow.org/programmers_guide/using_gpu)
import tensorflow as tf
if tf.test.gpu_device_name():
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF")
Run Code Online (Sandbox Code Playgroud)
或这个 :
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Run Code Online (Sandbox Code Playgroud)