私は(例はこちらから構成されている:https://github.com/valentin012/conspeech/blob/master/rnn_theano.py)RNNを実現するためにtheanoでスキャンを使用しようとしてきた私がしようとしたものを、今スキャン機能、リカレントニューラルネット
def forward_prop_step(x_t, s_t_prev, U, V, W):
u = T.dot(x_t,U)
s_t = T.tanh(u+T.dot(s_t_prev,W))
o_t = T.nnet.softmax(T.dot(s_t,V))
return [o_t[0], s_t]
Q = np.zeros(self.hidden_dim)
init = theano.shared(Q)
[o,s], updates = theano.scan(
forward_prop_step,
sequences=x,
outputs_info=[None, dict(initial=init)],
non_sequences=[U, V, W],
truncate_gradient=self.bptt_truncate,
strict=False)
されます出力変数が直接的に影響するRNNを実装します(o_{t-1}
およびo_t
は重みでリンクされています)。私はこのように実装しようとしました:
def forward_prop_step(x_t, s_t_prev, o_t_prev, U, V, W, Q):
u = T.dot(x_t,U)
s_t = T.tanh(u+T.dot(s_t_prev,W))
o_t = T.nnet.softmax(T.dot(o_t_prev,Q)+T.dot(s_t,V))
return [o_t[0], s_t, o_t[0]]
R = np.zeros(self.hidden_dim)
init = theano.shared(R)
S = np.zeros(self.word_dim)
init_S = theano.shared(S)
[o,s,op], updates = theano.scan(
forward_prop_step,
sequences=x,
outputs_info=[None, dict(initial=init), dict(initial=init_S)],
non_sequences=[U, V, W, Q],
truncate_gradient=self.bptt_truncate,
strict=False)
しかし、それは動作しませんし、それを修正する方法がわかりません。
エラーメッセージは次のとおりです。
File "theano/scan_module/scan_perform.pyx", line 397, in theano.scan_module.scan_perform.perform (/home/mertens/.theano/compiledir_Linux-3.2--amd64-x86_64-with-debian-7.6--2.7.9-64/scan_perform/mod.cpp:4193) ValueError: Shape mismatch: A.shape[1] != x.shape[0] Apply node that caused the error: CGemv{inplace}(AllocEmpty{dtype='float64'}.0, TensorConstant{1.0}, Q_copy.T, , TensorConstant{0.0}) Toposort index: 10
編集 これは正確なコードです:
word_dim=3
hidden_dim=4
U = np.random.uniform(-np.sqrt(1./word_dim), np.sqrt(1./word_dim), (word_dim,hidden_dim))
V = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (hidden_dim,word_dim))
W = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (hidden_dim, hidden_dim))
Q = np.random.uniform(-np.sqrt(1./word_dim), np.sqrt(1./word_dim), (word_dim, word_dim))
U = theano.shared(name='U', value=U.astype(theano.config.floatX))
V = theano.shared(name='V', value=V.astype(theano.config.floatX))
W = theano.shared(name='W', value=W.astype(theano.config.floatX))
Q = theano.shared(name='Q', value=W.astype(theano.config.floatX))
def forward_prop_step(x_t, o_t_prev, s_t_prev, U, V, W, Q):
u = T.dot(x_t,U)
s_t = T.tanh(u+T.dot(s_t_prev,W))
m = T.dot(o_t_prev,Q)
mm = T.dot(s_t,V)
SSS = mm
o_t = T.nnet.softmax(SSS)
q_t = o_t[0]
return [q_t, s_t, m]
R = np.zeros(self.hidden_dim)
init = theano.shared(R)
S = np.zeros(self.word_dim)
init_S = theano.shared(S)
[o,s,loorky], updates = theano.scan(
forward_prop_step,
sequences=x,
outputs_info=[dict(initial=init_S),dict(initial=init),None],
non_sequences=[U, V, W, Q],
truncate_gradient=self.bptt_truncate,
strict=False)
self.my_forward_propagation = theano.function([x], [o,s,loorky])
aaa = np.zeros((1,3))+1
print self.my_forward_propagation(aaa)
私はreturnステートメント(およびそれに対応loorky
変数プラスから出力m
を省略最後のNone
はoutputs_info
です)すべてが問題ありません。これが含まれている場合、エラーメッセージが表示されます。ValueError:シェイプの不一致:A.shape [1]!= x.shape [0]
に追加する適用される場合、問題はより多くの私はそれを行う方法(o_t_prevの追加の引数を渡す方法を考えていますwith outputs_infoなど)私は実際にスキャン機能を理解していません。 – user329469
output_infoは、スキャン機能の前の入力として使用される初期値を渡すために使用されます。これはあなたがそれを見る方法です。あなたのコードでエラーが発生しました。 'Shape mismatch:A.shape [1]!= x.shape [0]' – Feras
あなたの努力に感謝します。 Q = theano.shared(name = 'Q'、value = Q)によって 'Q = theano.shared(name = 'Q'、value = W.astype(theano.config.floatX) .astype(theano.config.floatX)) ' – user329469