基本上,我希望有一个选项,将输入到图的中间,然后计算输出。我的一个想法是使用默认为零张量的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.})发布于 2016-06-20 15:04:41
多亏了你的代码我才能更好地理解。
基本上,模式是:
input <- you can feed here
|
(encoder)
|
bottleneck <- you can also feed here instead
|
(decoder)
|
output您需要两个用例:
input,计算输出您不需要为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中的每个键都可以是以下类型之一: 如果键是张量,则该值可能是Python标量、字符串、列表或numpy ndarray,可以转换为与该张量相同的dtype。此外,如果键是占位符,则将检查值的形状是否与占位符兼容。
https://stackoverflow.com/questions/37924379
复制相似问题