gpt4 book ai didi

python - 如何在Tensorflow中形成多层张量

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

在tensorflow中,我有一个形状为[2,3,3,1]的张量,现在我想将张量复制到多层到形状为[2,3,3,3]的张量,我怎样才能这样做吗?

最佳答案

您可以使用tf.tiletf.concat来实现此目的:

t = tf.random_uniform([2, 3, 3, 1], 0, 1)
s1 = tf.tile(t, [1, 1, 1, 3])
s2 = tf.concat([t]*3, axis=-1)

with tf.Session() as sess:
tnp, s1np, s2np = sess.run([t, s1, s2])
print(tnp.shape)
print(s1np.shape)
print(s2np.shape)

打印内容

(2, 3, 3, 1)
(2, 3, 3, 3)
(2, 3, 3, 3)

为了说明发生的情况,查看 2d 示例可能会更容易:

import tensorflow as tf

t = tf.random_uniform([2, 1], 0, 1)
s1 = tf.tile(t, [1, 3])
s2 = tf.concat([t]*3, axis=-1)

with tf.Session() as sess:
tnp, s1np, s2np = sess.run([t, s1, s2])
print(tnp)
print(s1np)
print(s2np)

打印内容

[[0.52104855]
[0.95304275]]
[[0.52104855 0.52104855 0.52104855]
[0.95304275 0.95304275 0.95304275]]
[[0.52104855 0.52104855 0.52104855]
[0.95304275 0.95304275 0.95304275]]

关于python - 如何在Tensorflow中形成多层张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897339/

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