Thenao tutorial – indexing

Theano和numpy一样,支持基本的下标取值方法和高级的下标取值方法。

因为theano中没有boolean类型,所以不支持boolean类型的masks。

# head file support
import numpy as np

numpy中的 Advanced Indexing:

高级下标取值用于获取非元组序列对象中的元素时,一般为 bdarray结构。

通常可以使用的取值方法包括:integer 和boolean

  • integer indexing
>>> x = np.array([[1, 2], [3, 4], [5, 6]])
>>> x[[0, 1, 2], [0, 1, 0]]
array([1, 4, 5])
  • boolean indexing
>>> x = np.array([1., -1., -2., 3])
>>> x[x < 0] += 20
>>> x
array([  1.,  19.,  18.,   3.])

numpy 的mask运算:

>>> n = np.arange(9).reshape(3,3)
>>> n[n > 4]  # mask
array([5, 6, 7, 8])

theano中mask运算:

>>> t = theano.tensor.arange(9).reshape((3,3))
>>> t[t > 4].eval()  # an array with shape (3, 3, 3)
array([[[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]],

       [[0, 1, 2],
        [0, 1, 2],
        [3, 4, 5]],

       [[3, 4, 5],
        [3, 4, 5],
        [3, 4, 5]]], dtype=int8)
原文地址:https://www.cnblogs.com/ZJUT-jiangnan/p/4885122.html