mikeizbicki / cmc-csci181-deeplearning

deep learning course materials
15 stars 6 forks source link

AttributeError when running embeddings #46

Open zhh1997zhh opened 4 years ago

zhh1997zhh commented 4 years ago

When I run the following command for part 3: python3 names.py --embed --log_dir=./log

I get an attribute error like this:

Traceback (most recent call last):
  File "names.py", line 731, in <module>
    tag = 'category embedding'
  File "/Users/apple/anaconda3/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py", line 781, in add_embedding
    fs = tf.io.gfile.get_filesystem(save_path)
AttributeError: module 'tensorflow_core._api.v2.io.gfile' has no attribute 'get_filesystem'

My code for part 3 is:

if args.embed:
    print('--------------------------------------------------------------------')
    print('create embedding')
    category_embedding = torch.cat([model.fc_class.weight, torch.unsqueeze(model.fc_class.bias,dim=1)],dim=1)
    if args.log_dir == None:
        raise ValueError('must supply --log_dir to visualize an embedding')
    writer = SummaryWriter(log_dir = args.log_dir)
    writer.add_embedding(
        mat = category_embedding,
        metadata = all_categories,
        tag = 'category embedding'
    )

May I know how should I fix the problem?

keweizhou1999 commented 4 years ago

I had the same error, and I tried to fix it by adding

    import tensorflow as tf
    import tensorboard as tb
    tf.io.gfile = tb.compat.tensorflow_stub.io.gfile

This seems to have run the code without running into any error. However, the projector tab was not shown on tensorboard. There's an error when I ran the tensorboard command tensorboard --logdir=./log.

Below is my error message.

Exception in thread ProjectorPluginIsActiveThread:
Traceback (most recent call last):
  File "/Users/keweizhou/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/Users/keweizhou/anaconda3/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 326, in _determine_is_active
    if self.configs:
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 344, in configs
    self._augment_configs_with_checkpoint_info()
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 366, in _augment_configs_with_checkpoint_info
    tensor = _read_tensor_tsv_file(fpath)
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorboard/plugins/projector/projector_plugin.py", line 153, in _read_tensor_tsv_file
    for line in f:
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/lib/io/file_io.py", line 220, in __next__
    return self.next()
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/lib/io/file_io.py", line 214, in next
    retval = self.readline()
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/lib/io/file_io.py", line 178, in readline
    self._preread_check()
  File "/Users/keweizhou/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/lib/io/file_io.py", line 84, in _preread_check
    compat.as_bytes(self.__name), 1024 * 512)
tensorflow.python.framework.errors_impl.NotFoundError: ./log/00000/category embedding/tensors.tsv; No such file or directory

When I checked my category embedding folder, there's only one file named metadata.tsv

n8stringham commented 4 years ago

@zhh1997zhh I think you need to add the --warm_start flag to your initial command so that you have a model from which to extract the embedding.

mikeizbicki commented 4 years ago

@zhh1997zhh and @keweizhou1999 This error is caused by having incompatible versions of tensorboard and pytorch. I'm not sure if the fix proposed by @keweizhou1999 will work correctly, or if it just silently masks the error. See #40 for the versions of all libraries that I used.

zhh1997zhh commented 4 years ago

Thank you! Both suggestions are helpful!