Load keras model and cache it in a variable without having to reloadValueError: Tensor 'A' must be from the same graph as Tensor 'B'Load the model and predict with keras and tensorflowTensor is not an element of this graph; deploying Keras modelInstantiate VGG model for once only in Keras when predicting continuously?Concatening Attention layer with decoder input seq2seq model on KerasKeras load model problem using Cannot interpret feed_dict key as Tensor threadingHow to use multiple keras models in a single thread?Got error: Output tensors to a Model must be the output of a Keras `Layer`The initial state or constants of an RNN layer cannot be specified with a mix of Keras tensors and non-Keras tensorsGraph Disconnected when trying to build CNN model with Keras Functional API

New coworker has strange workplace requirements - how should I deal with them?

What is the practical impact of using System.Random which is not cryptographically random?

How to Flip Rotation from Positive to Negative?

Why doesn't Starship have four landing legs?

Understanding data transmission rates over copper wire

Cheap oscilloscope showing 16 MHz square wave

How do I get my neighbour to stop disturbing with loud music?

Does FERPA require parental notification of disability assessment?

Can authors email you PDFs of their textbook for free?

How can I improve my formal definitions?

What's the origin of the concept of alternate dimensions/realities?

IList<T> implementation

I failed to respond to a potential advisor

What caused the end of cybernetic implants?

Quick Tilepaint Puzzles: Corridors and Corners

Is Borg adaptation only temporary?

'Horseshoes' for Deer?

Heuristic argument for the Riemann Hypothesis

Can the inductive kick be discharged without a freewheeling diode, in this example?

Do universities maintain secret textbooks?

Moscow SVO airport, how to avoid scam taxis without pre-booking?

Sum and average calculator

What was Captain Marvel supposed to do once she reached her destination?

Padding a column of lists



Load keras model and cache it in a variable without having to reload


ValueError: Tensor 'A' must be from the same graph as Tensor 'B'Load the model and predict with keras and tensorflowTensor is not an element of this graph; deploying Keras modelInstantiate VGG model for once only in Keras when predicting continuously?Concatening Attention layer with decoder input seq2seq model on KerasKeras load model problem using Cannot interpret feed_dict key as Tensor threadingHow to use multiple keras models in a single thread?Got error: Output tensors to a Model must be the output of a Keras `Layer`The initial state or constants of an RNN layer cannot be specified with a mix of Keras tensors and non-Keras tensorsGraph Disconnected when trying to build CNN model with Keras Functional API






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Loading a model at the beggining of my Flask application and then using it for predictions in my endpoints results in an error



'ValueError: Tensor Tensor("dense/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph.'



model = keras.models.load_model("model.h5")

@app.route("/predict", methods=["POST"])
def predict():
json_data = request.get_json()

variable = preparePredictionInput(
[variable], alphabetDict, maxVariableLength)
prediction = list(model.predict(variable, steps=1, verbose=1)[0])


but loading keras model every time the prediction endpoint is called seems to be working perfectly



@app.route("/predict", methods=["POST"])
def predict():
json_data = request.get_json()
model = keras.models.load_model("model.h5")

variable = preparePredictionInput(
[variable], alphabetDict, maxVariableLength)
prediction = list(model.predict(variable, steps=1, verbose=1)[0])


is there a way to fix this? this quite radically reduces the performance having to reload the model every time.










