Tensorflow分布式训练实例详解
程序员文章站
2022-05-26 20:43:38
...
一、Tensorflow分布式训练策略 |
模型并行 in-graph replication:将模型部署到很多设备上运行,比如一个或多个机器的GPUs, 不同 GPU 负责网络模型的不同部分。
数据并行 between-graph replication:每个Worker拥有模型的完整副本但分配到不同的数据,各自单独地训练,然后将计算结果合并。这是目前主流的做法。
同步更新: 等到所有 GPU 的梯度计算完成,再统一根据梯度均值计算新参数,然后所有 GPU 同步新值后,才进行下一轮计算,loss下降比较稳定,但需要等待最慢的计算节点。
异步更新:所有 GPU 分别计算,分别更新参数,计算资源能充分利用,但loss下降不稳定,存在梯度失效问题。
1. gRPC (google remote procedure call) TensorFlow分布式并行基于gRPC通信框架(谷歌远程过程调用),其中包括一个master创建Session,还有多个worker负责执行计算图中的任务。
2. Ring AllReduce架构
PS架构中,当worker数量较多时,ps节点的网络带宽将成为系统的瓶颈。
Ring AllReduce架构中各个设备都是worker,没有中心节点来聚合所有worker计算的梯度。所有 device 在一个逻辑环路中,每个 device 从上 行的device 接收数据,并向下行的 deivce 发送数据。
|
二、TensorFlow Estimator API |
首先定义一个由参与分布式计算的机器组成的集群,集群中一般有多个worker,需要指定其中一个worker为主节点(cheif),chief节点会执行一些额外的工作,比如模型导出之类的。在PS分布式架构环境中,还需要定义ps节点。如下:
设置好TF_CONFIG环境变量:
|
三、TensorFlow分布式MNIST手写字体识别实例 |
项目地址: https://github.com/TracyMcgrady6/Distribute_MNIST
ps 节点执行:
worker1 节点执行:
worker2 节点执行:
|
上一篇: MARKDOWN操作
下一篇: Unix 桌面环境 CDE 开源