我对tensorflow的新选项有一些麻烦,它允许我们运行分布式张量流.
我只想运行2个tf.constant和2个任务,但我的代码永远不会结束.它看起来像那样:
import tensorflow as tf
cluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223"]})
server = tf.train.Server(cluster,
job_name="local",
task_index=0)
with tf.Session(server.target) as sess:
with tf.device("/job:local/replica:0/task:0"):
const1 = tf.constant("Hello I am the first constant")
with tf.device("/job:local/replica:0/task:1"):
const2 = tf.constant("Hello I am the second constant")
print sess.run([const1, const2])
Run Code Online (Sandbox Code Playgroud)
我有以下代码(只有一个localhost:2222):
import tensorflow as tf
cluster = tf.train.ClusterSpec({"local": ["localhost:2222"]})
server = tf.train.Server(cluster,
job_name="local",
task_index=0)
with tf.Session(server.target) as sess:
with tf.device("/job:local/replica:0/task:0"):
const1 = tf.constant("Hello I am the first constant")
const2 = tf.constant("Hello I am the second constant")
print …Run Code Online (Sandbox Code Playgroud) 我用gpu,cuda 7.0和cudnn 6.5安装了tensorflow.当我导入张量流时,效果很好.
我试图在Tensorflow上运行一个简单的矩阵乘法,它不想使用我的gpu,虽然它似乎认识到它.我的计算机上有一个nvidia geforce 970m和一个带有两个titan Z的集群.
我的第一个代码是:
import tensorflow as tf
import numpy as np
size=100
#I create 2 matrix
mat1 = np.random.random_sample([size, size])*100
mat2 = np.random.random_sample([size, size])*100
a = tf.constant(mat1)
b = tf.constant(mat2)
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(c)
Run Code Online (Sandbox Code Playgroud)
此代码有效,结果如下:
Const_1: /job:localhost/replica:0/task:0/gpu:0
I tensorflow/core/common_runtime/simple_placer.cc:289] Const_1: /job:localhost/replica:0/task:0/gpu:0
Const: /job:localhost/replica:0/task:0/gpu:0
I tensorflow/core/common_runtime/simple_placer.cc:289] Const: /job:localhost/replica:0/task:0/gpu:0
MatMul: /job:localhost/replica:0/task:0/cpu:0
I tensorflow/core/common_runtime/simple_placer.cc:289] MatMul: /job:localhost/replica:0/task:0/cpu:0
Run Code Online (Sandbox Code Playgroud)
所以在我的方式中,tensorflow使用我的gpu创建常量但不是matmul(这很奇怪).然后,我像这样强制gpu:
with tf.device("/gpu:0"):
a = tf.constant(mat1)
b = tf.constant(mat2)
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(c)
Run Code Online (Sandbox Code Playgroud)
并且Tensorflow返回: …