Jef*_*ker 6 python keras tensorflow tensorflow2.0
所以我最近尝试在具有以下规格的 PC 上运行 tensorflow-gpu:
AMD Ryzen 5 2600X 6 核,NVIDIA GeForce RTX 2060,16 GB 内存
我跑了内置了在本教程时尚MNIST数据集colab。我运行了以下代码并注意到 colab 没有在 gpu 上运行:
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
所以我浏览了教程并基本上运行了他们的代码:
import tensorflow as tf
import time
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images = training_images / 255.0
test_images = test_images / 255.0
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
start_time = time.time()
model.compile(optimizer = tf.keras.optimizers.Adam(),
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=5)
print("My program took {} seconds to run".format(time.time() - start_time))
Run Code Online (Sandbox Code Playgroud)
我注意到在 colab 上编译和拟合数据需要大约 17 秒。当我在确实检测到 GPU 的计算机上运行它时,执行相同的过程需要大约 13 秒。我的印象是我拥有的 GPU 会快数光年,所以我想知道我的设置有什么问题,或者我是否错误地使用了我的 GPU。
此外,我正在运行 python 3.7.7、tensorflow 版本 2.1.0 和 keras 版本 2.2.4-tf。
乐意效劳!
我认为问题在于该程序使用的计算资源很少。测试一下我的程序,也许你会看到GPU和CPU之间的区别~
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import tensorflow as tf
from datetime import datetime
len=10000 # Set shape(bigger, gpu runs faster and cpu runs slower.)
device_name = gpu # Choose device from cmd line. Options: gpu or cpu
shape = (int(len), int(len))
if device_name == "gpu":
device_name = "/gpu:0"
else:
device_name = "/cpu:0"
with tf.device(device_name):
# tensorflow 1.x use below:
# random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
# tensorflow 2.x use below:
random_matrix = tf.random.uniform(shape=shape, minval=0, maxval=1)
dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
sum_operation = tf.reduce_sum(dot_operation)
startTime = datetime.now()
# if tf v1.x, use below:
#with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
# if tf v2.x, use below:
with tf.compat.v1.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
result = session.run(sum_operation)
print(result)
# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.
print("\n" * 5)
print("Shape:", shape, "Device:", device_name)
print("Time taken:", datetime.now() - startTime)
print("\n" * 5)
Run Code Online (Sandbox Code Playgroud)
希望它能有所帮助。
祝你有美好的一天:)