我正在使用Theano的LSTM教程(http://deeplearning.net/tutorial/lstm.html).在lstm.py(http://deeplearning.net/tutorial/code/lstm.py)文件中,我不理解以下行:
c = m_[:, None] * c + (1. - m_)[:, None] * c_
Run Code Online (Sandbox Code Playgroud)
什么m_[:, None]意思?在这种情况下m_,theano矢量c是矩阵.
eic*_*erg 11
这个问题已在Theano邮件列表中得到询问和回答,但实际上是关于numpy索引的基础知识.
以下是问题和答案 https://groups.google.com/forum/#!topic/theano-users/jq92vNtkYUI
为了完整起见,这里有另一种解释:切片None为您的数组添加一个轴,请参阅相关的numpy文档,因为它在numpy和Theano中的行为相同:
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#numpy.newaxis
请注意np.newaxis is None:
import numpy as np
a = np.arange(30).reshape(5, 6)
print a.shape # yields (5, 6)
print a[np.newaxis, :, :].shape # yields (1, 5, 6)
print a[:, np.newaxis, :].shape # yields (5, 1, 6)
print a[:, :, np.newaxis].shape # yields (5, 6, 1)
Run Code Online (Sandbox Code Playgroud)
通常,这用于调整形状以便能够向更高维度广播.例如,可以实现在中轴上平铺7次
b = a[:, np.newaxis] * np.ones((1, 7, 1))
print b.shape # yields (5, 7, 6), 7 copies of a along the second axis
Run Code Online (Sandbox Code Playgroud)