我有一个形状为 [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/