Keras Tensorflow 中的切片张量
Slice tensor in Keras Tensorflow
例如,我有形状为 (None, 2, 100, 100, 1024)
的张量,我想将 2
拆分为 1
和 1
以便我有 2 个阶数为 4 的张量(None, 100, 100, 1024)
。我如何使用 Keras Tensorflow 执行此操作?
谢谢。
使用tf.split()
:
import tensorflow as tf
tensor = tf.placeholder(tf.float32, (None, 2, 100, 100, 1024))
splitted = [tf.squeeze(t, axis=1) for t in tf.split(tensor, 2, axis=1)]
print(splitted[0].get_shape().as_list(), splitted[1].get_shape().as_list())
# [None, 100, 100, 1024] [None, 100, 100, 1024]
要连接回去:
# manipulate here ...
splitted = [t[:, None, ...] for t in splitted]
res = tf.concat(splitted, axis=1)
print(res.get_shape().as_list()) # [None, 2, 100, 100, 1024]
例如,我有形状为 (None, 2, 100, 100, 1024)
的张量,我想将 2
拆分为 1
和 1
以便我有 2 个阶数为 4 的张量(None, 100, 100, 1024)
。我如何使用 Keras Tensorflow 执行此操作?
谢谢。
使用tf.split()
:
import tensorflow as tf
tensor = tf.placeholder(tf.float32, (None, 2, 100, 100, 1024))
splitted = [tf.squeeze(t, axis=1) for t in tf.split(tensor, 2, axis=1)]
print(splitted[0].get_shape().as_list(), splitted[1].get_shape().as_list())
# [None, 100, 100, 1024] [None, 100, 100, 1024]
要连接回去:
# manipulate here ...
splitted = [t[:, None, ...] for t in splitted]
res = tf.concat(splitted, axis=1)
print(res.get_shape().as_list()) # [None, 2, 100, 100, 1024]