张量流中将2d张量转换为3d

时间:2017-08-19 17:21:47

标签: tensorflow

我需要将2d张量转换为3d张量。如何在张量流中传递这个。

[[30, 29, 19, 17, 12, 11],
       [30, 27, 20, 16,  5,  1],
       [28, 25, 17, 14,  7,  2],
       [28, 26, 21, 14,  6,  4]]

到这个

[[[0,30], [0,29], [0,19], [0,17], [0,12], [0,11]],
       [[1,30], [1,27], [1,20], [1,16],[1,5], [1,1]],
       [[2,28], [2,25], [2,17], [2,14], [2,7],  [2,2]],
       [[3,28], [3,26], [3,21], [3,14], [3,6],  [3,4]]]

谢谢!我这样做是为了实现How to select rows from a 3-D Tensor in TensorFlow? @kom

中的问题

1 个答案:

答案 0 :(得分:1)

这是从3D张量

获得2D张量的解决方法
a = tf.constant([[30, 29, 19, 17, 12, 11],
   [30, 27, 20, 16,  5,  1],
   [28, 25, 17, 14,  7,  2],
   [28, 26, 21, 14,  6,  4]], dtype=tf.int32)
a = tf.expand_dims(a, axis=2)

b = tf.constant(np.asarray([i*np.ones(a.shape[1]) for i in range(0, a.shape[0])], dtype=np.int32), dtype=tf.int32)
b = tf.expand_dims(b, axis=2)

final_ids = tf.concat([b, a], axis=2)