对应的TensorFlow2.5.0+Keras2.5.0版本
安装完成总结测试可用的版本号
安装之前先安装一下cuda及cudnn的版本:
conda install -c nvidia cudnn=8.1.0.77
会自动匹配cuda的版本也可以两者都指定(cuda=11.1.74)
测试TensorFlow是否可以调用GPU训练:
import tensorflow as tf
# 判断当前TensorFlow是否使用GPU
if tf.test.gpu_device_name():
print("TensorFlow is using GPU")
else:
print("TensorFlow is not using GPU")
# 检测CUDA版本
try:
from tensorflow.python.platform import build_info
if build_info.cuda_version_number is not None:
print(f"CUDA version {build_info.cuda_version_number} is available")
except ImportError:
print("Could not detect CUDA version")
# 检测cuDNN版本
try:
cudnn_version = tf.__cudnn_version__
print(f"cuDNN version {cudnn_version} is available")
except AttributeError:
print("Could not detect cuDNN version")
TensorFlow-gpu2.5.0+Keras2.5.0+theano1.0.5+Python3.6.6+cuda11.1+cudnn8.1.0
1.查看keras版本
import keras
print(keras.__version__)
2.查看theano版本
import theano as th
th.__version__
3.查看tensorflow版本
import tensorflow as tf
tf.__version__
4.查看cudnn版本
dpkg -l | grep cudnn
5.测试TensorFlow和Keras是否可用
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
输出如下即可