web-dev-qa-db-ja.com

Python

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_

m_[:, None]の意味?この場合、m_はtheanoベクトルであり、cは行列です。

19
nisace

この質問はTheanoメーリングリストで質問され、回答されていますが、実際にはnumpyインデックス作成の基本についてです。

ここに質問と回答があります https://groups.google.com/forum/#!topic/theano-users/jq92vNtkYUI

完全を期すために、もう1つの説明を示します。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)

通常、これは形状を調整してより高い次元にブロードキャストできるようにするために使用されます。例えば。中央の軸で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
10
eickenberg

Theanoベクトルの__getitem__メソッドはタプルを引数として期待します!このような:

class Vect (object):
    def __init__(self,data):
        self.data=list(data)

    def __getitem__(self,key):
        return self.data[key[0]:key[1]+1]

a=Vect('hello')
print a[0,2]

ここに print a[0,2]aが通常のリストの場合、例外が発生します。

>>> a=list('hello')
>>> a[0,2]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: list indices must be integers, not Tuple

しかし、ここで__getitem__メソッドは異なり、タプルを引数として受け入れます。

:に署名__getitem__はこのように:スライスを意味します:

class Vect (object):
    def __init__(self,data):
        self.data=list(data)

    def __getitem__(self,key):
        return self.data[0:key[1]+1]+list(key[0].indices(key[1]))

a=Vect('hello')
print a[:,2]

Noneについて言えば、単純なPythonでインデックスを作成するときにも使用できます。

>>> 'hello'[None:None]
'hello'
4
ForceBru