首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将可选输入添加到TensorFlow中的图形中?

如何将可选输入添加到TensorFlow中的图形中?
EN

Stack Overflow用户
提问于 2016-06-20 13:55:18
回答 1查看 3.5K关注 0票数 12

基本上,我希望有一个选项,将输入到图的中间,然后计算输出。我的一个想法是使用默认为零张量的tf.placeholder_with_default。然后,我可以混合使用加法的可选输入,但是在一个大的形状上添加,这似乎是很多不必要的计算。是否有更好的方法来实现这一点?

代码语言:javascript
复制
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.})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-20 15:04:41

多亏了你的代码我才能更好地理解。

基本上,模式是:

代码语言:javascript
复制
       input       <- you can feed here
         |        
     (encoder)
         |
     bottleneck    <- you can also feed here instead
         |
     (decoder)
         |
       output

您需要两个用例:

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

您不需要为bottleneck创建占位符,因为sess.run()允许您将值提供给图形中的非占位符

代码语言:javascript
复制
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。此外,如果键是占位符,则将检查值的形状是否与占位符兼容。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37924379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档