gpt4 book ai didi

python - tf.slice 和 tf.strided_slice

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:02 25 4
gpt4 key购买 nike

尝试理解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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com