Keras concatenate LSTM model with non-LSTM model












-1















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:



enter image description here



enter image description here



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:



enter image description here










share|improve this question

























  • 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
















-1















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:



enter image description here



enter image description here



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:



enter image description here










share|improve this question

























  • 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














-1












-1








-1








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:



enter image description here



enter image description here



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:



enter image description here










share|improve this question
















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:



enter image description here



enter image description here



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:



enter image description here







python machine-learning keras concatenation lstm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















0














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)





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 7:19









        todaytoday

        10.6k21737




        10.6k21737






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