任意大小的Tensorflow上采样张量

时间:2017-06-01 08:03:26

标签: tensorflow

假设我有一个有形状的张量

[d0, d1,.., dn]

是否可以创建一个仅对某个维度k次进行上采样的函数? 一个例子

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

如果我将其应用于具有重复因子2

的维3
[[1,1,1,2,2,2],[3,3,3,4,4,4]]

我知道有一个tf.image.resize,但它需要一个特定形状的张量。

1 个答案:

答案 0 :(得分:1)

tf.concat怎么样?

这就是我的想法:如果输入的形状是[d1,d2,...,dn],那么输出的形状应该是[d1,d2,...,dn * 3]。如果我是对的,下面的代码可能会解决您的问题。

import tensorflow as tf
import numpy as np


def repetition(a, factor):
    # get a's shape as a list
    shape = a.get_shape().as_list()
    concat_shape = shape + [1]

    # new shape
    shape[-1] *= factor

    a = tf.reshape(a, concat_shape)
    b = tf.reshape(tf.concat([a] * factor, -1), shape)

    return b

d1 = 2
d2 = 3
d3 = 4

np_a = np.random.rand(d1, d2, d3)
a = tf.constant(np_a)

b = repetition(a, 3)


sess = tf.Session()

print np_a
print sess.run(b)

关键是tf.concat可以指示它将连接张量的轴。