Bug in caching: Boolean value of tensor comparison is ambiguous
See original GitHub issueI 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 True
s and False
s 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 True
s and False
s 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
- Create another container for unique hashes in addition to
unique_objects
, e.g.:unique_hashes
. - If hash(obj) not in the new container, add the hash there and add the object to
unique_objects
. - Return
unique_objects
as usual. We can even return list of hashes to further use them later, maybe.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 raiseRuntimeError
, the second -ValueError
. We can suppress this exceptions in method likeobj_in_list
and wait for feedback if they are not sufficient.fetch_unique_objects
withemit_objects
like discussed earlier in this thread@generall
Fixed in #34