Python 在三维中索引张量
我有一批长度为Python 在三维中索引张量,python,indexing,theano,deep-learning,Python,Indexing,Theano,Deep Learning,我有一批长度为L的N整数序列,嵌入到N*L*d张量中。这个序列由我的网络架构自动编码。因此,我: from theano import tensor as T X = T.imatrix('X') # N*L elements in [0,C] EMB = T.tensor('Embedding') # N*L*d ... # some code goes here :-) PY = T.tensor('PY') # N*L*C probability of the predicted
L
的N
整数序列,嵌入到N*L*d
张量中。这个序列由我的网络架构自动编码。因此,我:
from theano import tensor as T
X = T.imatrix('X') # N*L elements in [0,C]
EMB = T.tensor('Embedding') # N*L*d
... # some code goes here :-)
PY = T.tensor('PY') # N*L*C probability of the predicted class in [0,C]
cost = -T.log(PY[X])
据我所知,索引是在张量的第一维,所以我不得不使用
theano.scan
。有没有办法直接索引张量?听起来你想要一个三维版本的
如果是这样的话,那么我认为你可以简单地将真实类标签索引矩阵展平为向量,将预测类概率的3D张量展平为矩阵,然后使用内置函数
cost = T.nnet.categorical_crossentropy(
Y.reshape((Y.shape[0] * Y.shape[1], X.shape[2])),
X.flatten())
可能需要首先调整Y
中的条目顺序(例如,通过dimshuffle
),以确保要比较的矩阵和向量中的条目相互对应
这里我们假设,正如问题所暗示的,序列没有填充——它们的长度都是L
元素。如果序列实际上是填充的,那么您可能需要执行更复杂的操作,以避免在填充区域内计算成本元素