# -- encoding:utf-8 --
import numpy as np
import tensorflow as tf
# v1 = tf.constant(np.random.rand(10).astype(np.float32), name='abc')
# y = v1 * 0.1 + 0.3
# /job:ps 表示存储变量的节点。
with tf.device("/job:ps/task:1"): # '127.0.0.1:33332'
v1 = tf.constant(np.random.rand(10).astype(np.float32), name='v1')
# /job:work 表示计算执行的节点。
with tf.device("/job:work/task:0"): # '127.0.0.1:33333'
y1 = tf.add(v1 * 0.1, 0.5, name='y1')
with tf.device("/job:work/task:1"): # '127.0.0.1:33334'
y2 = tf.add(v1 * 0.1, 0.5, name='y2')
with tf.device("/job:work/task:2"): # '127.0.0.1:33335'
y3 = tf.add(v1 * 0.1, 0.5, name='y3')
y = tf.add(y1 + y2, y3, name='y')
with tf.Session(target='grpc://localhost:33335',
config=tf.ConfigProto(log_device_placement=True,
allow_soft_placement=True)) as sess:
# with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
print(sess.run(y))
# todo 关于tf.device参数使用,底层是用C写的。
"""
In TensorFlow a device name is a string of the following form:
// /job:<name>/replica:<replica>/task:<task>/device:<type>:<device_num>
//
// <name> is a short identifier conforming to the regexp
// [a-zA-Z][_a-zA-Z]*
// <type> is a supported device type (e.g. 'cpu' or 'gpu')
// <replica>, <task>, <device_num> are small non-negative integers and are
// densely allocated (except in tests).
//
// For some purposes, we also allow device patterns, which can specify
// some or none of the specific fields above, with missing components,
// or "<component>:*" indicating "any value allowed for that component.
//
// For example:
// "/job:param_server" - Consider any devices in the "param_server" job
// "/device:cpu:*" - Consider any cpu devices in any job/task/replica
// "/job:*/replica:*/task:*/device:cpu:*" - Consider any cpu devices in any
// job/task/replica
// "/job:w/replica:0/task:0/device:gpu:*" - Consider any gpu devices in
// replica 0, task 0, of job "w"
"""
3-Tensorflow-demo_12_01-client_demo
最新推荐文章于 2020-10-20 10:12:51 发布