Dictionary keys cannot be CuPy element
See original GitHub issueA more answerable question would be, can keys of a dictionary be an element stored in GPU?
- Code to reproduce
import numpy as np
import cupy as cp
arr = [1, 2, 3]
np_dict = dict()
cp_dict = dict()
np_arr = np.asarray(arr)
cp_arr = cp.asarray(arr)
for i in np_arr:
np_dict[i] = "okay"
print(np_dict) # {1: 'okay', 2: 'okay', 3: 'okay'}
for i in cp_arr:
cp_dict[i] = "okay"
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: unhashable type: 'cupy.core.core.ndarray'
- Error messages, stack traces, or logs
Conditions (you can just paste the output of
python -c 'import cupy; cupy.show_config()'
)
CuPy Version : 6.0.0
CUDA Root : /usr/local/cuda
CUDA Build Version : 10000
CUDA Driver Version : 11010
CUDA Runtime Version : 10000
cuDNN Build Version : 7301
cuDNN Version : 7605
NCCL Build Version : 1000
NCCL Runtime Version : (unknown)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Why I can't get dictionary keys by index? - Stack Overflow
So keys is a dictionary itself, but whose key values directly correspond to the "index" at which the keys of d are located....
Read more >Dictionary<TKey,TValue>.Add(TKey, TValue) Method
An element with the same key already exists in the Dictionary<TKey,TValue>. ... A key cannot be null , but a value can be,...
Read more >Dictionary | Apple Developer Documentation
When the dictionary's Key and Value types are neither classes nor @objc protocols, any required bridging of elements occurs at the first access...
Read more >Collections library | Robocorp documentation
First the equality of dictionaries' keys is checked and after that all the key value pairs. If there are differences between the values,...
Read more >Built-in Types — Python 3.11.1 documentation
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don't return a specific item, never...
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 FreeTop 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
Top GitHub Comments
Thanks! 👍🏼
Unlike NumPy, CuPy returns a ndarray when iterating over a ndarray, even if the resulting value is a scalar. This is because the scalar value is on GPU and we don’t want to implicitly transfer it to the host (CPU) memory. You can use ndarray.item() to explicly convert the ndarray (cupy or numpy) into a scalar, e.g.,
cp_dict[i.item()] = "okay"
instead ofcp_dict[i] = "okay"
. https://docs.cupy.dev/en/stable/reference/generated/cupy.ndarray.html#cupy.ndarray.item