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

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

新規登録して質問してみよう
ただいま回答率
85.46%
深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

477閲覧

深層学習を作成する際にtf.nn.rnn_cell.BasicRNNCellクラスをtf.nn.rnn_cell.BasicLSTMCellクラスに変更する方法

bam_boo

総合スコア0

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/04/22 01:26

前提・実現したいこと

お世話になっております。

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

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問