質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

384閲覧

Why does this gradient is invalid?(theano)

uyuutosa

総合スコア13

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/08/09 08:56

編集2017/08/09 09:27

I wonder why bellow code is invalid..

python

1from numpy import * 2import theano.tensor as T 3x = T.dmatrix("x") 4mx = x[...,None,:] 5a = T.ones((1,3)) 6T.grad(mx[...,0].dot(a).sum(), a).eval({x:ones((5,10)).astype(float32)})

bellow error is emerged.

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /home/yu/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs) 883 outputs =\ --> 884 self.fn() if output_subset is None else\ 885 self.fn(output_subset=output_subset) ValueError: Shape mismatch: A.shape[1] != x.shape[0] During handling of the above exception, another exception occurred: ValueError Traceback (most recent call last) <ipython-input-74-52410617594a> in <module>() 3 mx = x[...,None,:] 4 a = T.ones((1,3)) ----> 5 T.grad(mx[...,0].dot(a).sum(), a).eval({x:ones((5,10)).astype(float32)}) /home/yu/anaconda3/lib/python3.5/site-packages/theano/gof/graph.py in eval(self, inputs_to_values) 517 args = [inputs_to_values[param] for param in inputs] 518 --> 519 rval = self._fn_cache[inputs](*args) 520 521 return rval /home/yu/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs) 896 node=self.fn.nodes[self.fn.position_of_error], 897 thunk=thunk, --> 898 storage_map=getattr(self.fn, 'storage_map', None)) 899 else: 900 # old-style linkers raise their own exceptions /home/yu/anaconda3/lib/python3.5/site-packages/theano/gof/link.py in raise_with_op(node, thunk, exc_info, storage_map) 323 # extra long error message in that case. 324 pass --> 325 reraise(exc_type, exc_value, exc_trace) 326 327 /home/yu/anaconda3/lib/python3.5/site-packages/six.py in reraise(tp, value, tb) 683 value = tp() 684 if value.__traceback__ is not tb: --> 685 raise value.with_traceback(tb) 686 raise value 687 /home/yu/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs) 882 try: 883 outputs =\ --> 884 self.fn() if output_subset is None else\ 885 self.fn(output_subset=output_subset) 886 except Exception: ValueError: Shape mismatch: A.shape[1] != x.shape[0] Apply node that caused the error: CGemv{inplace}(AllocEmpty{dtype='float64'}.0, TensorConstant{1.0}, InplaceDimShuffle{1,0}.0, Rebroadcast{0}.0, TensorConstant{0.0}) Toposort index: 7 Inputs types: [TensorType(float64, vector), TensorType(float64, scalar), TensorType(float64, matrix), TensorType(float64, vector), TensorType(float64, scalar)] Inputs shapes: [(3,), (), (3, 5), (1,), ()] Inputs strides: [(8,), (), (8, 24), (80,), ()] Inputs values: [array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324]), array(1.0), 'not shown', array([ 1.]), array(0.0)] Inputs type_num: [12, 12, 12, 12, 12] Outputs clients: [[InplaceDimShuffle{x,0}(CGemv{inplace}.0)]] Debugprint of the apply node: CGemv{inplace} [id A] <TensorType(float64, vector)> '' |AllocEmpty{dtype='float64'} [id B] <TensorType(float64, vector)> '' | |TensorConstant{3} [id C] <TensorType(int64, scalar)> |TensorConstant{1.0} [id D] <TensorType(float64, scalar)> |InplaceDimShuffle{1,0} [id E] <TensorType(float64, matrix)> '' | |Alloc [id F] <TensorType(float64, matrix)> '' | |TensorConstant{(1, 1) of 1.0} [id G] <TensorType(float64, (True, True))> | |Shape_i{0} [id H] <TensorType(int64, scalar)> '' | | |x [id I] <TensorType(float64, matrix)> | |TensorConstant{3} [id C] <TensorType(int64, scalar)> |Rebroadcast{0} [id J] <TensorType(float64, vector)> '' | |Subtensor{int8, ::, int64} [id K] <TensorType(float64, (True,))> '' | |InplaceDimShuffle{0,x,1} [id L] <TensorType(float64, (False, True, False))> '' | | |x [id I] <TensorType(float64, matrix)> | |Constant{0} [id M] <int8> | |Constant{0} [id N] <int64> |TensorConstant{0.0} [id O] <TensorType(float64, scalar)> Storage map footprint: - x, Input, Shape: (5, 10), ElemSize: 8 Byte(s), TotalSize: 400 Byte(s) - InplaceDimShuffle{0,x,1}.0, Shape: (5, 1, 10), ElemSize: 8 Byte(s), TotalSize: 400 Byte(s) - Alloc.0, Shape: (5, 3), ElemSize: 8 Byte(s), TotalSize: 120 Byte(s) - InplaceDimShuffle{1,0}.0, Shape: (3, 5), ElemSize: 8 Byte(s), TotalSize: 120 Byte(s) - AllocEmpty{dtype='float64'}.0, Shape: (3,), ElemSize: 8 Byte(s), TotalSize: 24 Byte(s) - Subtensor{int8, ::, int64}.0, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8 Byte(s) - Shape_i{0}.0, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) - TensorConstant{1.0}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) - TensorConstant{0.0}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) - Constant{0}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) - Rebroadcast{0}.0, Shape: (1,), ElemSize: 8 Byte(s), TotalSize: 8 Byte(s) - TensorConstant{3}, Shape: (), ElemSize: 8 Byte(s), TotalSize: 8.0 Byte(s) - TensorConstant{(1, 1) of 1.0}, Shape: (1, 1), ElemSize: 8 Byte(s), TotalSize: 8 Byte(s) - Constant{0}, Shape: (), ElemSize: 1 Byte(s), TotalSize: 1.0 Byte(s) TotalSize: 593.0 Byte(s) 0.000 GB TotalSize inputs: 441.0 Byte(s) 0.000 GB HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.

I thought above script includes broadcasted operation was wrong,
So no broadcasting used before gradient operation as follows,

python

1x = T.tensor3("x") 2mx = x 3a = T.ones((1,3)) 4T.grad(mx[...,0].dot(a).sum(), a).eval({x:ones((5,1,10)).astype(float32)})

successfully performed and dumped bellow result.

array([[ 5., 5., 5.]], dtype=float32)

But why did the former case invalid?
Is the gradient with broadcasting mathmatically invalid?
Why does shape miss much happen on gradient?

Could you teach me about above question?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/08/10 01:47

teratailの方針でマルチポストは禁止はされていませんが、いくらかルールがあるようなのでそちらもご覧ください。https://teratail.com/help
uyuutosa

2017/08/10 02:54

承知いたしました、ご連絡いただきありがとうございます。
guest

回答1

0

自己解決

local_dimshuffle_subtensorのバグとのことでした。https://groups.google.com/forum/#!topic/theano-users/JKyNcQQJZDU

投稿2017/08/10 02:55

uyuutosa

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問