저는 예의 신경망을 가진 두 개의 층이 있습니다. 첫 번째 레이어는 두 개의 인수하고 하나의 출력이 있습니다. 두 번째 하나를 수행해야 함을 인수의 결과로는 첫 번째 층을 하나 추가 인수입니다. 그것은 다음과 같습니다:
x1 x2 x3
\ / /
y1 /
\ /
y2
그래서 나는'd 만들어진 모델로는 두 개의 층을 병합하지만 오류를 반환합니다:첫 번째 층에서 순차적으로 모델을 얻어야 한다는"input_shape"또"batch_input_shape"인수입니다.
라인결과입니다.추가(합병)
.
모델:
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'])
You'다시 오류가 발생하기 때문에그 결과
정의순차적()
은 단지 컨테이너를 위한 모델을 정의하지 않는 입력한다.
주어진 당신이 무엇'다시 구축을 위해 노력 설정결과
가 세 번째 입력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'])
그러나 내가 선호하는 방식의 건물이 있는 모델의 이 유형 입력 구조를 사용하는 것은기능 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'])
의 질문에 대답하의 의견이:
1)어떻게는 결과가 및 병합된 연결되어 있습니까? 가정하면 당신 말은 그들은 어떻게 연결됩니다.
연결은 다음과 같이 작동:
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
i.e 행은 그냥 합류했다.
2)이제,x1
을 입력하는 첫째,x2
은 입력으로 두 번째`x3 의 입력으로 세 번째입니다.
실험할 수 있는함께 모델입니다.요약()
(주 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
추가 위 받아들이 대답하는 데 도움이되도록을 사용하는 사람들은tensorflow2.0
``python
가져오기 tensorflow 으로 tf
c1=tf.일정([[1, 1, 1], [2, 2, 2]], dtype=tf.float32) c2=tf.일정([[2, 2, 2], [3, 3, 3]], dtype=tf.float32) c3=tf.일정([[3, 3, 3], [4, 4, 4]], dtype=tf.float32)
x1=tf.keras.레이어입니다.밀도(10)(c1) x2=tf.keras.레이어입니다.밀도(10)(c2) x3=tf.keras.레이어입니다.밀도(10)(c3)
y1=tf.keras.레이어입니다.연결(축=1)([x1,x2])
y2=tf.keras.레이어입니다.연결(축=1)([y1,x3])
인쇄("-"30) 인쇄("x1",x1.양,"x2"x2.양,"x3",x3.형) 인쇄("y1",y1.형) 인쇄("y2",y2.형) 인쇄("-"30) ``
결과:
``