3-Tensorflow-demo_12_01-client_demo

# -- 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"
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值