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

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

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

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

0回答

758閲覧

ファンクショナルAPIでのエラー(次元のそろえ方)

tkgtkg

総合スコア1

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

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

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/11/12 14:42

LSTMを使ったモデルを入力時点の重みを出力前の全結合層にて共有する
「重み共有」という手法を使って精度が上がるか試してみたいと考えてコーディング中です。
(ゼロつく2のLSTMに記載されている手法です)

実際に重み共有をkerasを使って試しているのですが、シークエンシャルAPIとは異なり、ファンクショナルAPIを使う所でコーディングに躓いています。
プログラミング入門者のため、至らない点が多くあるかもしれませんが、ご容赦頂ければ幸いです。

python

1from keras.layers import Input, Dense, Dropout 2from keras.layers.core import Dense,Activation 3from keras.layers.recurrent import LSTM 4from keras.callbacks import EarlyStopping 5from keras.layers import Input, Embedding,Dense 6from keras.models import Model,Sequential 7 8#重み共有のためのインスタンスを作成 9shared_Dense=Dense(30,activation='relu') 10#インプット情報 11inputs = Input(shape=((train_x.shape[1],train_x.shape[2],)) 12 13#以下モデル 14x = shared_Dense(inputs) 15x1 = LSTM(128)(x) 16x2 = LSTM(64)(x1) 17x3 = LSTM(32)(x2) 18 19#出力前に重みを共有 20x4 = shared_Dense(30,activation='relu')(x3) 21 22#出力 23output = Dense(10,activation='softmax')(x4) 24 25 26predictions = Dense(3,activation='softmax')(output) 27 28model = Model(inputs=inputs, outputs=predictions) 29 30model.summary()

#発生したエラー

python

1コード 2ValueError Traceback (most recent call last) 3<ipython-input-39-8b340e5170c1> in <module> 4 13 x = shared_Dense(inputs) 5 14 x1 = LSTM(128)(x) 6---> 15 x2 = LSTM(64)(x1) 7 16 x3 = LSTM(32)(x2) 8 17 x4 = shared_Dense(30,activation='relu')(x3) 9 10~\anaconda3\lib\site-packages\keras\layers\recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs) 11 530 12 531 if initial_state is None and constants is None: 13--> 532 return super(RNN, self).__call__(inputs, **kwargs) 14 533 15 534 # If any of `initial_state` or `constants` are specified and are Keras 16 17~\anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs) 18 412 # Raise exceptions in case the input is not compatible 19 413 # with the input_spec specified in the layer constructor. 20--> 414 self.assert_input_compatibility(inputs) 21 415 22 416 # Collect input shapes to build layer. 23 24~\anaconda3\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs) 25 309 self.name + ': expected ndim=' + 26 310 str(spec.ndim) + ', found ndim=' + 27--> 311 str(K.ndim(x))) 28 312 if spec.max_ndim is not None: 29 313 ndim = K.ndim(x) 30 31ValueError: Input 0 is incompatible with layer lstm_17: expected ndim=3, found ndim=2 32 33

#考えた事と試したこと
おそらくValueError: Input 0 is incompatible with layer lstm_17: expected ndim=3, found ndim=2というところから、入力に対して次元があっていないと推測していますが、
他のページを参考にしてもうまく動いてくれません。
例えばinputs以下のような形にして次元を3になるか試したところ…
inputs = Input(shape=((train_x.shape[1],train_x.shape[2],1)))

ValueError:
Input 0 is incompatible with layer lstm_18: expected ndim=3, found ndim=4

となってしまいました。。 expected ndim=3となっているところをexpected ndim=2とすることは出来るのでしょうか。 もしくはexpected ndim=3, found ndim=3とする方法はあるのでしょうか ご存じの方がいらっしゃいましたら何卒よろしくお願い致します。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問