问题 如何在TensorFlow中向图形添加可选输入?


我基本上想要选择将输入提供给图形的中间并计算从那里输出的输出。我有一个想法是使用 tf.placeholder_with_default 默认为零张量。然后我可以使用加法混合可选输入,但是在大形状上添加这似乎是很多不必要的计算。有没有更好的方法来实现这一目标?

input_enabled = tf.placeholder_with_default(tf.constant(1.), [1])

input_shape = [None, in_size]
input = tf.placeholder_with_default(tf.zeros(input_shape), input_shape)
// ...
bottleneck_shape = [None, bottleneck_size]
bottleneck = input_enabled * f(prev_layer) + tf.placeholder_with_default(tf.zeros(bottleneck_shape), bottleneck_shape)
// ...

// Using graph with input at first layer:
sess.run([output], feed_dict={input: x})

// Using graph with input at bottleneck layer:
sess.run([output], feed_dict={bottleneck: b, input_enabled: 0.})

2482
2018-06-20 13:55


起源

您能否对问题进行更具体的概述?您想要什么类型的可选输入,以及做什么? - Olivier Moindrot
A有一个类似自动编码器的图形,我想用一个用于训练的相同图形重建代码作为瓶颈的输入。 - Lenar Hoyt
你能给出你现在拥有的部分代码吗? tf.placeholder_with_default 你想改变吗? - Olivier Moindrot
我添加了一些伪代码。 - Lenar Hoyt
PS,您可以通过获取/提供“<name>:0”(print tf.get_default_graph()。as_graph_def()来获取<name>)来提取或获取TensorFlow图中的任何节点。 - Yaroslav Bulatov


答案:


感谢您的代码我明白了。

架构基本上是:

       input       <- you can feed here
         |        
     (encoder)
         |
     bottleneck    <- you can also feed here instead
         |
     (decoder)
         |
       output

你想要两个用例:

  1. 培养:将图像输入 input,计算输出
  2. 测试:将代码输入瓶颈,计算输出

您无需为其创建占位符 bottleneck因为 sess.run() 允许您将值提供给 非占位符 在图表中:

input_shape = [None, in_size]
input = tf.placeholder(tf.float32, input_shape)
# ...

bottleneck = f(prev_layer)  # of shape [None, bottleneck_size]
# ...

# Using graph with input at first layer:
sess.run([output], feed_dict={input: x})

# Using graph with input at bottleneck layer:
sess.run([output], feed_dict={bottleneck: b})

从文档中 sess.run()

可选的feed_dict参数允许调用者覆盖图中的张量值。 feed_dict中的每个键都可以是以下类型之一:

如果键是Tensor,则该值可以是Python标量,字符串,列表或numpy ndarray,可以转换为与该张量相同的dtype。此外,如果键是占位符,则将检查值的形状是否与占位符兼容。


13
2018-06-20 15:04