簡體   English   中英

訓練keras model時GPU的性能應該是多少?

[英]How much should GPU performance be when training keras model?

我在 mnist 數據集上使用 RTX 2080 ti。 我已經安裝了 tensrflow-gpu。 它比在其他環境中僅在 CPU 上運行快近 12 倍。

我在訓練時檢查任務管理器 CPU 和 GPU 性能。 以下是訓練期間的表現:

GPU 環境: CPU =20% GPU = 10% 訓練時間 = 24 秒

CPU 環境: CPU =100% GPU = 10% 訓練時間 = 500 秒

請問GPU跑10%正常嗎? 我可以手動提高或降低性能嗎?

這取決於您的應用程序。 GPU 的利用率較低並不罕見。 嘗試增加批量大小以獲得更多利用率。

話雖如此,MNIST size.networks 很小,很難為它們實現 GPU(或 CPU)的高效率,我認為 10% 的利用率和 CPU 對於您的應用程序來說並不罕見。 您將獲得更高的計算效率和更大的批量大小,這意味着您每秒可以處理更多的示例,但您也會獲得更低的統計效率,這意味着您需要處理更多的示例才能達到目標准確性。 所以這是一個權衡。 對於微小的字符模型,統計效率在 100 后下降得非常快,因此可能不值得嘗試增加批處理大小來進行訓練。 對於推理,您應該使用最大的批量大小。

您還可以設置要在程序中使用的設備類型。 在您的情況下,強制您的程序僅使用 GPU 並驗證 GPU 利用率。

例如在程序中使用 GPU 僅用於 model.fit

%tensorflow_version 2.x
print(tf.__version__)
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import model_from_json

# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")

# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]

# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Model Summary
model.summary()

# Fit the model
with tf.device("/device:GPU:0"):
  model.fit(X, Y, epochs=150, batch_size=10, verbose=0)

# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

Output -

2.2.0
Model: "sequential_6"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_18 (Dense)             (None, 12)                108       
_________________________________________________________________
dense_19 (Dense)             (None, 8)                 104       
_________________________________________________________________
dense_20 (Dense)             (None, 1)                 9         
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
accuracy: 78.39%

希望這能回答您的問題。 快樂學習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM