gpt4 book ai didi

使用广播语义进行转置的tensorflow concat

转载 作者:行者123 更新时间:2023-12-05 07:34:23 32 4
gpt4 key购买 nike

假设 v1v2 具有相同的形状。在 tensorflow 中是否可以使用广播语义连接 v1v2转置版本?

例如,

v1 = tf.constant([[1,1,1,1],[3,3,3,3],[5,5,5,5]])
v2 = tf.constant([[2,2,2,2],[4,4,4,4]])

我想制作类似的东西

[
[[[1,1,1,1], [2,2,2,2]],
[[1,1,1,1], [4,4,4,4]]],
[[[3,3,3,3], [2,2,2,2]],
[[3,3,3,3], [4,4,4,4]]],
[[[5,5,5,5], [2,2,2,2]],
[[5,5,5,5], [4,4,4,4]]]]

v1[3, 4]v2[2,4] , 我想做

tf.concat([v1, tf.transpose(v2)], axis=0)

并产生一个[3,2,2,4]矩阵。

这样做有什么技巧吗?

最佳答案

如果您的意思是技巧是一个优雅的解决方案,我不这么认为。然而,一个可行的解决方案是平铺和重复传入的 v1、v2

import tensorflow as tf

v1 = tf.constant([[1, 1, 1, 1],
[3, 3, 3, 3],
[7, 7, 7, 7],
[5, 5, 5, 5]])
v2 = tf.constant([[2, 2, 2, 2],
[6, 6, 6, 6],
[4, 4, 4, 4]])


def my_concat(v1, v2):
v1_m, v1_n = v1.shape.as_list()
v2_m, v2_n = v2.shape.as_list()

v1 = tf.concat([v1 for i in range(v2_m)], axis=-1)
v1 = tf.reshape(v1, [v2_m * v1_m, -1])

v2 = tf.tile(v2, [v1_m, 1])
v1v2 = tf.concat([v1, v2], axis=-1)

return tf.reshape(v1v2, [v1_m, v2_m, 2, v2_n])


with tf.Session() as sess:
ret = sess.run(my_concat(v1, v2))

print ret.shape
print ret

关于使用广播语义进行转置的tensorflow concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128777/

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