Issue
I have a half precision input that may be an array scalar x = np.float32(3)
. I'd like to add one to it: y = x + 1
. However y
will be float64 instead of float32.
I can only think of two workarounds:
- convert the input to 1d array:
x = np.float32([3])
so thaty = x + 1
is float32 - convert 1 into lower precision:
y = np.float32(3) + np.float16(1)
is float32
However, I have a lot of functions, so the above fixes require me to add if-else statements to each function... Are there any better ways? Thanks!
Solution
You can use numpy.add(..., dtype=np.float64)
like below:
>>> res = np.add(np.array([3], dtype=np.float32), np.float16(1), dtype=np.float64)
>>> res
array([4.])
>>> res.dtype
dtype('float64')
Answered By - I'mahdi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.