# -- encoding:utf-8 --
import tensorflow as tf
# 0. 用于命令行传参数(标准接口)
"""
第一个:参数名字;第二个:默认值;第三个:参数描述
DEFINE_float
DEFINE_bolean
"""
tf.app.flags.DEFINE_string('job_name', default_value='work',
docstring='必须给定一个任务名称,可选值: ps or work')
tf.app.flags.DEFINE_integer('task_index', default_value=0,
docstring='必须给定一个启动的id')
FLAGS = tf.app.flags.FLAGS
# 1. 定义服务器(节点)都在哪些机器上,并且使用什么端口号提供服务
# ps:表示用于变量的存储节点
# work:表示用于工作计算的节点
ps_hosts = ['127.0.0.1:33331', '127.0.0.1:33332']
work_hosts = ['127.0.0.1:33333', '127.0.0.1:33334', '127.0.0.1:33335']
# 创建集群
cluster = tf.train.ClusterSpec({'ps': ps_hosts, 'work': work_hosts})
def main(_):
# 构建server服务器
"""
使用给定的定义创建server服务器
tf.train.Server(object):
def __init__(self,
server_or_cluster_def, # 集群对象
job_name=None, # 服务器所属作业名称
task_index=None, # 指定服务器任务工作索引
protocol=None,
config=None,
start=True):
"""
server = tf.train.Server(cluster, job_name=FLAGS.job_name, task_index=FLAGS.task_index)
# 启动线程等待client的链接
server.join()
# 使用这种方式保证了,如果此文件被其他文件 import的时候,不会执行main 函数
if __name__ == '__main__':
tf.app.run() # 解析命令行参数,调用main函数
3-Tensorflow-demo_12_02-server_demo
最新推荐文章于 2023-05-11 18:04:15 发布
本文介绍如何使用TensorFlow进行分布式训练,包括通过tf.app.flags设置命令行参数,定义集群节点和服务器,以及如何运行主函数启动服务器等待客户端连接。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
TensorFlow-v2.15
TensorFlow
TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

3201

被折叠的 条评论
为什么被折叠?



