简体   繁体   English

使用theano中的向量值索引矩阵

[英]Indexing matrix with values from vector in theano

I'm working on a cost function in theano and faced the following problem: I have a matrix M (say [[3,4],[5,6]] ), a vector v ( [0,1] ) and I would like to get the vector of entries in M corresponding to the indexes contained in v (here [3,6] ). 我正在theano中处理成本函数,并遇到以下问题:我有一个矩阵M(例如[[3,4],[5,6]] ),向量v( [0,1] )和I想要获取M中与v中包含的索引相对应的条目的矢量(此处[3,6] )。 Is there a command to do exactly that? 有命令可以做到这一点吗?

Edit: I ended up doing the following : M[T.arange(v.shape[0]), v], where T.arange(v.shape[0]) is just a vector which entries are integers in the range of the length of v. M needed to be indexed with a list of two vectors 编辑:我结束了以下操作:M [T.arange(v.shape [0]),v],其中T.arange(v.shape [0])只是一个向量,其中的项是以下范围内的整数v。M的长度需要用两个向量的列表来索引

This should work. 这应该工作。

VecOut = [M[i][j] for i,j in enumerate(V)]

enumerate returns indices and vector values to i (row index) and j (value of vector component), which can be used to get corresponding values from matrix. 枚举将索引和向量值返回到i(行索引)和j(向量分量的值),可用于从矩阵中获取相应的值。

example: 例:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM