Merging keras models before compile or fit?









up vote
0
down vote

favorite












working in Keras 2.2.4



I have three data features (feature1, feature2, feature3) that correspond to a set of users and how they rank certain items. At present, I have modelled them all separately using a model architecture as follows:



input_vecs = add([feature1_vec, user_vec])
nn = Dropout(0.5)(Dense(128, activation='relu')(input_vecs))
nn = BatchNormalization()(nn)
nn = Dropout(0.5)(Dense(128, activation='relu')(nn))
nn = BatchNormalization()(nn)
nn = Dense(128, activation='relu')(nn)
feature1result = Dense(9, activation='softmax')(nn)

feature1model = Model([feature1_input, user_input], feature1result)
featuremodel.compile('adam', 'categorical_crossentropy')


Each of the models have similar architectures (each separately tuned), and the same style/shape of outputs. I would like to take the preliminary result of the three models and put those into a new layer and then create a final result.



I guess I could run the three models separately, take their output, and then put that output into an entirely new (likely Sequential) model using a structure like this:



model1 = load_model("feature1.h5")
model2 = load_model("feature2.h5")
model3 = load_model("feature3.h5")
merged_model = Sequential()
merged_model.add(merge([model1.layers[-1].output,model2.layers[-1].output,model3.layers[-1].output]))
merged_model.add(Dense(units = 9, activation='relu')) #or whatever units
merged_model.add(Dense(units = 12, activation='relu'))#or whatever units
merged_model.add(Dense(9, activation='softmax'))


However, I'd like to merge them beforehand as it will make managing the models easier later on (i.e., only updating one large model instead of updating 4 smaller models).



How can I do this before doing the compile() and fit() steps?










