2
votes

I have a very simple code where i want to add embedding but i get error. I want to see the embedding output.

MY code:

input_question_ = Input((query_maxlen,))
embedded_question = Embedding(vocab_size, embedding_dim)(input_question_)

sess = tf.Session()

sess.run(embedded_question, feed_dict={ input_question_: queries_train})

Error :

---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _do_call(self, fn, *args) 1364 try: -> 1365 return fn(*args) 1366 except errors.OpError as e:

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1349 return self._call_tf_sessionrun(options, feed_dict, fetch_list, -> 1350 target_list, run_metadata) 1351

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1442 fetch_list, target_list, -> 1443 run_metadata) 1444

NotFoundError: Container localhost does not exist. (Could not find resource: localhost/embedding_1/embeddings)
   [[{{node embedding_1/embedding_lookup}}]]

During handling of the above exception, another exception occurred:

NotFoundError                             Traceback (most recent call last)
<ipython-input-95-bf218d6ed295> in <module>
     39 sess = tf.Session()
     40 
---> 41 sess.run(embedded_question, feed_dict={ input_question_: queries_train})

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in run(self, fetches, feed_dict, options, run_metadata) 954 try: 955 result = self._run(None, fetches, feed_dict, options_ptr, --> 956 run_metadata_ptr) 957 if run_metadata: 958 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _run(self, handle, fetches, feed_dict, options, run_metadata) 1178 if final_fetches or final_targets or (handle and feed_dict_tensor): 1179 results = self._do_run(handle, final_targets, final_fetches, -> 1180 feed_dict_tensor, options, run_metadata) 1181 else: 1182 results = []

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1357 if handle is None: 1358 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1359 run_metadata) 1360 else: 1361 return self._do_call(_prun_fn, handle, feeds, fetches)

~/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/client/session.py

in _do_call(self, fn, *args) 1382 '\nsession_config.graph_options.rewrite_options.' 1383 'disable_meta_optimizer = True') -> 1384 raise type(e)(node_def, op, message) 1385 1386 def _extend_graph(self):

NotFoundError: Container localhost does not exist. (Could not find resource: localhost/embedding_1/embeddings)
   [[node embedding_1/embedding_lookup (defined at /home/mzaman/miniconda2/envs/py3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1748)

]]

Asking for a solution

1

1 Answers

1
votes

It seems you are missing a reference to the Tensorflow session the model is created with. Try:

import numpy as np
import tensorflow as tf

query_maxlen = 100
vocab_size = 500
embedding_dim = 32
input_question = tf.keras.layers.Input((query_maxlen,))
embedded_question = tf.keras.layers.Embedding(vocab_size, embedding_dim)(input_question)

sess = tf.keras.backend.get_session()

output = sess.run(
    embedded_question, feed_dict={input_question: np.ones((1, query_maxlen))}
)
assert (1, 100, 32) == output.shape
print(output)

Related question: Container localhost does not exist error when using Keras + Flask Blueprints