Skip to main content
 首页 » 编程设计

python之Tensorflow 性能(版本 1 与 2 和 CPU 与 GPU)

2024年11月01日41zhoujg

我是机器学习的新手,发现自己花费了过多的时间来设置 Tensorflow。我使用 Anaconda 来管理不同版本的环境。我成功安装了

  • Tensorflow-cpu_1.14.0

  • Tensorflow-gpu_1.14.0

  • Tensorflow-cpu_2.0.0-beta1.

由于 CUDA 驱动程序的一些问题,我未能成功设置 Tensorflow-gpu_2.0.0-beta,我暂时放弃了。

我的目标是确保上面指定的三个版本正常工作并使用我系统上的所有可用资源。特别是,我的问题是:

  1. 如何可靠地衡量现有计算机和 Tensorflow 设置的性能?

  2. 对于我使用的示例,仅 CPU 版本速度更快是否正常?

  3. 我应该如何为我的系统选择和安装最佳的 Tensorflow 设置?

我阅读了许多有关 Windows 性能问题以及 GPU 和 CPU 运行时比较的主题,但似乎没有一个主题可以解决上述问题。

我没有找到任何单一的既定标准示例来测试性能,所以我构建了自己的(可能是一个严重的错误)。我在我的家用电脑上测试了所有三种环境(使用下面指定的代码)(Windows 10 Home,x64。处理器:Intel i7-8750 CPU @2.20GHz,2208Mhz,6 核,12 个逻辑处理器。RAM:16GB。显卡: GeForce RTX 2060)。我还运行了来自 1 的测试示例,这表明使用 GPU 时矩阵乘法速度更快。我认为存在差异是有原因的,我失踪了。请随时评论代码中可能明显存在的基本思维错误。

# python 3.6 
import numpy as np 
import tensorflow as tf 
from tensorflow.python.client import device_lib 
from timeit import default_timer as timer 
 
model = tf.keras.Sequential() 
model.add(tf.keras.layers.Dense(64, activation='relu')) 
model.add(tf.keras.layers.Dense(64, activation='relu')) 
model.add(tf.keras.layers.Dense(10, activation='softmax')) 
 
model.compile(optimizer=tf.compat.v1.train.AdamOptimizer(0.001), 
              loss='categorical_crossentropy', 
              metrics=['accuracy']) 
 
 
def random_one_hot_labels(shape): 
    n, n_class = shape 
    classes = np.random.randint(0, n_class, n) 
    tmp_labels = np.zeros((n, n_class)) 
    tmp_labels[np.arange(n), classes] = 1 
    return tmp_labels 
 
 
data = np.random.random((1000, 32)) 
labels = random_one_hot_labels((1000, 10)) 
 
durations = [] 
for i in range(10):  # run N times 
    start = timer() 
    model.fit(data, labels, epochs=500, batch_size=32) 
    durations.append(timer() - start) 
 
print(f"tf.version.VERSION = {tf.version.VERSION}") 
print(f"tf.keras.__version__ = {tf.keras.__version__}") 
devices = device_lib.list_local_devices()  # this may allocate all GPU memory ?! 
print(f"devices = {[x.name for x in devices]}") 
print(f"model.fit durations: {durations}") 
 

纯 CPU 版本均优于 GPU 版本。此外,不同的 Tensorflow 版本之间存在巨大差异。在我的代码使用三种不同环境的输出下方:

tf.version.VERSION = 1.14.0 
tf.keras.__version__ = 2.2.4-tf 
2019-08-26 13:41:15.980626: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties: 
name: GeForce RTX 2060 major: 7 minor: 5 memoryClockRate(GHz): 1.2 
pciBusID: 0000:01:00.0 
2019-08-26 13:41:15.986261: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check. 
2019-08-26 13:41:15.990784: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0 
2019-08-26 13:41:15.993919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix: 
2019-08-26 13:41:15.997211: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187]      0 
2019-08-26 13:41:16.000263: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N 
2019-08-26 13:41:16.002807: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:0 with 4616 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2060, pci bus id: 0000:01:00.0, compute capability: 7.5) 
devices = ['/device:CPU:0', '/device:GPU:0'] 
model.fit durations: [34.81, 33.84, 34.37, 34.21, 34.54, 34.18, 35.09, 33.24, 33.32, 33.54] 
 
----------------------------------------------------------- 
 
tf.version.VERSION = 1.14.0 
tf.keras.__version__ = 2.2.4-tf 
devices = ['/device:CPU:0'] 
model.fit durations: [23.48, 23.43, 23.25, 23.71, 23.54, 24.017, 23.43, 24.08, 23.67, 23.94] 
 
----------------------------------------------------------- 
 
tf.version.VERSION = 2.0.0-beta1 
tf.keras.__version__ = 2.2.4-tf 
devices = ['/device:CPU:0'] 
model.fit durations: [15.53, 14.87, 14.65, 14.73, 14.68, 14.67, 15.11, 14.71, 15.54, 14.38] 

请您参考如下方法:

我用不同的网络大小测试了相同的代码。事实证明,当使用更大的网络时,GPU 版本的性能比仅使用 CPU 的版本要好得多。我怀疑这是由于将数据加载到 GPU 内存的开销所致。

如果你想测试这个,使用例如上述代码中每层 1024 个节点(并减少了 epoch 的数量)。