Keras concatenate LSTM model with non-LSTM model
I have two data. One is time series and the other contains features such as Sex, education, etc. and I want to concatenate output of LSTM model and a dense model. However, I got an error message (look at the end).
This is what the data looks like:
And this is the code:
# PAY_data net
input1 = Input(shape=(6,1))
pay = LSTM(10)(input1)
pay = Dense(10, activation='relu')(pay)
# DEMO_data net
input2 = Input(shape=(5,1))
demo = Dense(10, activation='relu')(input2)
demo = Dense(10, activation='relu')(demo)
merge = concatenate([pay, demo])
hidden1 = Dense(10, activation='relu')(merge)
output = Dense(1, activation='sigmoid')(merge)
model = Model(inputs=[input1, input2], outputs=output)
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= ['accuracy'])
model.fit([PAY_data, DEMO_data], y,nb_epoch=20, batch_size=50, verbose=2, validation_split=0.2)
and this is the error I get:
python machine-learning keras concatenation lstm
add a comment |
I have two data. One is time series and the other contains features such as Sex, education, etc. and I want to concatenate output of LSTM model and a dense model. However, I got an error message (look at the end).
This is what the data looks like:
And this is the code:
# PAY_data net
input1 = Input(shape=(6,1))
pay = LSTM(10)(input1)
pay = Dense(10, activation='relu')(pay)
# DEMO_data net
input2 = Input(shape=(5,1))
demo = Dense(10, activation='relu')(input2)
demo = Dense(10, activation='relu')(demo)
merge = concatenate([pay, demo])
hidden1 = Dense(10, activation='relu')(merge)
output = Dense(1, activation='sigmoid')(merge)
model = Model(inputs=[input1, input2], outputs=output)
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= ['accuracy'])
model.fit([PAY_data, DEMO_data], y,nb_epoch=20, batch_size=50, verbose=2, validation_split=0.2)
and this is the error I get:
python machine-learning keras concatenation lstm
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53
add a comment |
I have two data. One is time series and the other contains features such as Sex, education, etc. and I want to concatenate output of LSTM model and a dense model. However, I got an error message (look at the end).
This is what the data looks like:
And this is the code:
# PAY_data net
input1 = Input(shape=(6,1))
pay = LSTM(10)(input1)
pay = Dense(10, activation='relu')(pay)
# DEMO_data net
input2 = Input(shape=(5,1))
demo = Dense(10, activation='relu')(input2)
demo = Dense(10, activation='relu')(demo)
merge = concatenate([pay, demo])
hidden1 = Dense(10, activation='relu')(merge)
output = Dense(1, activation='sigmoid')(merge)
model = Model(inputs=[input1, input2], outputs=output)
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= ['accuracy'])
model.fit([PAY_data, DEMO_data], y,nb_epoch=20, batch_size=50, verbose=2, validation_split=0.2)
and this is the error I get:
python machine-learning keras concatenation lstm
I have two data. One is time series and the other contains features such as Sex, education, etc. and I want to concatenate output of LSTM model and a dense model. However, I got an error message (look at the end).
This is what the data looks like:
And this is the code:
# PAY_data net
input1 = Input(shape=(6,1))
pay = LSTM(10)(input1)
pay = Dense(10, activation='relu')(pay)
# DEMO_data net
input2 = Input(shape=(5,1))
demo = Dense(10, activation='relu')(input2)
demo = Dense(10, activation='relu')(demo)
merge = concatenate([pay, demo])
hidden1 = Dense(10, activation='relu')(merge)
output = Dense(1, activation='sigmoid')(merge)
model = Model(inputs=[input1, input2], outputs=output)
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics= ['accuracy'])
model.fit([PAY_data, DEMO_data], y,nb_epoch=20, batch_size=50, verbose=2, validation_split=0.2)
and this is the error I get:
python machine-learning keras concatenation lstm
python machine-learning keras concatenation lstm
edited Nov 13 '18 at 7:22
today
10.6k21737
10.6k21737
asked Nov 13 '18 at 5:05
MinJaeMinJae
11
11
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53
add a comment |
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53
add a comment |
1 Answer
1
active
oldest
votes
Since the Dense layer is applied on the last axis of its input data, and considering that you have specified an input shape of (5,1)
for your "Demo_data net", the output shape of this model would be (None, 5, 10)
and therefore it cannot be concatenated with the output of the "Pay_data net" which has an output shape of (None, 10)
. To resolve this, you can remove the redundant last axis from PAY_data
using np.squeeze()
:
PAY_data = np.squeeze(PAY_data)
and also set the input shape accordingly:
input2 = Input(shape=(5,)) # now the input shape is (5,) and not (5,1)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274141%2fkeras-concatenate-lstm-model-with-non-lstm-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since the Dense layer is applied on the last axis of its input data, and considering that you have specified an input shape of (5,1)
for your "Demo_data net", the output shape of this model would be (None, 5, 10)
and therefore it cannot be concatenated with the output of the "Pay_data net" which has an output shape of (None, 10)
. To resolve this, you can remove the redundant last axis from PAY_data
using np.squeeze()
:
PAY_data = np.squeeze(PAY_data)
and also set the input shape accordingly:
input2 = Input(shape=(5,)) # now the input shape is (5,) and not (5,1)
add a comment |
Since the Dense layer is applied on the last axis of its input data, and considering that you have specified an input shape of (5,1)
for your "Demo_data net", the output shape of this model would be (None, 5, 10)
and therefore it cannot be concatenated with the output of the "Pay_data net" which has an output shape of (None, 10)
. To resolve this, you can remove the redundant last axis from PAY_data
using np.squeeze()
:
PAY_data = np.squeeze(PAY_data)
and also set the input shape accordingly:
input2 = Input(shape=(5,)) # now the input shape is (5,) and not (5,1)
add a comment |
Since the Dense layer is applied on the last axis of its input data, and considering that you have specified an input shape of (5,1)
for your "Demo_data net", the output shape of this model would be (None, 5, 10)
and therefore it cannot be concatenated with the output of the "Pay_data net" which has an output shape of (None, 10)
. To resolve this, you can remove the redundant last axis from PAY_data
using np.squeeze()
:
PAY_data = np.squeeze(PAY_data)
and also set the input shape accordingly:
input2 = Input(shape=(5,)) # now the input shape is (5,) and not (5,1)
Since the Dense layer is applied on the last axis of its input data, and considering that you have specified an input shape of (5,1)
for your "Demo_data net", the output shape of this model would be (None, 5, 10)
and therefore it cannot be concatenated with the output of the "Pay_data net" which has an output shape of (None, 10)
. To resolve this, you can remove the redundant last axis from PAY_data
using np.squeeze()
:
PAY_data = np.squeeze(PAY_data)
and also set the input shape accordingly:
input2 = Input(shape=(5,)) # now the input shape is (5,) and not (5,1)
answered Nov 13 '18 at 7:19
todaytoday
10.6k21737
10.6k21737
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274141%2fkeras-concatenate-lstm-model-with-non-lstm-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If the answer resolved your issue, kindly accept it by clicking on the checkmark next to the answer to mark it as "answered" - see What should I do when someone answers my question?
– today
Nov 26 '18 at 15:53