share|improve this question






























    0















    Loading a model at the beggining of my Flask application and then using it for predictions in my endpoints results in an error



    'ValueError: Tensor Tensor("dense/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph.'



    model = keras.models.load_model("model.h5")

    @app.route("/predict", methods=["POST"])
    def predict():
    json_data = request.get_json()

    variable = preparePredictionInput(
    [variable], alphabetDict, maxVariableLength)
    prediction = list(model.predict(variable, steps=1, verbose=1)[0])


    but loading keras model every time the prediction endpoint is called seems to be working perfectly



    @app.route("/predict", methods=["POST"])
    def predict():
    json_data = request.get_json()
    model = keras.models.load_model("model.h5")

    variable = preparePredictionInput(
    [variable], alphabetDict, maxVariableLength)
    prediction = list(model.predict(variable, steps=1, verbose=1)[0])


    is there a way to fix this? this quite radically reduces the performance having to reload the model every time.










    share|improve this question


























      0












      0








      0








      Loading a model at the beggining of my Flask application and then using it for predictions in my endpoints results in an error



      'ValueError: Tensor Tensor("dense/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph.'



      model = keras.models.load_model("model.h5")

      @app.route("/predict", methods=["POST"])
      def predict():
      json_data = request.get_json()

      variable = preparePredictionInput(
      [variable], alphabetDict, maxVariableLength)
      prediction = list(model.predict(variable, steps=1, verbose=1)[0])


      but loading keras model every time the prediction endpoint is called seems to be working perfectly



      @app.route("/predict", methods=["POST"])
      def predict():
      json_data = request.get_json()
      model = keras.models.load_model("model.h5")

      variable = preparePredictionInput(
      [variable], alphabetDict, maxVariableLength)
      prediction = list(model.predict(variable, steps=1, verbose=1)[0])


      is there a way to fix this? this quite radically reduces the performance having to reload the model every time.










      share|improve this question














      Loading a model at the beggining of my Flask application and then using it for predictions in my endpoints results in an error



      'ValueError: Tensor Tensor("dense/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph.'



      model = keras.models.load_model("model.h5")

      @app.route("/predict", methods=["POST"])
      def predict():
      json_data = request.get_json()

      variable = preparePredictionInput(
      [variable], alphabetDict, maxVariableLength)
      prediction = list(model.predict(variable, steps=1, verbose=1)[0])


      but loading keras model every time the prediction endpoint is called seems to be working perfectly



      @app.route("/predict", methods=["POST"])
      def predict():
      json_data = request.get_json()
      model = keras.models.load_model("model.h5")

      variable = preparePredictionInput(
      [variable], alphabetDict, maxVariableLength)
      prediction = list(model.predict(variable, steps=1, verbose=1)[0])


      is there a way to fix this? this quite radically reduces the performance having to reload the model every time.







      python tensorflow keras






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 13:41









      HigeathHigeath

      361 silver badge6 bronze badges




      361 silver badge6 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0















          Semms like your model variable is not global. Have a look at the below code:



          def init():
          global model
          model = lkeras.models.load_model("model.h5")

          @app.route("/predict", methods=["POST"])
          def predict():
          json_data = request.get_json()
          variable = preparePredictionInput([variable], alphabetDict, maxVariableLength)
          prediction = list(model.predict(variable, steps=1, verbose=1)[0])


          if __name__ == "__main__":
          init()
          app.run()





          share|improve this answer

























          • It is assigned in global context, it is global..

            – Higeath
            Mar 29 at 18:14











          • Did you try the code?

            – stacklikemind
            Mar 30 at 10:48










          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%2f55378692%2fload-keras-model-and-cache-it-in-a-variable-without-having-to-reload%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















          Semms like your model variable is not global. Have a look at the below code:



          def init():
          global model
          model = lkeras.models.load_model("model.h5")

          @app.route("/predict", methods=["POST"])
          def predict():
          json_data = request.get_json()
          variable = preparePredictionInput([variable], alphabetDict, maxVariableLength)
          prediction = list(model.predict(variable, steps=1, verbose=1)[0])


          if __name__ == "__main__":
          init()
          app.run()





          share|improve this answer

























          • It is assigned in global context, it is global..

            – Higeath
            Mar 29 at 18:14











          • Did you try the code?

            – stacklikemind
            Mar 30 at 10:48















          0















          Semms like your model variable is not global. Have a look at the below code:



          def init():
          global model
          model = lkeras.models.load_model("model.h5")

          @app.route("/predict", methods=["POST"])
          def predict():
          json_data = request.get_json()
          variable = preparePredictionInput([variable], alphabetDict, maxVariableLength)
          prediction = list(model.predict(variable, steps=1, verbose=1)[0])


          if __name__ == "__main__":
          init()
          app.run()





          share|improve this answer

























          • It is assigned in global context, it is global..

            – Higeath
            Mar 29 at 18:14











          • Did you try the code?

            – stacklikemind
            Mar 30 at 10:48













          0














          0










          0









          Semms like your model variable is not global. Have a look at the below code:



          def init():
          global model
          model = lkeras.models.load_model("model.h5")

          @app.route("/predict", methods=["POST"])
          def predict():
          json_data = request.get_json()
          variable = preparePredictionInput([variable], alphabetDict, maxVariableLength)
          prediction = list(model.predict(variable, steps=1, verbose=1)[0])


          if __name__ == "__main__":
          init()
          app.run()





          share|improve this answer













          Semms like your model variable is not global. Have a look at the below code:



          def init():
          global model
          model = lkeras.models.load_model("model.h5")

          @app.route("/predict", methods=["POST"])
          def predict():
          json_data = request.get_json()
          variable = preparePredictionInput([variable], alphabetDict, maxVariableLength)
          prediction = list(model.predict(variable, steps=1, verbose=1)[0])


          if __name__ == "__main__":
          init()
          app.run()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 23:32









          stacklikemindstacklikemind

          1661 silver badge10 bronze badges




          1661 silver badge10 bronze badges















          • It is assigned in global context, it is global..

            – Higeath
            Mar 29 at 18:14











          • Did you try the code?

            – stacklikemind
            Mar 30 at 10:48

















          • It is assigned in global context, it is global..

            – Higeath
            Mar 29 at 18:14











          • Did you try the code?

            – stacklikemind
            Mar 30 at 10:48
















          It is assigned in global context, it is global..

          – Higeath
          Mar 29 at 18:14





          It is assigned in global context, it is global..

          – Higeath
          Mar 29 at 18:14













          Did you try the code?

          – stacklikemind
          Mar 30 at 10:48





          Did you try the code?

          – stacklikemind
          Mar 30 at 10:48








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55378692%2fload-keras-model-and-cache-it-in-a-variable-without-having-to-reload%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

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해