【问题标题】:TensorFlow concat a variable-sized placeholder with a vectorTensorFlow 将可变大小的占位符与向量连接起来
【发布时间】:2016-07-02 15:46:20
【问题描述】:

假设我有一个占位符

ph_input = tf.placeholder(dtype=tf.int32, [None, 1])

和一个向量

h = tf.zeros([1,2], dtype=tf.int32)

在此示例中,h 为简单起见用零填充,但在实际情况下,它将被其他变量更改并具有不同的值。

我想在ph_inputh 上有效地在1 上执行concat 并得到一个形状为[None, 1+2] 的新张量。不幸的是,concat 需要所有输入张量具有相同的形状,除了 concat_dim,我的示例不满足。

我正在考虑将h 扩展为与提供给ph_input 的数据相同的形状,但我不确定如何使用占位符本身来做到这一点。如果我直接从输入数据中获取形状,那么我想没有必要使用占位符。

【问题讨论】:

    标签: concat tensorflow


    【解决方案1】:

    最通用的解决方案是使用tf.shape() op 获取占位符的运行时大小,并使用tf.tile() op 将h 扩展为合适的大小:

    ph_input = tf.placeholder(dtype=tf.int32, shape=[None, 1])
    h = tf.zeros([1, 2], dtype=tf.int32)  # ...or some other tensor of shape [1, 2]
    
    # Get the number of rows in the fed value at run-time.
    ph_num_rows = tf.shape(ph_input)[0]
    
    # Makes a `ph_num_rows x 2` matrix, by tiling `h` along the row dimension.
    h_tiled = tf.tile(h, tf.pack([ph_num_rows, 1]))
    
    result = tf.concat(1, [ph_input, h_tiled])
    

    【讨论】:

    • 完美。使用 tf 10 和 11 为我工作。
    • pack 已重命名为 stack !
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2019-09-29
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多