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

标签 python tensorflow

在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/

相关文章:

python - 类型错误 : Mismatch between array dtype ('object' ) and format specifier ('%.18e' )

python - TensorFlow - `keys` 或 `default_value` 与表数据类型不匹配

python - 如何在 Keras API 中将数组列表作为输入

python - 使用 cv2 显示unet预测图像

tensorflow - tf.contrib.layers.embed_sequence() 是干什么用的?

python - TypeError at/admin/jobs/job/add/'tuple' 不支持缓冲区接口(interface)

python - 如何将日期时间转换为日期?

python-3.x - 形状与多类分类器不兼容

python - 保持对象按多个键排序的高效数据结构

python - Python 中真正的非阻塞 HTTPS 服务器