我有一个形状为[6,20,30,6]的4-D张量,我想执行以下等效的keras / tensorflow:

new = np.array([old[i,:,:,i] for i in range(6)])

任何帮助表示赞赏!

最佳答案

您可以扩展old的尺寸,使用理解列表选择所需的切片,然后将它们沿着扩展的尺寸连接起来。例如:

import tensorflow as tf
import numpy as np

tensor_shape = (6, 20, 30, 6)
old = np.arange(np.prod(tensor_shape)).reshape(tensor_shape)
new = np.array([old[i, :, :, i] for i in range(6)])

old_ = tf.placeholder(old.dtype, tensor_shape)
new_ = tf.concat([old[None, i, :, :, i] for i in range(6)], axis=0)

with tf.Session() as sess:
    new_tf = sess.run(new_, feed_dict={old_: old})
    assert (new == new_tf).all()

关于python - Keras:如何仅从张量中提取某些层,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54084546/

10-12 21:25