Linux -- 使用多张gpu卡进行深度学习任务(以tensorflow为例)

在linux系统上进行多gpu卡的深度学习任务

  • 确保已安装最新的 TensorFlow GPU 版本。
python 复制代码
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
  • 1、确保你已经正确安装了tensorflow和相关的GPU驱动,这里可以通过在命令行输入nvidia-smi来查看:

    如果成功显示了类似上述的GPU信息和驱动版本信息,则说明NVIDIA驱动已经正确安装。

2、导入必要的库,设置可见的gpu设备列表:

python 复制代码
import tensorflow as tf
# 设置可见的GPU设备列表(例如,使用GPU 0、1、2和3)
gpu_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_visible_devices(gpu_devices, 'GPU')
  • 3、创建一个MirroredStrategy对象,该对象将自动复制模型和数据到每个可见的GPU卡上:
python 复制代码
strategy = tf.distribute.MirroredStrategy()
  • 4、在strategy范围内创建和训练模型:
python 复制代码
with strategy.scope():
    # 创建和编译模型
    model = create_model()
    model.compile(...)
    
    # 加载数据
    train_dataset = load_train_data()
    test_dataset = load_test_data()
    
    # 训练模型
    model.fit(train_dataset, validation_data=test_dataset, ...)

以上,在MirroredStrategy范围内创建的模型将自动复制并分布到每个可见的GPU卡上,每个卡都将处理一部分数据。

使用多个 GPU 的最佳做法是使用 tf.distribute.Strategy

以下给出一个官网的简单示例:

python 复制代码
tf.debugging.set_log_device_placement(True)
gpus = tf.config.list_logical_devices('GPU')
strategy = tf.distribute.MirroredStrategy(gpus)
with strategy.scope():
  inputs = tf.keras.layers.Input(shape=(1,))
  predictions = tf.keras.layers.Dense(1)(inputs)
  model = tf.keras.models.Model(inputs=inputs, outputs=predictions)
  model.compile(loss='mse',
                optimizer=tf.keras.optimizers.SGD(learning_rate=0.2))

当然,也有手动的放置方法:

python 复制代码
tf.debugging.set_log_device_placement(True)

gpus = tf.config.list_logical_devices('GPU')
if gpus:
  # Replicate your computation on multiple GPUs
  c = []
  for gpu in gpus:
    with tf.device(gpu.name):
      a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
      b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
      c.append(tf.matmul(a, b))

  with tf.device('/CPU:0'):
    matmul_sum = tf.add_n(c)

  print(matmul_sum)

在tensorflow上使用gpu:https://www.tensorflow.org/guide/gpu?hl=zh-cn

相关推荐
likfishdn1 分钟前
Linux的文件与目录管理
linux·运维·服务器
夜幕龙11 分钟前
深度生成模型(二)——基本概念与数学建模
人工智能·深度学习·transformer
山海青风17 分钟前
从零开始玩转TensorFlow:小明的机器学习故事 3
人工智能·机器学习·tensorflow
chian-ocean21 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=23 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
春末的南方城市28 分钟前
VidSketch:具有扩散控制的手绘草图驱动视频生成
人工智能·深度学习·计算机视觉·aigc
紫雾凌寒32 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
Francek Chen1 小时前
【大模型科普】AIGC技术发展与应用实践(一文读懂AIGC)
人工智能·深度学习·语言模型·大模型·aigc
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux