Tensorflow hub issue: Can't load embedding model after last update - python

This code has been working until last tf_hub update. I think, the problem is in the tensorflow_text module, that I haven't installed. But when I try to execute "pip install tensorflow_text==2.3.0" command (copied from the official tf_hub page) it throws back the error. I also tried to install it manually from github repo, but the package is still not available. Thanks.
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in get_tensor(self, tensor_str)
69 return CheckpointReader.CheckpointReader_GetTensor(
---> 70 self, compat.as_bytes(tensor_str))
71 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the
RuntimeError:
During handling of the above exception, another exception occurred:
OpError Traceback (most recent call last)
<ipython-input-8-c716b77a9bc1> in <module>
----> 1 embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
G:\Anaconda\lib\site-packages\tensorflow_hub\module_v2.py in load(handle, tags, options)
112 module_path, tags=tags, options=options)
113 else:
--> 114 obj = tf_v1.saved_model.load_v2(module_path, tags=tags)
115 obj._is_hub_module_v1 = is_hub_module_v1 # pylint: disable=protected-access
116 return obj
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in load(export_dir, tags, options)
601 ValueError: If `tags` don't match a MetaGraph in the SavedModel.
602 """
--> 603 return load_internal(export_dir, tags, options)
604
605
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in load_internal(export_dir, tags, options, loader_cls)
631 try:
632 loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
--> 633 ckpt_options)
634 except errors.NotFoundError as err:
635 raise FileNotFoundError(
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir, ckpt_options)
129
130 self._load_all()
--> 131 self._restore_checkpoint()
132
133 for node in self._nodes:
G:\Anaconda\lib\site-packages\tensorflow\python\saved_model\load.py in _restore_checkpoint(self)
328 self._checkpoint_options).expect_partial()
329 else:
--> 330 load_status = saver.restore(variables_path, self._checkpoint_options)
331 load_status.assert_existing_objects_matched()
332 checkpoint = load_status._checkpoint
G:\Anaconda\lib\site-packages\tensorflow\python\training\tracking\util.py in restore(self, save_path, options)
1280 dtype_map = reader.get_variable_to_dtype_map()
1281 try:
-> 1282 object_graph_string = reader.get_tensor(base.OBJECT_GRAPH_PROTO_KEY)
1283 except errors_impl.NotFoundError:
1284 # The object graph proto does not exist in this checkpoint. Try the
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in get_tensor(self, tensor_str)
72 # issue with throwing python exceptions from C++.
73 except RuntimeError as e:
---> 74 error_translator(e)
75
76
G:\Anaconda\lib\site-packages\tensorflow\python\training\py_checkpoint_reader.py in error_translator(e)
46 raise errors_impl.InternalError(None, None, error_message)
47 else:
---> 48 raise errors_impl.OpError(None, None, error_message, errors_impl.UNKNOWN)
49
50
OpError:
C:\Users\usr>pip install tensorflow_text v==2.3.0
ERROR: Could not find a version that satisfies the requirement tensorflow_text (from versions: none)
ERROR: No matching distribution found for tensorflow_text

This code is working fine in Tensorflow 2.7 in Anaconda jupyter notebook. Please specify the Tensorflow version you are using while running this code.
!pip install tensorflow-hub
import tensorflow_hub as hub
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
There is no need to install the tensorflow_text to run this piece of code. However I have found there is typo error in your traceback error while installing tensorflow_text.
It should be as below:
!pip install tensorflow_text==2.3.0
Please check this link to access the Universal Sentence Encoder on TF-Hub.

Related

Installing spacy returns 'set_default_tensor_type' error

In a Jupyter Notebook, using Python 3.9.9
I went to https://spacy.io/usage and followed the instructions for installing Spacy
• MacOS/OSX
• conda
• virtual env
• English
• efficiency
!python -m venv .env
my virtual environment's name is firstEnv
!source .env/bin/activate
!conda install -c conda-forge spacy
python -m spacy download en_core_web_sm
I then import spacy
import spacy
spacy version is 3.2.0
Instantiate the class
nlp = spacy.load("en_core_web_sm")
Returns error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/var/folders/dp/5k1wgpbj6d72lnbdgvwv16l40000gn/T/ipykernel_41554/3909579629.py in <module>
----> 1 nlp = spacy.load("en_core_web_sm")
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/__init__.py in load(name, vocab, disable, exclude, config)
49 RETURNS (Language): The loaded nlp object.
50 """
---> 51 return util.load_model(
52 name, vocab=vocab, disable=disable, exclude=exclude, config=config
53 )
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/util.py in load_model(name, vocab, disable, exclude, config)
418 return get_lang_class(name.replace("blank:", ""))()
419 if is_package(name): # installed as package
--> 420 return load_model_from_package(name, **kwargs) # type: ignore[arg-type]
421 if Path(name).exists(): # path to model data directory
422 return load_model_from_path(Path(name), **kwargs) # type: ignore[arg-type]
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/util.py in load_model_from_package(name, vocab, disable, exclude, config)
451 """
452 cls = importlib.import_module(name)
--> 453 return cls.load(vocab=vocab, disable=disable, exclude=exclude, config=config) # type: ignore[attr-defined]
454
455
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/en_core_web_sm/__init__.py in load(**overrides)
8
9 def load(**overrides):
---> 10 return load_model_from_init_py(__file__, **overrides)
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/util.py in load_model_from_init_py(init_file, vocab, disable, exclude, config)
613 if not model_path.exists():
614 raise IOError(Errors.E052.format(path=data_path))
--> 615 return load_model_from_path(
616 data_path,
617 vocab=vocab,
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/util.py in load_model_from_path(model_path, meta, vocab, disable, exclude, config)
486 overrides = dict_to_dot(config)
487 config = load_config(config_path, overrides=overrides)
--> 488 nlp = load_model_from_config(config, vocab=vocab, disable=disable, exclude=exclude)
489 return nlp.from_disk(model_path, exclude=exclude, overrides=overrides)
490
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/util.py in load_model_from_config(config, vocab, disable, exclude, auto_fill, validate)
523 # registry, including custom subclasses provided via entry points
524 lang_cls = get_lang_class(nlp_config["lang"])
--> 525 nlp = lang_cls.from_config(
526 config,
527 vocab=vocab,
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/language.py in from_config(cls, config, vocab, disable, exclude, meta, auto_fill, validate)
1753 # then we would load them twice at runtime: once when we make from config,
1754 # and then again when we load from disk.
-> 1755 nlp = lang_cls(vocab=vocab, create_tokenizer=create_tokenizer, meta=meta)
1756 if after_creation is not None:
1757 nlp = after_creation(nlp)
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/language.py in __init__(self, vocab, max_length, meta, create_tokenizer, batch_size, **kwargs)
174 if vocab is True:
175 vectors_name = meta.get("vectors", {}).get("name")
--> 176 vocab = create_vocab(self.lang, self.Defaults, vectors_name=vectors_name)
177 else:
178 if (self.lang and vocab.lang) and (self.lang != vocab.lang):
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/vocab.pyx in spacy.vocab.create_vocab()
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/vocab.pyx in spacy.vocab.Vocab.__init__()
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/spacy/vectors.pyx in spacy.vectors.Vectors.__init__()
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/thinc/backends/__init__.py in get_current_ops()
124 """Get the current backend object."""
125 if context_ops.get() is None:
--> 126 require_cpu()
127 return cast(Ops, context_ops.get())
128
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/thinc/util.py in require_cpu()
164
165 ops = get_ops("cpu")
--> 166 set_current_ops(ops)
167 set_torch_tensor_type_for_ops(ops)
168
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/thinc/backends/__init__.py in set_current_ops(ops)
132 context_ops.set(ops)
133 _get_thread_state().ops = ops
--> 134 set_torch_tensor_type_for_ops(ops)
135
136
/opt/anaconda3/envs/firstEnv/lib/python3.9/site-packages/thinc/util.py in set_torch_tensor_type_for_ops(ops)
487 torch.set_default_tensor_type("torch.cuda.FloatTensor")
488 else:
--> 489 torch.set_default_tensor_type("torch.FloatTensor")
490 except ImportError:
491 pass
AttributeError: module 'torch' has no attribute 'set_default_tensor_type'
​```
I had to updated pytorch
in your terminal, type:
$! pip install --upgrade torch torchvision

models.vgg16(pretrained=True) due to TqdmKeyError: "Unknown argument(s): {'unit_divisor': 1024}"

im trying to run :
models.vgg16(pretrained=True)
and get pretained model for pytorch but i get as an error this, this error already was asked about before- but answer from there aren't helping me :/
I tried to upgrade tqdm, and twine but with no success.
thanks..
full log:
TqdmKeyError Traceback
(most recent call last)
<ipython-input-13-f93ae0d83650> in <module>
2 from torchvision import transforms, datasets, models
3
----> 4 model = models.vgg16(pretrained=True)
5
6 # n_inputs = model.classifier[6].in_features
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\torchvision\models\vgg.py in vgg16(pretrained, progress, **kwargs)
148 progress (bool): If True, displays a progress bar of the download to stderr
149 """
--> 150 return _vgg('vgg16', 'D', False, pretrained, progress, **kwargs)
151
152
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\torchvision\models\vgg.py in _vgg(arch, cfg, batch_norm, pretrained, progress, **kwargs)
91 if pretrained:
92 state_dict = load_state_dict_from_url(model_urls[arch],
---> 93 progress=progress)
94 model.load_state_dict(state_dict)
95 return model
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\torch\hub.py in load_state_dict_from_url(url, model_dir, map_location, progress, check_hash, file_name)
553 r = HASH_REGEX.search(filename) # r is Optional[Match[str]]
554 hash_prefix = r.group(1) if r else None
--> 555 download_url_to_file(url, cached_file, hash_prefix, progress=progress)
556
557 if _is_legacy_zip_format(cached_file):
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\torch\hub.py in download_url_to_file(url, dst, hash_prefix, progress)
443 sha256 = hashlib.sha256()
444 with tqdm(total=file_size, disable=not progress,
--> 445 unit='B', unit_scale=True, unit_divisor=1024) as pbar:
446 while True:
447 buffer = u.read(8192)
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\tqdm\_tqdm.py in __init__(self, iterable, desc, total, leave, file, ncols, mininterval, maxinterval, miniters, ascii, disable, unit, unit_scale, dynamic_ncols, smoothing, bar_format, initial, position, postfix, gui, **kwargs)
TqdmKeyError: "Unknown argument(s): {'unit_divisor': 1024}"
Reinstalling and upgrading tqdm worked for me!
pip uninstall tqdm
pip install tqdm
What version of tqdm do you have? I upgraded to 4.63.1 and it worked

