Copying weights of a specific layer - keras



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















According to this the following copies weights from one model to another:



target_model.set_weights(model.get_weights())


What about copying the weights of a specific layer, would this work?



model_1.layers[0].set_weights(source_model.layers[0].get_weights())
model_2.layers[0].set_weights(source_model.layers[0].get_weights())


If I train model_1 and model_2 will they have separate weights? The documentation doesn't state whether if this get_weights makes a deep copy or not. If this doesn't work, how can this be achieved?










share|improve this question






























    2















    According to this the following copies weights from one model to another:



    target_model.set_weights(model.get_weights())


    What about copying the weights of a specific layer, would this work?



    model_1.layers[0].set_weights(source_model.layers[0].get_weights())
    model_2.layers[0].set_weights(source_model.layers[0].get_weights())


    If I train model_1 and model_2 will they have separate weights? The documentation doesn't state whether if this get_weights makes a deep copy or not. If this doesn't work, how can this be achieved?










    share|improve this question


























      2












      2








      2








      According to this the following copies weights from one model to another:



      target_model.set_weights(model.get_weights())


      What about copying the weights of a specific layer, would this work?



      model_1.layers[0].set_weights(source_model.layers[0].get_weights())
      model_2.layers[0].set_weights(source_model.layers[0].get_weights())


      If I train model_1 and model_2 will they have separate weights? The documentation doesn't state whether if this get_weights makes a deep copy or not. If this doesn't work, how can this be achieved?










      share|improve this question
















      According to this the following copies weights from one model to another:



      target_model.set_weights(model.get_weights())


      What about copying the weights of a specific layer, would this work?



      model_1.layers[0].set_weights(source_model.layers[0].get_weights())
      model_2.layers[0].set_weights(source_model.layers[0].get_weights())


      If I train model_1 and model_2 will they have separate weights? The documentation doesn't state whether if this get_weights makes a deep copy or not. If this doesn't work, how can this be achieved?







      python keras neural-network deep-learning keras-layer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 7:39









      today

      11.5k22239




      11.5k22239










      asked Nov 15 '18 at 7:12









      bones.felipebones.felipe

      317313




      317313






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:



          model1 = Sequential()
          model1.add(Dense(10, input_dim=2))

          model2 = Sequential()
          model2.add(Dense(10, input_dim=2))

          model1.compile(loss='mse', optimizer='adam')
          model2.compile(loss='mse', optimizer='adam')


          Test:



          >>> model1.layers[0].get_weights()
          [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
          0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
          [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
          0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
          dtype=float32),
          array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

          >>> model2.layers[0].get_weights()
          [array([[-0.30062824, -0.3740575 , -0.3502644 , 0.28050178, -0.68631136,
          0.1596322 , 0.08288956, -0.20988202, 0.34323698, 0.2893324 ],
          [-0.29182747, -0.2754455 , -0.64082885, 0.29160154, 0.04342002,
          -0.4996035 , 0.6608283 , 0.10293472, 0.11375248, -0.43438092]],
          dtype=float32),
          array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

          >>> model2.layers[0].set_weights(model1.layers[0].get_weights())
          >>> model2.layers[0].get_weights()
          [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
          0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
          [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
          0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
          dtype=float32),
          array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

          >>> id(model1.layers[0].get_weights()[0])
          140494823634144

          >>> id(model2.layers[0].get_weights()[0])
          140494823635664


          The ids of kernel weights arrays are different so they are different objects, but with the same value.






          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%2f53314152%2fcopying-weights-of-a-specific-layer-keras%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









            1














            Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:



            model1 = Sequential()
            model1.add(Dense(10, input_dim=2))

            model2 = Sequential()
            model2.add(Dense(10, input_dim=2))

            model1.compile(loss='mse', optimizer='adam')
            model2.compile(loss='mse', optimizer='adam')


            Test:



            >>> model1.layers[0].get_weights()
            [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
            0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
            [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
            0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
            dtype=float32),
            array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

            >>> model2.layers[0].get_weights()
            [array([[-0.30062824, -0.3740575 , -0.3502644 , 0.28050178, -0.68631136,
            0.1596322 , 0.08288956, -0.20988202, 0.34323698, 0.2893324 ],
            [-0.29182747, -0.2754455 , -0.64082885, 0.29160154, 0.04342002,
            -0.4996035 , 0.6608283 , 0.10293472, 0.11375248, -0.43438092]],
            dtype=float32),
            array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

            >>> model2.layers[0].set_weights(model1.layers[0].get_weights())
            >>> model2.layers[0].get_weights()
            [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
            0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
            [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
            0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
            dtype=float32),
            array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

            >>> id(model1.layers[0].get_weights()[0])
            140494823634144

            >>> id(model2.layers[0].get_weights()[0])
            140494823635664


            The ids of kernel weights arrays are different so they are different objects, but with the same value.






            share|improve this answer





























              1














              Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:



              model1 = Sequential()
              model1.add(Dense(10, input_dim=2))

              model2 = Sequential()
              model2.add(Dense(10, input_dim=2))

              model1.compile(loss='mse', optimizer='adam')
              model2.compile(loss='mse', optimizer='adam')


              Test:



              >>> model1.layers[0].get_weights()
              [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
              0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
              [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
              0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
              dtype=float32),
              array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

              >>> model2.layers[0].get_weights()
              [array([[-0.30062824, -0.3740575 , -0.3502644 , 0.28050178, -0.68631136,
              0.1596322 , 0.08288956, -0.20988202, 0.34323698, 0.2893324 ],
              [-0.29182747, -0.2754455 , -0.64082885, 0.29160154, 0.04342002,
              -0.4996035 , 0.6608283 , 0.10293472, 0.11375248, -0.43438092]],
              dtype=float32),
              array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

              >>> model2.layers[0].set_weights(model1.layers[0].get_weights())
              >>> model2.layers[0].get_weights()
              [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
              0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
              [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
              0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
              dtype=float32),
              array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

              >>> id(model1.layers[0].get_weights()[0])
              140494823634144

              >>> id(model2.layers[0].get_weights()[0])
              140494823635664


              The ids of kernel weights arrays are different so they are different objects, but with the same value.






              share|improve this answer



























                1












                1








                1







                Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:



                model1 = Sequential()
                model1.add(Dense(10, input_dim=2))

                model2 = Sequential()
                model2.add(Dense(10, input_dim=2))

                model1.compile(loss='mse', optimizer='adam')
                model2.compile(loss='mse', optimizer='adam')


                Test:



                >>> model1.layers[0].get_weights()
                [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
                0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
                [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
                0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> model2.layers[0].get_weights()
                [array([[-0.30062824, -0.3740575 , -0.3502644 , 0.28050178, -0.68631136,
                0.1596322 , 0.08288956, -0.20988202, 0.34323698, 0.2893324 ],
                [-0.29182747, -0.2754455 , -0.64082885, 0.29160154, 0.04342002,
                -0.4996035 , 0.6608283 , 0.10293472, 0.11375248, -0.43438092]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> model2.layers[0].set_weights(model1.layers[0].get_weights())
                >>> model2.layers[0].get_weights()
                [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
                0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
                [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
                0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> id(model1.layers[0].get_weights()[0])
                140494823634144

                >>> id(model2.layers[0].get_weights()[0])
                140494823635664


                The ids of kernel weights arrays are different so they are different objects, but with the same value.






                share|improve this answer















                Of course, it would be a copy of the weights. It does not make sense the weights object to be shared between two separate models. You can check it for yourself with a simple example like this:



                model1 = Sequential()
                model1.add(Dense(10, input_dim=2))

                model2 = Sequential()
                model2.add(Dense(10, input_dim=2))

                model1.compile(loss='mse', optimizer='adam')
                model2.compile(loss='mse', optimizer='adam')


                Test:



                >>> model1.layers[0].get_weights()
                [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
                0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
                [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
                0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> model2.layers[0].get_weights()
                [array([[-0.30062824, -0.3740575 , -0.3502644 , 0.28050178, -0.68631136,
                0.1596322 , 0.08288956, -0.20988202, 0.34323698, 0.2893324 ],
                [-0.29182747, -0.2754455 , -0.64082885, 0.29160154, 0.04342002,
                -0.4996035 , 0.6608283 , 0.10293472, 0.11375248, -0.43438092]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> model2.layers[0].set_weights(model1.layers[0].get_weights())
                >>> model2.layers[0].get_weights()
                [array([[-0.42853734, 0.18648076, -0.47137827, 0.1792168 , 0.0373047 ,
                0.2765705 , 0.38383502, 0.09664273, -0.4971757 , 0.41548246],
                [ 0.0403192 , -0.01309097, 0.6656211 , -0.0536288 , 0.58677703,
                0.21625364, 0.26447064, -0.42619988, 0.17218047, -0.39748642]],
                dtype=float32),
                array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

                >>> id(model1.layers[0].get_weights()[0])
                140494823634144

                >>> id(model2.layers[0].get_weights()[0])
                140494823635664


                The ids of kernel weights arrays are different so they are different objects, but with the same value.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 6:49

























                answered Nov 15 '18 at 7:38









                todaytoday

                11.5k22239




                11.5k22239





























                    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%2f53314152%2fcopying-weights-of-a-specific-layer-keras%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo