有没有办法在 Tensorflow 中完成这种切片方法(使用 numpy 显示的示例)?
z = np.random.random((3,7,7,12))
x = z[...,[0,5]]
这样
x_hat = np.concatenate([z[...,[0]], z[...,[5]]], 3)
assert np.all(x == x_hat)
x.shape # (3, 7, 7, 2)
在Tensorflow中,这个操作
tfz = tf.constant(z)
i = np.array([0,5] dtype=np.int32)
tfx = tfz[...,i]
抛出错误
ValueError: Shapes must be equal rank, but are 0 and 1
From merging shape 0 with other shapes. for 'strided_slice/stack_1' (op: 'Pack') with input shapes: [], [2].
最佳答案
怎么样:
x = tf.stack([tfz[..., i] for i in [0,5]], axis=-1)
这对我有用:
z = np.random.random((3,7,7,12))
tfz = tf.constant(z)
x = tf.stack([tfz[..., i] for i in [0,5]], axis=-1)
x_hat = np.concatenate([z[...,[0]], z[...,[5]]], 3)
with tf.Session() as sess:
x_run = sess.run(x)
assert np.all(x_run == x_hat)
关于python - 用列表切片张量 - TensorFlow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46881006/