Facing "the Session graph is empty. Add operations to the graph before calling run()". When running plotly-dash and tensorflow

So guys, I am using tensorflow==1.15.0 and python==3.7.10.
I can’t upgrade to tensorflow==2.X because I am working upon the code which has been written in 1.X version.
So, here is the complete Error:-

Type of the model_obj: <class 'CustomAutoencoder_tf21s.AutoEncoderModel'>
Type of test_norm: <class 'numpy.ndarray'>
The test_norm is
 [[-0.8249858  -1.89925946 -0.19963089 -0.72175069 -0.08146654  0.17114502
  -0.78429501 -0.41983836 -1.16081624 -0.13773452  0.88164535 -0.67126653
  -0.81465053 -0.03487744 -0.17827847 -0.75210082 18.50982367 -1.24800367
  -0.11829605 -0.57853602  0.22019275  0.87136914  0.15625786 -0.0284152
   0.61130833  0.14720818]]
shape of test_norm: (1, 26)
shape of test_norm[0] (26,)
[2021-06-10 01:51:39,464] ERROR in app: Exception on /_dash-update-component [POST]
Traceback (most recent call last):
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflaskapp.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflaskapp.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflaskapp.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflask_compat.py", line 39, in
reraise
    raise value
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflaskapp.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesflaskapp.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesdashdash.py", line 1079, in dispatch
    response.set_data(func(*args, outputs_list=outputs_list))
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagesdashdash.py", line 1010, in add_context
    output_value = func(*args, **kwargs)  # %% callback invoked %%
  File "c:UsersSPARSHDocumentsSiemensInternshipRealWorkXAI_ChillerCaseMyImplementChillerTopAnomaliesLastAttempt.py", line 90, in update_graph
    fig = plot_data_for_top_contributing(MyModel, df_train, df_train_norm,index_to_analyse[-1],-3)
  File "c:UsersSPARSHDocumentsSiemensInternshipRealWorkXAI_ChillerCaseMyImplementChillerApplyingShapley.py", line 645, in plot_data_for_top_contributing
    print('model_obj.model.predict(test_norm):',model_obj.model.predict(test_norm))
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasenginetraining.py", line 908, in predict
    use_multiprocessing=use_multiprocessing)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasenginetraining_arrays.py", line 723, in predict
    callbacks=callbacks)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasenginetraining_arrays.py", line 271, in model_iteration
    model.reset_metrics()
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasenginetraining.py", line 914, in reset_metrics
    m.reset_states()
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasmetrics.py", line 210, in reset_states
    K.batch_set_value([(v, 0) for v in self.variables])
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonkerasbackend.py", line 3259, in batch_set_value
    get_session().run(assign_ops, feed_dict=feed_dict)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonclientsession.py", line 956, in run
    run_metadata_ptr)
  File "C:UsersSPARSHanaconda3envssiemenslibsite-packagestensorflow_corepythonclientsession.py", line 1105, in _run
    raise RuntimeError('The Session graph is empty.  Add operations to the '
RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().
127.0.0.1 - - [10/Jun/2021 01:51:39] "POST /_dash-update-component HTTP/1.1" 500 -
127.0.0.1 - - [10/Jun/2021 01:51:39] "GET /_favicon.ico?v=1.20.0 HTTP/1.1" 200 -

The method which in particular is causing the issue is when I call model_obj.model.predict(test_norm)
But whenever I run a script without it being in the callback of dash, it works fine.

Please help!

Source: Python-3x Questions

LEAVE A COMMENT