python - 用列表切片张量 - TensorFlow

标签 python tensorflow

有没有办法在 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/

相关文章:

python - 从给定字符开始的给定长度的字符列表中创建所有可能的单词

machine-learning - 机器学习: Why xW+b instead of Wx+b?

python - 为 tf.contrib.learn.Estimator 使用 input_fn 时设置 batch_size

python - tensorflow 中的参数值

python - 创建一个字典列表,每一步仅修改一个值

python - 使用 **kwargs 在 factory boy test 中调用函数

python - Jython 比 Python 快吗?

python - sqlite 游标 fetchone() 返回 'NoneType' ?

python - 获取一个 python 实例的总内存和 cpu 使用率

tensorflow - Keras:自定义损失函数,训练数据与模型不直接相关