question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in caching: Boolean value of tensor comparison is ambiguous

See original GitHub issue

I think this is an edge case. when we do if sample.obj not in unique_objects in fetch_unique_objects of data loader classes, it may return a tensor of Trues and Falses if the individual elements in the tensor (sample.obj) are equal in certain indexes and not equal in others. When we want to use that returned tensor in a conditional expression, it tries to reduce it to a single boolean value, but the combination of multiple Trues and Falses is ambiguous, so it throws a runtime error:

...
  File "C:\Users\Yusuf\coding\quaterion\quaterion\dataset\cache_data_loader.py", line 68, in cache_collate_fn
    unique_objects = self.unique_objects_extractor(batch)
  File "C:\Users\Yusuf\coding\quaterion\quaterion\dataset\similarity_data_loader.py", line 42, in fetch_unique_objects
    if sample.obj_b not in unique_objects:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Predicting:   0%|          | 0/387 [00:00<?, ?it/s]

Possible solution

  1. Create another container for unique hashes in addition to unique_objects, e.g.: unique_hashes.
  2. If hash(obj) not in the new container, add the hash there and add the object to unique_objects.
  3. Return unique_objects as usual. We can even return list of hashes to further use them later, maybe.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
joeincommented, Feb 12, 2022

We discussed and here are to possible solutions we figured out:

  • torch.Tensors, np.ndarray and everything built upon them doomed to be broken (e.g. pandas objects), the first ones will raise RuntimeError, the second - ValueError. We can suppress this exceptions in method like obj_in_list and wait for feedback if they are not sufficient.
  • replace fetch_unique_objects with emit_objects like discussed earlier in this thread

@generall

0reactions
monatiscommented, Feb 22, 2022

Fixed in #34

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bool value of Tensor with more than one value is ambiguous ...
the constructor will at some point try to use the passed tensor Train["Label"] as a boolean, which throws the mentioned error message. Share....
Read more >
`if not something` is ambiguous · Issue #19136 - GitHub
My required_input is a tensor (in jax), sometimes I get error ValueError: The truth value of an array with more than one element...
Read more >
Bool value of Tensor with more than one ... - PyTorch Forums
This error is raised, if you are trying to compare more than a single value without calling all() or any() on the result....
Read more >
runtimeerror: boolean value of tensor with more than one ...
RuntimeError: bool value of Tensor with more than one value is ambiguous Usually it is the wrong use of Loss, for example, the...
Read more >
PyTorch 1.10.0 Now Available - Exxact Corporation
conj() is now an O(1) operation and returns a tensor that views the same memory as tensor and has conjugate bit set. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found