这取决于您的应用程序。 GPU 利用率低并不罕见。尝试增加批量大小以提高利用率。
话虽如此,MNIST 规模的网络很小,很难为它们实现高 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))
输出 -
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%
希望这能回答您的问题。快乐学习。