索引变量范围(单位:numpy)

索引变量范围(单位:numpy),numpy,tensorflow,Numpy,Tensorflow,我有一个numpy零矩阵a的形状(2,5) 我有另一个数组seq,大小为2。这与A的第一个轴相同 seq = [2, 3] 我想创建另一个矩阵B,如下所示: B = [[ 1., 1., 0., 0., 0.], [ 1., 1., 1., 0., 0.]] B是通过将A的ith行中的第一个seq[i]元素更改为1来构造的 这是一个玩具的例子A和seq可能很大,因此需要效率如果有人知道如何在tensorflow中执行此操作,我将非常感激。您可以在tensorflow中

我有一个numpy零矩阵
a
的形状
(2,5)

我有另一个数组
seq
,大小为
2
。这与
A
的第一个轴相同

seq = [2, 3]
我想创建另一个矩阵
B
,如下所示:

B = [[ 1.,  1.,  0.,  0.,  0.],
    [ 1.,  1.,  1.,  0.,  0.]]
B
是通过将
A
ith
行中的第一个
seq[i]
元素更改为
1
来构造的


这是一个玩具的例子
A
seq
可能很大,因此需要效率如果有人知道如何在tensorflow中执行此操作,我将非常感激。

您可以在tensorflow中执行此操作(并使用NumPy中的一些类似代码),如下所示:

seq = [2, 3]

b = tf.expand_dims(tf.range(5), 0)   # A 1 x 5 matrix.
seq_matrix = tf.expand_dims(seq, 1)  # A 2 x 1 matrix.
b_bool = tf.greater(seq_matrix, b)   # A 2 x 5 bool matrix.

B = tf.to_int32(b_bool)              # A 2 x 5 int matrix.
示例输出:

In [7]: b = tf.expand_dims(tf.range(5), 0)
        [[0 1 2 3 4]]

In [21]: b_bool = tf.greater(seq_matrix, b)
In [22]: op = sess.run(b_bool)
In [23]: print(op)
[[ True  True False False False]
 [ True  True  True False False]]

In [24]: bint = tf.to_int32(b_bool)
In [25]: op = sess.run(bint)
In [26]: print(op)
[[1 1 0 0 0]
 [1 1 1 0 0]]

您可以在TensorFlow中执行此操作(并使用NumPy中的一些类似代码),如下所示:

seq = [2, 3]

b = tf.expand_dims(tf.range(5), 0)   # A 1 x 5 matrix.
seq_matrix = tf.expand_dims(seq, 1)  # A 2 x 1 matrix.
b_bool = tf.greater(seq_matrix, b)   # A 2 x 5 bool matrix.

B = tf.to_int32(b_bool)              # A 2 x 5 int matrix.
示例输出:

In [7]: b = tf.expand_dims(tf.range(5), 0)
        [[0 1 2 3 4]]

In [21]: b_bool = tf.greater(seq_matrix, b)
In [22]: op = sess.run(b_bool)
In [23]: print(op)
[[ True  True False False False]
 [ True  True  True False False]]

In [24]: bint = tf.to_int32(b_bool)
In [25]: op = sess.run(bint)
In [26]: print(op)
[[1 1 0 0 0]
 [1 1 1 0 0]]

这个
@mrry的
解决方案,表达方式有点不同

In [667]: [[2],[3]]>np.arange(5)
Out[667]: 
array([[ True,  True, False, False, False],
       [ True,  True,  True, False, False]], dtype=bool)
In [668]: ([[2],[3]]>np.arange(5)).astype(int)
Out[668]: 
array([[1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0]])
这个想法是在“外部”广播意义上比较[2,3]和[0,1,2,3,4]。结果是布尔值,可以轻松更改为0/1整数

另一种方法是使用(或其他功能):

或以
1开头的变体

In [681]: A=np.ones((2,5))
In [682]: A[range(2),[2,3]]=0
In [683]: A
Out[683]: 
array([[ 1.,  1.,  0.,  1.,  1.],
       [ 1.,  1.,  1.,  0.,  1.]])
In [684]: np.minimum.accumulate(A,axis=1)
Out[684]: 
array([[ 1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  0.,  0.]])

这个
@mrry的
解决方案,表达方式有点不同

In [667]: [[2],[3]]>np.arange(5)
Out[667]: 
array([[ True,  True, False, False, False],
       [ True,  True,  True, False, False]], dtype=bool)
In [668]: ([[2],[3]]>np.arange(5)).astype(int)
Out[668]: 
array([[1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0]])
这个想法是在“外部”广播意义上比较[2,3]和[0,1,2,3,4]。结果是布尔值,可以轻松更改为0/1整数

另一种方法是使用(或其他功能):

或以
1开头的变体

In [681]: A=np.ones((2,5))
In [682]: A[range(2),[2,3]]=0
In [683]: A
Out[683]: 
array([[ 1.,  1.,  0.,  1.,  1.],
       [ 1.,  1.,  1.,  0.,  1.]])
In [684]: np.minimum.accumulate(A,axis=1)
Out[684]: 
array([[ 1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  0.,  0.]])

这是一个使用numpy的伟大解决方案。这是一个使用numpy的伟大解决方案。