gpt4 book ai didi

concatenation - TensorFlow 将可变大小的占位符与向量连接起来

转载 作者:行者123 更新时间:2023-12-02 08:15:45 25 4
gpt4 key购买 nike

假设我有一个占位符

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

和一个向量

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

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

我想在维度 1 上有效地对 ph_inputh 执行 concat 并获得新的张量形状为[无,1+2]。不幸的是,concat 需要除 concat_dim 之外的所有输入张量具有相同的形状,而我的示例不满足这一要求。

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

最佳答案

最通用的解决方案是使用 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])

关于concatenation - TensorFlow 将可变大小的占位符与向量连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com