gpt4 book ai didi

Tensorflow:如何处理试图平铺和连接两个张量的动态形状?

转载 作者:行者123 更新时间:2023-12-04 01:17:10 25 4
gpt4 key购买 nike

我有两个张量,例如 x = [1,2] and y=[3] ,我想沿另一个轴复制最后一个,获得z = [[1,3],[2,3]].理想情况下在 tensorflow 中:

x = tf.placeholder(shape=[None, 2], dtype = tf.float32)
y = tf.placeholder(shape=[1], dtype = tf.float32)
z = tf.concat(x, tf.tile(y, [ x.shape[0] ]) , 1)

问题是 x 占位符第一个维度未确定,我该如何解决?

最佳答案

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

x = tf.placeholder(shape=[None, 2], dtype = tf.float32)
y = tf.placeholder(shape=[1], dtype = tf.float32)

dim = tf.shape(x)[0]
y1 = tf.expand_dims(y, axis = 1)
y1 = tf.tile(y1, [dim, 1])
z = tf.concat((x, y1), axis = 1)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
z_val = sess.run(z, feed_dict = {x:[[2,5],[5,7],[8,9]], y:[3]})
print(z_val)

输出:
[[ 2.  5.  3.]
[ 5. 7. 3.]
[ 8. 9. 3.]]

关于Tensorflow:如何处理试图平铺和连接两个张量的动态形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47537552/

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