python - tf.slice 和 tf.strided_slice

标签 python tensorflow nlp

尝试理解tensorflow strided_slice 和 slice

x = tf.constant(np.array(   [[[111, 112, 113], [121, 122, 123]],
                            [[211, 212, 213], [221, 222, 223]],
                            [[311, 312, 313], [321, 322, 323]]]))
with tf.Session() as sess:
    print("tf.shape ------------------")
    print(sess.run(tf.shape(x)))
    print("tf.slice ------------------------")
    print(sess.run((tf.slice(x, [1, 0, 0], [2, 1, 3]) )))
    print("tf.strided_slice ------------------------")
    print(sess.run(tf.strided_slice(x, [1, 0, 0], [2, 1, 3], [1, 1, 1])))
    print(sess.run(tf.strided_slice(x, [1, -1, 0], [2, -3, 3], [1, -1, 1])))
    print(sess.run(x[1,-1,0]))
    print(sess.run(x[2,-3,3]))

输出

tf.shape ------------------
[3 2 3]
tf.slice ------------------------
[[[211 212 213]]

 [[311 312 313]]]
tf.strided_slice ------------------------
[[[211 212 213]]]
[[[221 222 223]
  [211 212 213]]]
221
ValueError: slice index -1 of dimension 1 out of bounds. for 'strided_slice_8' (op: 'StridedSlice') with input shapes: [3,2,3], [3], [3], [3] and with computed input tensors: input[1] = <2 -3 3>, input[2] = <3 -2 4>, input[3] = <1 1 1>.

对于 tf.slice,我知道我们必须提及每个维度中的切片大小,因此超出范围的值是有意义的。但在 strided 切片中,末尾是张量本身的张量索引,如何得出大小值有效。

示例取自 https://www.tensorflow.org/api_docs/python/tf/strided_slice

尝试从纸张实现折叠层部分 A Convolutional Neural Network for Modelling Sentences

In the formulation of the network so far, feature detectors applied to an individual row of the sentence matrix s can have many orders and create complex dependencies across the same rows in multiple feature maps. Feature detectors in different rows, however, are independent of each other until the top fully connected layer. Full dependence between different rows could be achieved by making M in Eq. 5 a full matrix instead of a sparse matrix of diagonals. Here we explore a simpler method called folding that does not introduce any additional parameters. After a convolutional layer and before (dynamic) k-max pooling, one just sums every two rows in a feature map component-wise. For a map of d rows, folding returns a map of d/2 rows, thus halving the size of the representation. With a folding layer, a feature detector of the i-th order depends now on two rows of feature values in the lower maps of order i − 1. This ends the description of the DCNN.

最佳答案

产生的 ValueError 与对 Strided_Slice 的前 2 次调用没有任何关系。您对 Strided_Slice 操作的使用正确的是

print(sess.run(tf.strided_slice(x, [1, 0, 0], [2, 1, 3], [1, 1, 1])))

print(sess.run(tf.strided_slice(x, [1, -1, 0], [2, -3, 3], [1, -1, 1])))

您的问题在于调用

print(sess.run(x[2,-3,3]))

Python 数组中的负索引会反向迭代数组。

例如,给定一个像这样的数组

arr = ['a', 'b', 'c', 'd', 'e', 'f']

调用 arr[-1] 将产生“f”。类似地,调用 arr[-4] 将产生“c”。如果我们尝试调用 arr[-7] 会发生什么?这将尝试访问索引 -1,这会引发错误。

请记住,Python 中的数组具有从 0 开始的索引。对 x[2,-3, 3] 的调用最初访问外部数组中索引 2 处的元素(第三个元素),即

[[311, 312, 313], [321, 322, 323]]

现在,在这个外部数组中,有 2 个元素。但是,您的调用 x[2, -3, 3] 尝试访问索引 -1 处的元素,因为它从数组末尾迭代。这就是产生错误的原因

slice index -1 of dimension 1 out of bounds

注意:您尝试访问 x[2, -3, 3] 中的最后一个索引也会产生 ValueError,因为它尝试访问的索引不是在数组中。要解决此问题,您的调用可以是 x[2, -2, 2]。

以下是有关 Python 中的跨步切片、切片和数组索引的一些链接: https://www.tensorflow.org/api_docs/python/tf/strided_slice

https://www.tensorflow.org/api_docs/python/tf/slice

Negative list index?

关于python - tf.slice 和 tf.strided_slice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900233/

相关文章:

python - 如何对这样的列表进行排序?

python - 我的带有蒙特卡洛 dropout 的模型是否应该提供类似于确定性预测的平均预测?

delphi - 如何从用户的自然语言查询中选择常见问题解答条目?

python - 层 lstm_5 的输入 0 与层 : expected ndim=3, 不兼容,发现 ndim=2

python - PySide *任何*方法调用 - 信号和插槽

python - 如何使用shell命令通过应用程序名称查找PID

python - sqlalchemy 中插入操作的列名和类型

python - 我如何判断 tf op 是否具有梯度?

tensorflow - 由于环境错误 : [WinError 5] Access is denied:,无法安装软件包

可以访问包括单词定义在内的英语词典的 Python 模块