2016-06-20 37 views
7

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

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.}) 
+0

你能给出一个更具体的问题概述吗?你想要什么类型的可选输入,并做什么? –

+0

A有一个类似autoencoder的图形,我想用一个用于训练的图形重建代码作为瓶颈输入。 –

+0

你可以给你现在使用'tf.placeholder_with_default'的部分代码来改变吗? –

回答

10

由于您的代码,我理解得更好。

基本架构是:

 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中的每个键可以是以下类型之一:

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

相关问题