2つの層を持つニューラルネットワークの例があります。第1層は2つの引数を取り、1つの出力を持ちます。第2層は、第1層の結果として1つの引数を取り、さらに1つの引数を取る必要があります。これは次のようになります。
x1 x2 x3
\ / /
y1 /
\ /
y2
そこで、2つのレイヤーを持つモデルを作成し、それらをマージしようとしましたが、エラーが返されました:result.add(merged)
の行で、The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument.
.
モデルを
first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))
second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))
result = Sequential()
merged = Concatenate([first, second])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.add(merged)
result.compile(optimizer=ada_grad, loss=_loss_tensor, metrics=['accuracy'])
エラーが発生するのは、Sequential()
として定義されたresult
が単なるモデルのコンテナであり、そのための入力を定義していないからです。
あなたが構築しようとしているものを考えると、result
が3番目の入力x3
を取るように設定します。
first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))
second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))
third = Sequential()
# of course you must provide the input to result with will be your x3
third.add(Dense(1, input_shape=(1,), activation='sigmoid'))
# lets say you add a few more layers to first and second.
# concatenate them
merged = Concatenate([first, second])
# then concatenate the two outputs
result = Concatenate([merged, third])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.compile(optimizer=ada_grad, loss='binary_crossentropy',
metrics=['accuracy'])
しかし、このような入力構造を持つモデルを構築する私の望ましい方法は、functional apiを使用することです。
ここでは、あなたの要求を実現するための実装を紹介します。
from keras.models import Model
from keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from keras.optimizers import Adagrad
first_input = Input(shape=(2, ))
first_dense = Dense(1, )(first_input)
second_input = Input(shape=(2, ))
second_dense = Dense(1, )(second_input)
merge_one = concatenate([first_dense, second_dense])
third_input = Input(shape=(1, ))
merge_two = concatenate([merge_one, third_input])
model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
model.compile(optimizer=ada_grad, loss='binary_crossentropy',
metrics=['accuracy'])
コメントの質問に答えるには:。
連結は次のように行われます。
a b c
a b c g h i a b c g h i
d e f j k l d e f j k l
すなわち、行は単に結合されます。
x1
が1番目に,x2
が2番目に,x3
が3番目に入力されます.model.summary()`で実験してみてください(concatenate_XX(連結)のレイヤーサイズに注目)。
# merge samples, two input must be same shape
inp1 = Input(shape=(10,32))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=0) # Merge data must same row column
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()
# merge row must same column size
inp1 = Input(shape=(20,10))
inp2 = Input(shape=(32,10))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()
# merge column must same row size
inp1 = Input(shape=(10,20))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()
詳しくはこちらのノートブックをご覧ください。 https://nbviewer.jupyter.org/github/anhhh11/DeepLearning/blob/master/Concanate_two_layer_keras.ipynb
tensorflow 2.0 "を使用している人に役立つように、上記の受け入れられた回答に追加します。
``python
tfとしてtensorflowを読み込む
c1 = tf.constant([[1, 1, 1], [2, 2, 2]], dtype=tf.float32) c2 = tf.constant([[2, 2, 2], [3, 3, 3]], dtype=tf.float32) c3 = tf.constant([[3, 3, 3], [4, 4, 4]], dtype=tf.float32)
x1 = tf.keras.layers.Dense(10)(c1) x2 = tf.keras.Lays.Dense(10)(c2) x3 = tf.keras.レイヤーズ.Dense(10)(c3)
y1 = tf.keras.layers.Concatenate(axis=1)([x1, x2])
y2 = tf.keras.layers.Concatenate(axis=1)([y1, x3])
print("-"30) print("x1", x1.shape, "x2", x2.shape, "x3", x3.shape) print("y1", y1.shape) print("y2", y2.shape) print("-"30)
結果は以下の通りです。
``text
------------------------------
x1 (2, 10) x2 (2, 10) x3 (2, 10)
y1 (2, 20)
y2 (2, 30)
------------------------------