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 Nov 8 at 10:48









      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















            Required, but never shown

























            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 Nov 8 at 11:04









                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















                    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

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff