如何向 TensorFlow 中的图形添加可选输入?
How can I add an optional input to a graph in 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.})
感谢您的代码,我理解得更好。
基本上架构是:
input <- you can feed here
|
(encoder)
|
bottleneck <- you can also feed here instead
|
(decoder)
|
output
您需要两个用例:
- 训练:将图像输入
input
,计算输出
- test:将代码输入瓶颈,计算输出
您不需要为 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()
的文档:
The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.
我基本上希望能够选择将输入馈送到图表的中间并计算从那里开始的输出。我的一个想法是使用默认为零张量的 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.})
感谢您的代码,我理解得更好。
基本上架构是:
input <- you can feed here
|
(encoder)
|
bottleneck <- you can also feed here instead
|
(decoder)
|
output
您需要两个用例:
- 训练:将图像输入
input
,计算输出 - test:将代码输入瓶颈,计算输出
您不需要为 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()
的文档:
The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.