2017-08-07 15 views
1

フェッチする記号変数がいくつかあることから、どのプレースホルダが依存関係であるかを知る必要があります。 TheanoでTensorFlowのプレースホルダ依存関係を判断する方法

、我々は持っている:TensorFlowで同じことを行う方法

import theano as th 
import theano.tensor as T 

x, y, z = T.scalars('xyz') 
u, v = x*y, y*z 
w = u + v 

th.gof.graph.inputs([w]) # gives [x, y, z] 
th.gof.graph.inputs([u]) # gives [x, y] 
th.gof.graph.inputs([v]) # gives [y, z] 
th.gof.graph.inputs([u, v]) # gives [x, y, z] 

答えて

1

は組み込み関数は、(私の知っていること)はありませんが、それはものを作るのは簡単です:もちろん

# Setup a graph 
import tensorflow as tf 
placeholder0 = tf.placeholder(tf.float32, []) 
placeholder1 = tf.placeholder(tf.float32, []) 
constant0 = tf.constant(2.0) 
sum0 = tf.add(placeholder0, constant0) 
sum1 = tf.add(placeholder1, sum0) 

# Function to get *all* dependencies of a tensor. 
def get_dependencies(tensor): 
    dependencies = set() 
    dependencies.update(tensor.op.inputs) 
    for sub_op in tensor.op.inputs: 
     dependencies.update(get_dependencies(sub_op)) 
    return dependencies 

print(get_dependencies(sum0)) 
print(get_dependencies(sum1)) 
# Filter on type to get placeholders. 
print([tensor for tensor in get_dependencies(sum0) if tensor.op.type == 'Placeholder']) 
print([tensor for tensor in get_dependencies(sum1) if tensor.op.type == 'Placeholder']) 

、あなたは同様の機能にプレースホルダフィルタリングを投げることができました。

+0

この再帰は、グラフのような "フィボナッチ"ではおそらく効率的ではありませんが、実際には良い出発点です。 – Kh40tiK

関連する問題