1

I have an input tensor as follow:

a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

and the 'multiple' tensor:

mul= tf.constant([1, 3, 2])

Is it possible to tile a 3D tensor with the first element of a appears once, the second appears 3 times, the last element appears twice?

result = [
             [[1, 2, 3]],
             [[4, 5, 6],[4, 5, 6],[4, 5, 6]],
             [[7, 8, 9], [7, 8, 9]]
                                   ]

Tensorflow 0.12

Thank you very much.

4

3 回答 3

2

不,这是不可能的。从文档中了解张量和形状。

要理解为什么不可能想象每行中元素数量不同的矩阵。它显然不是矩阵。

于 2017-05-14T21:53:38.460 回答
0

你可以使用 numpy

import numpy as np
import tensorflow as tf
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mul = np.array([1,3,2])
result = []
for i in range(len(mul)):
  result.append(np.tile(a[i], (mul[i], 1)))
result = np.array(result)
于 2017-05-14T15:39:47.857 回答
0

我敢肯定,张量流中不能有非矩形张量。这就是造成问题的原因。否则我只是扩展了@Kris 的代码以完全在 tensorflow 上运行。

import tensorflow as tf

sess = tf.InteractiveSession()

a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
mul = tf.constant([1,3,2])
result = []
for i in range(3):
  result.append(tf.tile(a[i],[mul[i]]))
print([r.eval() for r in result])
#r_tensor = tf.stack(0,[r for r in result]) # Not possible
于 2017-05-14T18:31:10.433 回答