Getting error "NameError: name 'base' is not defined" while trying to run a Deep Q Network in Google Collab

Does anyone know how to resolve this issue? I'm using Google Collab to try run my code but I cant seem to fix this issue.
Note: I have an Environment setup in Pycharm where this issue doesn't appear and the code runs (just very laggy due to intense training).
Currently running on Tensorflow 2.0 and Python 3.7
NameError Traceback (most recent call last)
<ipython-input-5-14174aa69df8> in <module>()
666
667 # Find Initial State
--> 668 cur_state = env.reset()
669 dqn_agent = DQN(env, cur_state.shape)
670 dqn_agent.save_model('models/deepq.h5')
4 frames
<ipython-input-5-14174aa69df8> in reset(self)
303 self.car = Car(self.world, *self.track[0][1:4])
304
--> 305 return self.step(None)[0]
306
307 def step(self, action):
<ipython-input-5-14174aa69df8> in step(self, action)
319 self.t += 1.0 / FPS
320
--> 321 self.state = self.render("state_pixels")
322
323 step_reward = 0
<ipython-input-5-14174aa69df8> in render(self, mode)
340 assert mode in ['human', 'state_pixels', 'rgb_array']
341 if self.viewer is None:
--> 342 from gym.envs.classic_control import rendering
343 self.viewer = rendering.Viewer(WINDOW_W, WINDOW_H)
344 self.score_label = pyglet.text.Label('0000', font_size=36,
/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/rendering.py in <module>()
25
26 try:
---> 27 from pyglet.gl import *
28 except ImportError as e:
29 raise ImportError('''
/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py in <module>()
223 elif compat_platform == 'darwin':
224 from .cocoa import CocoaConfig as Config
--> 225 del base # noqa: F821
226
227 # XXX remove
NameError: name 'base' is not defined
Link to the code - https://github.com/eoinmca/Final_Year_Project/blob/master/google_collab_version.py
If anyone tries to reproduce this error you need to ensure collab is running tf2 not tf1
Prerequisites to set up collab
!pip install box2d-py
!pip install gym[Box_2D]
print(tf.__version__)
# Run next lines if not on tf2
!pip uninstall tensorflow
!pip install tensorflow==2.0.0

AttributeError: module 'tensorflow' has no attribute 'io'

My problem is when i try to run this code
if log_to_tensorboard: from torch.utils.tensorboard import SummaryWriter
if log_to_tensorboard: writer = SummaryWriter()
I get this error:
(import SummaryWriter works without any problems, but then I try run "writer = SummaryWriter()" and it doesnt work)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-d77d9d09e62b> in <module>
----> 1 writer = SummaryWriter()
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in __init__(self, log_dir, comment, purge_step, max_queue, flush_secs, filename_suffix)
223 # and recreated later as needed.
224 self.file_writer = self.all_writers = None
--> 225 self._get_file_writer()
226
227 # Create default bins for histograms, see generate_testdata.py in tensorflow/tensorboard
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in _get_file_writer(self)
254 if self.all_writers is None or self.file_writer is None:
255 self.file_writer = FileWriter(self.log_dir, self.max_queue,
--> 256 self.flush_secs, self.filename_suffix)
257 self.all_writers = {self.file_writer.get_logdir(): self.file_writer}
258 if self.purge_step is not None:
/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py in __init__(self, log_dir, max_queue, flush_secs, filename_suffix)
64 log_dir = str(log_dir)
65 self.event_writer = EventFileWriter(
---> 66 log_dir, max_queue, flush_secs, filename_suffix)
67
68 def get_logdir(self):
/anaconda3/lib/python3.6/site-packages/tensorboard/summary/writer/event_file_writer.py in __init__(self, logdir, max_queue_size, flush_secs, filename_suffix)
71 """
72 self._logdir = logdir
---> 73 if not tf.io.gfile.exists(logdir):
74 tf.io.gfile.makedirs(logdir)
75 self._file_name = os.path.join(logdir, "events.out.tfevents.%010d.%s.%s.%s" %
/anaconda3/lib/python3.6/site-packages/tensorboard/lazy.py in __getattr__(self, attr_name)
63 class LazyModule(types.ModuleType):
64 def __getattr__(self, attr_name):
---> 65 return getattr(load_once(self), attr_name)
66
67 def __dir__(self):
AttributeError: module 'tensorflow' has no attribute 'io'
How to fix it?
I uninstall and install tensorflow, upgraded tensorboard and torch - that didnt help me
I met the same problem. And my solution is as follow:
Check to see if tensorflow-tensorboard is exists.
pip uninstall tensorflow-tensorboard
install tensorboard==1.14
because an error occurred: TensorBoard logging requires TensorBoard with Python summary writer installed. This should be available in 1.14 or above.
ref: https://github.com/pytorch/pytorch/issues/20140
pip install tensorboard==1.14.0
I was able to solve this problem by using the solution recommended here which suggests to:
pip install tensorflow-io
You can upgrade the version of tensorboard

import of packages does not work - probably version conflict

After a long period where things went OK (I mean here I could pip install packages and then use them in a jupyter notebook with the import statement), I ran into multiple problems recently.
I think this happened when I upgraded my anaconda install from Continuum Analytics (though not sure)
I get error reports for basic imports like pandas or matplotlib like
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-adcb0ce40833> in <module>()
1 # Plot softmax curves
----> 2 import matplotlib.pyplot as plt
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/site- packages/matplotlib/__init__.py in <module>()
1129
1130 # this is the instance used by the matplotlib classes
-> 1131 rcParams = rc_params()
1132
1133 if rcParams['examples.directory']:
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/site-packages/matplotlib/__init__.py in rc_params(fail_on_error)
973 return ret
974
--> 975 return rc_params_from_file(fname, fail_on_error)
976
977
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/site- packages/matplotlib/__init__.py in rc_params_from_file(fname, fail_on_error, use_default_template)
1098 parameters specified in the file. (Useful for updating dicts.)
1099 """
-> 1100 config_from_file = _rc_params_in_file(fname, fail_on_error)
1101
1102 if not use_default_template:
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/site-packages/matplotlib/__init__.py in _rc_params_in_file(fname, fail_on_error)
1016 cnt = 0
1017 rc_temp = {}
-> 1018 with _open_file_or_url(fname) as fd:
1019 try:
1020 for line in fd:
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/contextlib.pyc in __enter__(self)
15 def __enter__(self):
16 try:
---> 17 return self.gen.next()
18 except StopIteration:
19 raise RuntimeError("generator didn't yield")
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/site- packages/matplotlib/__init__.py in _open_file_or_url(fname)
998 else:
999 fname = os.path.expanduser(fname)
-> 1000 encoding = locale.getdefaultlocale()[1]
1001 if encoding is None:
1002 encoding = "utf-8"
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/locale.pyc in getdefaultlocale(envvars)
541 else:
542 localename = 'C'
--> 543 return _parse_localename(localename)
544
545
/Users/peterhirt/anaconda/envs/tensorflow/lib/python2.7/locale.pyc in _parse_localename(localename)
473 elif code == 'C':
474 return None, None
--> 475 raise ValueError, 'unknown locale: %s' % localename
476
477 def _build_localename(localetuple):
ValueError: unknown locale: UTF-8
So, I tried to use a specific environment where I installed first the python version I wanted to use and then added things like tensorflow (which went very well)
I do this by
conda create tensorflow python=2.7
this creates me a environment called tensorflow and install python version 2.7
then I activate the envrionment by
source activate tensorflow
Then, I added matplotlib with
pip install matplotlib
and i get again this error
I am lost. I think it has to do with incompatible packages but I cannot go around this and solve it.
Can someone give me a helping hand please
Thanks in advance
Peter

Categories

Resources