Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在三维中索引张量_Python_Indexing_Theano_Deep Learning - Fatal编程技术网

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
元素。如果序列实际上是填充的,那么您可能需要执行更复杂的操作,以避免在填充区域内计算成本元素