share|improve this question

























    up vote
    0
    down vote

    favorite












    working in Keras 2.2.4



    I have three data features (feature1, feature2, feature3) that correspond to a set of users and how they rank certain items. At present, I have modelled them all separately using a model architecture as follows:



    input_vecs = add([feature1_vec, user_vec])
    nn = Dropout(0.5)(Dense(128, activation='relu')(input_vecs))
    nn = BatchNormalization()(nn)
    nn = Dropout(0.5)(Dense(128, activation='relu')(nn))
    nn = BatchNormalization()(nn)
    nn = Dense(128, activation='relu')(nn)
    feature1result = Dense(9, activation='softmax')(nn)

    feature1model = Model([feature1_input, user_input], feature1result)
    featuremodel.compile('adam', 'categorical_crossentropy')


    Each of the models have similar architectures (each separately tuned), and the same style/shape of outputs. I would like to take the preliminary result of the three models and put those into a new layer and then create a final result.



    I guess I could run the three models separately, take their output, and then put that output into an entirely new (likely Sequential) model using a structure like this:



    model1 = load_model("feature1.h5")
    model2 = load_model("feature2.h5")
    model3 = load_model("feature3.h5")
    merged_model = Sequential()
    merged_model.add(merge([model1.layers[-1].output,model2.layers[-1].output,model3.layers[-1].output]))
    merged_model.add(Dense(units = 9, activation='relu')) #or whatever units
    merged_model.add(Dense(units = 12, activation='relu'))#or whatever units
    merged_model.add(Dense(9, activation='softmax'))


    However, I'd like to merge them beforehand as it will make managing the models easier later on (i.e., only updating one large model instead of updating 4 smaller models).



    How can I do this before doing the compile() and fit() steps?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      working in Keras 2.2.4



      I have three data features (feature1, feature2, feature3) that correspond to a set of users and how they rank certain items. At present, I have modelled them all separately using a model architecture as follows:



      input_vecs = add([feature1_vec, user_vec])
      nn = Dropout(0.5)(Dense(128, activation='relu')(input_vecs))
      nn = BatchNormalization()(nn)
      nn = Dropout(0.5)(Dense(128, activation='relu')(nn))
      nn = BatchNormalization()(nn)
      nn = Dense(128, activation='relu')(nn)
      feature1result = Dense(9, activation='softmax')(nn)

      feature1model = Model([feature1_input, user_input], feature1result)
      featuremodel.compile('adam', 'categorical_crossentropy')


      Each of the models have similar architectures (each separately tuned), and the same style/shape of outputs. I would like to take the preliminary result of the three models and put those into a new layer and then create a final result.



      I guess I could run the three models separately, take their output, and then put that output into an entirely new (likely Sequential) model using a structure like this:



      model1 = load_model("feature1.h5")
      model2 = load_model("feature2.h5")
      model3 = load_model("feature3.h5")
      merged_model = Sequential()
      merged_model.add(merge([model1.layers[-1].output,model2.layers[-1].output,model3.layers[-1].output]))
      merged_model.add(Dense(units = 9, activation='relu')) #or whatever units
      merged_model.add(Dense(units = 12, activation='relu'))#or whatever units
      merged_model.add(Dense(9, activation='softmax'))


      However, I'd like to merge them beforehand as it will make managing the models easier later on (i.e., only updating one large model instead of updating 4 smaller models).



      How can I do this before doing the compile() and fit() steps?










      share|improve this question













      working in Keras 2.2.4



      I have three data features (feature1, feature2, feature3) that correspond to a set of users and how they rank certain items. At present, I have modelled them all separately using a model architecture as follows:



      input_vecs = add([feature1_vec, user_vec])
      nn = Dropout(0.5)(Dense(128, activation='relu')(input_vecs))
      nn = BatchNormalization()(nn)
      nn = Dropout(0.5)(Dense(128, activation='relu')(nn))
      nn = BatchNormalization()(nn)
      nn = Dense(128, activation='relu')(nn)
      feature1result = Dense(9, activation='softmax')(nn)

      feature1model = Model([feature1_input, user_input], feature1result)
      featuremodel.compile('adam', 'categorical_crossentropy')


      Each of the models have similar architectures (each separately tuned), and the same style/shape of outputs. I would like to take the preliminary result of the three models and put those into a new layer and then create a final result.



      I guess I could run the three models separately, take their output, and then put that output into an entirely new (likely Sequential) model using a structure like this:



      model1 = load_model("feature1.h5")
      model2 = load_model("feature2.h5")
      model3 = load_model("feature3.h5")
      merged_model = Sequential()
      merged_model.add(merge([model1.layers[-1].output,model2.layers[-1].output,model3.layers[-1].output]))
      merged_model.add(Dense(units = 9, activation='relu')) #or whatever units
      merged_model.add(Dense(units = 12, activation='relu'))#or whatever units
      merged_model.add(Dense(9, activation='softmax'))


      However, I'd like to merge them beforehand as it will make managing the models easier later on (i.e., only updating one large model instead of updating 4 smaller models).



      How can I do this before doing the compile() and fit() steps?







      python machine-learning keras






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      user1563247

      9710




      9710






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You need to use the functional API to use the merge layer. I hope this gives you the idea:



          input = Input( ... ) 
          model1 = load_model("feature1.h5")
          model2 = load_model("feature2.h5")
          model3 = load_model("feature3.h5")

          m1 = model1(input)
          m2 = model2(input)
          m3 = model3(input)
          merged_model = merge([m1,m2,m3])
          ...





          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',
            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%2f53206147%2fmerging-keras-models-before-compile-or-fit%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You need to use the functional API to use the merge layer. I hope this gives you the idea:



            input = Input( ... ) 
            model1 = load_model("feature1.h5")
            model2 = load_model("feature2.h5")
            model3 = load_model("feature3.h5")

            m1 = model1(input)
            m2 = model2(input)
            m3 = model3(input)
            merged_model = merge([m1,m2,m3])
            ...





            share|improve this answer
























              up vote
              1
              down vote



              accepted










              You need to use the functional API to use the merge layer. I hope this gives you the idea:



              input = Input( ... ) 
              model1 = load_model("feature1.h5")
              model2 = load_model("feature2.h5")
              model3 = load_model("feature3.h5")

              m1 = model1(input)
              m2 = model2(input)
              m3 = model3(input)
              merged_model = merge([m1,m2,m3])
              ...





              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You need to use the functional API to use the merge layer. I hope this gives you the idea:



                input = Input( ... ) 
                model1 = load_model("feature1.h5")
                model2 = load_model("feature2.h5")
                model3 = load_model("feature3.h5")

                m1 = model1(input)
                m2 = model2(input)
                m3 = model3(input)
                merged_model = merge([m1,m2,m3])
                ...





                share|improve this answer












                You need to use the functional API to use the merge layer. I hope this gives you the idea:



                input = Input( ... ) 
                model1 = load_model("feature1.h5")
                model2 = load_model("feature2.h5")
                model3 = load_model("feature3.h5")

                m1 = model1(input)
                m2 = model2(input)
                m3 = model3(input)
                merged_model = merge([m1,m2,m3])
                ...






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Daniel GL

                698316




                698316



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206147%2fmerging-keras-models-before-compile-or-fit%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Edmonton

                    Crossroads (UK TV series)