使用 Python 脚本调用四张 GPU 卡运行深度学习
以下是一个使用 Python 脚本调用四张卡并运行深度学习的示例:
import tensorflow as tf
# 设置使用的 GPU 数量
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# 限制使用四张卡
tf.config.experimental.set_visible_devices(gpus[:4], 'GPU')
for gpu in gpus[:4]:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), 'Physical GPUs,', len(logical_gpus), 'Logical GPUs')
except RuntimeError as e:
print(e)
# 在这里编写你的深度学习代码
# 例如:
# model = tf.keras.Sequential([
# tf.keras.layers.Dense(64, activation='relu'),
# tf.keras.layers.Dense(10, activation='softmax')
# ])
# model.compile(optimizer='adam',
# loss='sparse_categorical_crossentropy',
# metrics=['accuracy'])
# model.fit(x_train, y_train, epochs=10, batch_size=32)
请注意,这只是一个示例脚本,你需要根据你的具体需求和深度学习模型进行适当的修改。此外,确保你的机器上安装了适当的深度学习库(如 TensorFlow)和 GPU 驱动程序。
原文地址: https://www.cveoy.top/t/topic/fOtD 著作权归作者所有。请勿转载和采集!