8000 BUG: wrong dtype computing with 0-D teensor · Issue #22823 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
HydrogenSulfate opened this issue Dec 19, 2022 · 6 comments
Closed

BUG: wrong dtype computing with 0-D teensor #22823

HydrogenSulfate opened this issue Dec 19, 2022 · 6 comments
Labels

Comments

@HydrogenSulfate
Copy link
HydrogenSulfate commented Dec 19, 2022

Describe the issue:

I guess result dtype should be same as input dtype, but code below is not.

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) # float64

and when z is reshaped to [1], dtype is consistent with input as 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).reshape([1]) # reshape to [1]
print(z.ndim) # 1
print(z.shape) # (1,)
print(z.dtype) # float32
print((z**2).dtype) # float32

Reproduce the code example:

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) # float64
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).reshape([1]) # reshape to [1]
print(z.ndim)
print(z.shape)
print(z.dtype)
print((z**2).dtype)

Error message:

No response

NumPy/Python version information:

1.21.6 could reproduce this BUG.

Context for the issue:

No response

@Pranay144
Copy link

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

@HydrogenSulfate
Copy link
Author

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😥

@eric-wieser
Copy link
Member

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

@HydrogenSulfate
Copy link
Author

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?

@seberg
Copy link
Member
seberg commented Dec 20, 2022

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. arr**2 for 0-D arrays is (as Eric pointed out) an outlier where this does not happen.

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):

# clearly will go away and might have a few very corner cases
# not 100% working yet:
np._set_promotion_state("weak")

# Your example will give the expected result now

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)

@seberg seberg closed this as completed Dec 20, 2022
@eric-wieser
Copy link
Member

I was a bit surprised that my solution worked, I came to this issue expecting it to be about that exact spelling not working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
0