Getting a part of a Keras Model












0















I have a model for an AutoEncoder as follows:



height, width = 28, 28

input_img = Input(shape=(height * width,))
encoding_dim = height * width//256

x = Dense(height * width, activation='relu')(input_img)

encoded1 = Dense(height * width//2, activation='relu')(x)
encoded2 = Dense(height * width//8, activation='relu')(encoded1)

y = Dense(encoding_dim, activation='relu')(encoded2)

decoded2 = Dense(height * width//8, activation='relu')(y)
decoded1 = Dense(height * width//2, activation='relu')(decoded2)

z = Dense(height * width, activation='sigmoid')(decoded1)
autoencoder = Model(input_img, z)

#encoder is the model of the autoencoder slice in the middle
encoder = Model(input_img, y)
decoder = Model(y, z)
print(encoder)
print(decoder)


The encoder part is retrived using the code above, however I can't get the decoder part using the code I added above:



I recieve the following error:



ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_39:0", shape=(?, 784), dtype=float32) at layer "input_39". The following previous layers were accessed without issue: 


Could you please guide me how to get that part?










share|improve this question





























    0















    I have a model for an AutoEncoder as follows:



    height, width = 28, 28

    input_img = Input(shape=(height * width,))
    encoding_dim = height * width//256

    x = Dense(height * width, activation='relu')(input_img)

    encoded1 = Dense(height * width//2, activation='relu')(x)
    encoded2 = Dense(height * width//8, activation='relu')(encoded1)

    y = Dense(encoding_dim, activation='relu')(encoded2)

    decoded2 = Dense(height * width//8, activation='relu')(y)
    decoded1 = Dense(height * width//2, activation='relu')(decoded2)

    z = Dense(height * width, activation='sigmoid')(decoded1)
    autoencoder = Model(input_img, z)

    #encoder is the model of the autoencoder slice in the middle
    encoder = Model(input_img, y)
    decoder = Model(y, z)
    print(encoder)
    print(decoder)


    The encoder part is retrived using the code above, however I can't get the decoder part using the code I added above:



    I recieve the following error:



    ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_39:0", shape=(?, 784), dtype=float32) at layer "input_39". The following previous layers were accessed without issue: 


    Could you please guide me how to get that part?










    share|improve this question



























      0












      0








      0








      I have a model for an AutoEncoder as follows:



      height, width = 28, 28

      input_img = Input(shape=(height * width,))
      encoding_dim = height * width//256

      x = Dense(height * width, activation='relu')(input_img)

      encoded1 = Dense(height * width//2, activation='relu')(x)
      encoded2 = Dense(height * width//8, activation='relu')(encoded1)

      y = Dense(encoding_dim, activation='relu')(encoded2)

      decoded2 = Dense(height * width//8, activation='relu')(y)
      decoded1 = Dense(height * width//2, activation='relu')(decoded2)

      z = Dense(height * width, activation='sigmoid')(decoded1)
      autoencoder = Model(input_img, z)

      #encoder is the model of the autoencoder slice in the middle
      encoder = Model(input_img, y)
      decoder = Model(y, z)
      print(encoder)
      print(decoder)


      The encoder part is retrived using the code above, however I can't get the decoder part using the code I added above:



      I recieve the following error:



      ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_39:0", shape=(?, 784), dtype=float32) at layer "input_39". The following previous layers were accessed without issue: 


      Could you please guide me how to get that part?










      share|improve this question
















      I have a model for an AutoEncoder as follows:



      height, width = 28, 28

      input_img = Input(shape=(height * width,))
      encoding_dim = height * width//256

      x = Dense(height * width, activation='relu')(input_img)

      encoded1 = Dense(height * width//2, activation='relu')(x)
      encoded2 = Dense(height * width//8, activation='relu')(encoded1)

      y = Dense(encoding_dim, activation='relu')(encoded2)

      decoded2 = Dense(height * width//8, activation='relu')(y)
      decoded1 = Dense(height * width//2, activation='relu')(decoded2)

      z = Dense(height * width, activation='sigmoid')(decoded1)
      autoencoder = Model(input_img, z)

      #encoder is the model of the autoencoder slice in the middle
      encoder = Model(input_img, y)
      decoder = Model(y, z)
      print(encoder)
      print(decoder)


      The encoder part is retrived using the code above, however I can't get the decoder part using the code I added above:



      I recieve the following error:



      ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_39:0", shape=(?, 784), dtype=float32) at layer "input_39". The following previous layers were accessed without issue: 


      Could you please guide me how to get that part?







      python tensorflow keras autoencoder






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 6:41









      Milo Lu

      1,60311327




      1,60311327










      asked Nov 19 '18 at 21:33









      AhmadAhmad

      2,77333058




      2,77333058
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The decoder Model needs to have an input layer. For example, the decoder_input here:



          height, width = 28, 28

          # Encoder layers
          input_img = Input(shape=(height * width,))
          encoding_dim = height * width//256

          x = Dense(height * width, activation='relu')(input_img)
          encoded1 = Dense(height * width//2, activation='relu')(x)
          encoded2 = Dense(height * width//8, activation='relu')(encoded1)
          y = Dense(encoding_dim, activation='relu')(encoded2)

          # Decoder layers
          decoder_input = Input(shape=(encoding_dim,))
          decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
          decoded1 = Dense(height * width//2, activation='relu')(decoded2)
          z = Dense(height * width, activation='sigmoid')(decoded1)

          # Build the encoder and decoder models
          encoder = Model(input_img, y)
          decoder = Model(decoder_input, z)

          # Finally, glue encoder and decoder together by feeding the encoder
          # output to the decoder
          autoencoder = Model(input_img, decoder(y))





          share|improve this answer


























          • Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

            – Ahmad
            Nov 20 '18 at 9:58











          • Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

            – Kilian Batzner
            Nov 20 '18 at 10:14











          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%2f53382944%2fgetting-a-part-of-a-keras-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









          1














          The decoder Model needs to have an input layer. For example, the decoder_input here:



          height, width = 28, 28

          # Encoder layers
          input_img = Input(shape=(height * width,))
          encoding_dim = height * width//256

          x = Dense(height * width, activation='relu')(input_img)
          encoded1 = Dense(height * width//2, activation='relu')(x)
          encoded2 = Dense(height * width//8, activation='relu')(encoded1)
          y = Dense(encoding_dim, activation='relu')(encoded2)

          # Decoder layers
          decoder_input = Input(shape=(encoding_dim,))
          decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
          decoded1 = Dense(height * width//2, activation='relu')(decoded2)
          z = Dense(height * width, activation='sigmoid')(decoded1)

          # Build the encoder and decoder models
          encoder = Model(input_img, y)
          decoder = Model(decoder_input, z)

          # Finally, glue encoder and decoder together by feeding the encoder
          # output to the decoder
          autoencoder = Model(input_img, decoder(y))





          share|improve this answer


























          • Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

            – Ahmad
            Nov 20 '18 at 9:58











          • Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

            – Kilian Batzner
            Nov 20 '18 at 10:14
















          1














          The decoder Model needs to have an input layer. For example, the decoder_input here:



          height, width = 28, 28

          # Encoder layers
          input_img = Input(shape=(height * width,))
          encoding_dim = height * width//256

          x = Dense(height * width, activation='relu')(input_img)
          encoded1 = Dense(height * width//2, activation='relu')(x)
          encoded2 = Dense(height * width//8, activation='relu')(encoded1)
          y = Dense(encoding_dim, activation='relu')(encoded2)

          # Decoder layers
          decoder_input = Input(shape=(encoding_dim,))
          decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
          decoded1 = Dense(height * width//2, activation='relu')(decoded2)
          z = Dense(height * width, activation='sigmoid')(decoded1)

          # Build the encoder and decoder models
          encoder = Model(input_img, y)
          decoder = Model(decoder_input, z)

          # Finally, glue encoder and decoder together by feeding the encoder
          # output to the decoder
          autoencoder = Model(input_img, decoder(y))





          share|improve this answer


























          • Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

            – Ahmad
            Nov 20 '18 at 9:58











          • Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

            – Kilian Batzner
            Nov 20 '18 at 10:14














          1












          1








          1







          The decoder Model needs to have an input layer. For example, the decoder_input here:



          height, width = 28, 28

          # Encoder layers
          input_img = Input(shape=(height * width,))
          encoding_dim = height * width//256

          x = Dense(height * width, activation='relu')(input_img)
          encoded1 = Dense(height * width//2, activation='relu')(x)
          encoded2 = Dense(height * width//8, activation='relu')(encoded1)
          y = Dense(encoding_dim, activation='relu')(encoded2)

          # Decoder layers
          decoder_input = Input(shape=(encoding_dim,))
          decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
          decoded1 = Dense(height * width//2, activation='relu')(decoded2)
          z = Dense(height * width, activation='sigmoid')(decoded1)

          # Build the encoder and decoder models
          encoder = Model(input_img, y)
          decoder = Model(decoder_input, z)

          # Finally, glue encoder and decoder together by feeding the encoder
          # output to the decoder
          autoencoder = Model(input_img, decoder(y))





          share|improve this answer















          The decoder Model needs to have an input layer. For example, the decoder_input here:



          height, width = 28, 28

          # Encoder layers
          input_img = Input(shape=(height * width,))
          encoding_dim = height * width//256

          x = Dense(height * width, activation='relu')(input_img)
          encoded1 = Dense(height * width//2, activation='relu')(x)
          encoded2 = Dense(height * width//8, activation='relu')(encoded1)
          y = Dense(encoding_dim, activation='relu')(encoded2)

          # Decoder layers
          decoder_input = Input(shape=(encoding_dim,))
          decoded2 = Dense(height * width//8, activation='relu')(decoder_input)
          decoded1 = Dense(height * width//2, activation='relu')(decoded2)
          z = Dense(height * width, activation='sigmoid')(decoded1)

          # Build the encoder and decoder models
          encoder = Model(input_img, y)
          decoder = Model(decoder_input, z)

          # Finally, glue encoder and decoder together by feeding the encoder
          # output to the decoder
          autoencoder = Model(input_img, decoder(y))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 10:13

























          answered Nov 19 '18 at 22:47









          Kilian BatznerKilian Batzner

          2,42311832




          2,42311832













          • Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

            – Ahmad
            Nov 20 '18 at 9:58











          • Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

            – Kilian Batzner
            Nov 20 '18 at 10:14



















          • Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

            – Ahmad
            Nov 20 '18 at 9:58











          • Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

            – Kilian Batzner
            Nov 20 '18 at 10:14

















          Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

          – Ahmad
          Nov 20 '18 at 9:58





          Thanks, but for the line decoded2 = Dense(...)(decoder_input, it gives the error float() argument must be a string or a number, not 'Dimension'

          – Ahmad
          Nov 20 '18 at 9:58













          Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

          – Kilian Batzner
          Nov 20 '18 at 10:14





          Sorry, I was testing the code with tf.keras instead of keras. I updated my answer, now it should work.

          – Kilian Batzner
          Nov 20 '18 at 10:14


















          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%2f53382944%2fgetting-a-part-of-a-keras-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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith