-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: wrong dtype computing with 0-D teensor #22823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
you could use (z**2).astype("float32"). import numpy as np
x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
z = np.dot(x, y)
print(z.ndim) # 0
print(z.shape) # ()
print(z.dtype) # float32
print(((z**2).astype("float32")).dtype) #float32 |
thanks, but it still remains a type BUG for numpy😥 |
You're not working with a "0-D teensor", you're working with a scalar. If you cast to a 0d array, then things work as you expected: import numpy as np
x = np.array([0.04399043, -0.26885903])
y = np.array([-0.99222845, -0.1244299])
x = x.astype("float32")
y = y.astype("float32")
z = np.dot(x, y)[...]
print(z.ndim) # 0
print(z.shape) # ()
print(z.dtype) # float32
print((z**2).dtype) # float32 |
I'm curious about why 0-D tensor is not considered scalar in numpy? |
NumPy is unfortunately very unclear and not logical about the definition of what it considers a "NumPy scalar" and a 0-D "scalar array". In general, your observation is unfortunately expected. The problem is that NumPy tries to be smart about scalars upcasting and arrays not, but because NumPy doesn't distinguish between 0-D arrays and Python scalars this all fails. NEP 50 is the attempt to eventually get away from that, you can even try it (requires a new NumPy version):
In general, the observation is in line with gh-10322, so going to close. But happy to follow-up! (I admit, the fact that Eric's solution works is independent of that, it uses the "array" logic, even though the array is 0-D, which is not typical) |
I was a bit surprised that my solution worked, I came to this issue expecting it to be about that exact spelling not working! |
Uh oh!
There was an error while loading. Please reload this page.
Describe the issue:
I guess result dtype should be same as input dtype, but code below is not.
and when
z
is reshaped to[1]
, dtype is consistent with input as expectedReproduce the code example:
Error message:
No response
NumPy/Python version information:
1.21.6 could reproduce this BUG.
Context for the issue:
No response
The text was updated successfully, but these errors were encountered: