前提・実現したいこと
お世話になっております。
1)現在、時系列データの予測を行う深層学習の作成を以下のwebサイトをもとに行っております。
URL:https://www.atmarkit.co.jp/ait/articles/1804/26/news150_2.html
2)このサイトではTensorflowのtf.nn.rnn_cell.BasicRNNCellクラスを使用して深層学習を作成しています。
3)webサイトをもとにしたプログラムを実行していく中でセルが複雑なRNN(LSTM)のモデルを作成できるtf.nn.rnn_cell.BasicLSTMCellクラスを使用してみたくなり、tf.nn.rnn_cell.BasicRNNCellクラスをを元として書き換えています。
4)しかし、tf.nn. rnn_cell.BasicLSTMCellクラスに書き換え、実行しようとした際にエラーメッセージが出力され、どのように対応したらいいかわからず手詰まりになってしまいました。
5)どのようにすればこのエラーを解消し、プログラムを実行できますでしょうか?
厚かましく申し訳ありませんが、アドバイスやご回答いただければ幸いです。
また、質問で不足している事項がありましたら、ご指摘ください。よろしくお願いします。
試したこと
1)tf.nn.rnn_cell.BasicRNNCellクラスを用いた際のcodeは以下の通りです。
python
1cell = tf.nn.rnn_cell.BasicRNNCell(10) 2initial_state = cell.zero_state(tf.shape(x)[0], dtype=tf.float32) 3outputs, last_state = tf.nn.dynamic_rnn(cell, x, initial_state=initial_state, dtype=tf.float32)
2)上記のcodeをもとにtf.nn. rnn_cell.BasicLSTMCellクラスに書き換えたcodeが以下になります。
python
1cell = tf.nn.rnn_cell.BasicLSTMCell(100) 2initial_state_c = tf.placeholder(tf.float32, [None, 100]) 3initial_state_h = tf.placeholder(tf.float32, [None, 100]) 4initial_state = (initial_state_c, initial_state_h) 5outputs, last_state = tf.nn.dynamic_rnn(cell, x, initial_state=initial_state, dtype=tf.float32)
3)2)のプログラムを実行した際に出力されたエラーメッセージが以下になります。
python
1--------------------------------------------------------------------------- 2TypeError Traceback (most recent call last) 3c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\util\nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites) 4 318 _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types, 5--> 319 expand_composites) 6 320 except (ValueError, TypeError) as e: 7 8TypeError: The two structures don't have the same nested structure. 9 10First structure: type=list str=[<tf.Tensor 'rnn/while/Identity:0' shape=() dtype=int32>, (<tf.Tensor 'rnn/while/Identity_1:0' shape=() dtype=int32>, (<tensorflow.python.ops.tensor_array_ops.TensorArray object at 0x00000111B1B9B588>,), (<tf.Tensor 'rnn/while/Identity_3:0' shape=(?, 100) dtype=float32>, <tf.Tensor 'rnn/while/Identity_4:0' shape=(?, 100) dtype=float32>))] 11 12Second structure: type=list str=[<tf.Tensor 'rnn/while/add:0' shape=() dtype=int32>, (<tf.Tensor 'rnn/while/add_1:0' shape=() dtype=int32>, (<tensorflow.python.ops.tensor_array_ops.TensorArray object at 0x00000111B1B04B08>,), LSTMStateTuple(c=<tf.Tensor 'rnn/while/basic_lstm_cell/Add_1:0' shape=(?, 100) dtype=float32>, h=<tf.Tensor 'rnn/while/basic_lstm_cell/Mul_2:0' shape=(?, 100) dtype=float32>))] 13 14More specifically: The two namedtuples don't have the same sequence type. First structure type=tuple str=(<tf.Tensor 'rnn/while/Identity_3:0' shape=(?, 100) dtype=float32>, <tf.Tensor 'rnn/while/Identity_4:0' shape=(?, 100) dtype=float32>) has type tuple, while second structure type=LSTMStateTuple str=LSTMStateTuple(c=<tf.Tensor 'rnn/while/basic_lstm_cell/Add_1:0' shape=(?, 100) dtype=float32>, h=<tf.Tensor 'rnn/while/basic_lstm_cell/Mul_2:0' shape=(?, 100) dtype=float32>) has type LSTMStateTuple 15 16During handling of the above exception, another exception occurred: 17 18TypeError Traceback (most recent call last) 19<ipython-input-10-a3291a61f68f> in <module> 20 4 initial_state_h = tf.placeholder(tf.float32, [None, 100]) #前のcellの出力を参照するのでその初期値 21 5 initial_state = (initial_state_c, initial_state_h) 22----> 6 outputs, last_state = tf.nn.dynamic_rnn(cell, x, initial_state=initial_state, dtype=tf.float32) 23 24c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\util\deprecation.py in new_func(*args, **kwargs) 25 322 'in a future version' if date is None else ('after %s' % date), 26 323 instructions) 27--> 324 return func(*args, **kwargs) 28 325 return tf_decorator.make_decorator( 29 326 func, new_func, 'deprecated', 30 31c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\ops\rnn.py in dynamic_rnn(cell, inputs, sequence_length, initial_state, dtype, parallel_iterations, swap_memory, time_major, scope) 32 705 swap_memory=swap_memory, 33 706 sequence_length=sequence_length, 34--> 707 dtype=dtype) 35 708 36 709 # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth]. 37 38c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\ops\rnn.py in _dynamic_rnn_loop(cell, inputs, initial_state, parallel_iterations, swap_memory, sequence_length, dtype) 39 914 parallel_iterations=parallel_iterations, 40 915 maximum_iterations=time_steps, 41--> 916 swap_memory=swap_memory) 42 917 43 918 # Unpack final output if not using output tuples. 44 45c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations, return_same_structure) 46 2751 ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, loop_context) 47 2752 result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants, 48-> 2753 return_same_structure) 49 2754 if maximum_iterations is not None: 50 2755 return result[1] 51 52c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\ops\control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants, return_same_structure) 53 2243 with ops.get_default_graph()._mutation_lock(): # pylint: disable=protected-access 54 2244 original_body_result, exit_vars = self._BuildLoop( 55-> 2245 pred, body, original_loop_vars, loop_vars, shape_invariants) 56 2246 finally: 57 2247 self.Exit() 58 59c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\ops\control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants) 60 2192 # outputs of the body are typically tuples. 61 2193 nest.assert_same_structure( 62-> 2194 list(packed_vars_for_body), list(body_result), expand_composites=True) 63 2195 64 2196 # Store body_result to keep track of TensorArrays returned by body 65 66c:\PATH\anaconda3\envs\tensor\lib\site-packages\tensorflow_core\python\util\nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites) 67 324 "Entire first structure:\n%s\n" 68 325 "Entire second structure:\n%s" 69--> 326 % (str(e), str1, str2)) 70 327 71 328 72 73TypeError: The two structures don't have the same nested structure. 74 75First structure: type=list str=[<tf.Tensor 'rnn/while/Identity:0' shape=() dtype=int32>, (<tf.Tensor 'rnn/while/Identity_1:0' shape=() dtype=int32>, (<tensorflow.python.ops.tensor_array_ops.TensorArray object at 0x00000111B1B9B588>,), (<tf.Tensor 'rnn/while/Identity_3:0' shape=(?, 100) dtype=float32>, <tf.Tensor 'rnn/while/Identity_4:0' shape=(?, 100) dtype=float32>))] 76 77Second structure: type=list str=[<tf.Tensor 'rnn/while/add:0' shape=() dtype=int32>, (<tf.Tensor 'rnn/while/add_1:0' shape=() dtype=int32>, (<tensorflow.python.ops.tensor_array_ops.TensorArray object at 0x00000111B1B04B08>,), LSTMStateTuple(c=<tf.Tensor 'rnn/while/basic_lstm_cell/Add_1:0' shape=(?, 100) dtype=float32>, h=<tf.Tensor 'rnn/while/basic_lstm_cell/Mul_2:0' shape=(?, 100) dtype=float32>))] 78 79More specifically: The two namedtuples don't have the same sequence type. First structure type=tuple str=(<tf.Tensor 'rnn/while/Identity_3:0' shape=(?, 100) dtype=float32>, <tf.Tensor 'rnn/while/Identity_4:0' shape=(?, 100) dtype=float32>) has type tuple, while second structure type=LSTMStateTuple str=LSTMStateTuple(c=<tf.Tensor 'rnn/while/basic_lstm_cell/Add_1:0' shape=(?, 100) dtype=float32>, h=<tf.Tensor 'rnn/while/basic_lstm_cell/Mul_2:0' shape=(?, 100) dtype=float32>) has type LSTMStateTuple 80Entire first structure: 81[., (., (.,), (., .))] 82Entire second structure: 83[., (., (.,), LSTMStateTuple(c=., h=.))]
補足情報
環境については以下に示す通りです。
・Anaconda3 ・jupyter lab ・python3.7 ・Tensorflow-gpu==1.15
あなたの回答
tips
プレビュー