Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUG: Fix norm type promotion#10667

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@kmaehashi
Copy link
Member

I testednp.linalg.norm with the following code, but it seems the type is not preserved correctly.

importnumpyasnpfordtypein [np.float16,np.float32,np.float64]:a=np.ones((2,),dtype=dtype)ret=np.linalg.norm(a,3,None,False)ifnot (dtype==ret.dtype):print('expected',dtype,'but got',ret.dtype)

Without this fix (NumPy 1.14.1):

expected <class 'numpy.float16'> but got float64expected <class 'numpy.float32'> but got float64

With NumPy 1.14.1 + this fix, nothing should be printed.

Related to#10368 andcupy/cupy#875 (comment)

@eric-wieser
Copy link
Member

eric-wieser commentedFeb 26, 2018
edited
Loading

This doesn't affect the result dtype at all, AFAICT

**= operates in-place, so should not result in a type promotion (edit: except on scalars, where it does not)

@eric-wieser
Copy link
Member

eric-wieser commentedFeb 26, 2018
edited
Loading

Edit: I'm wrong.

Thanks to#10374,np.reciprocal is unusable in generic code. You can usenp.true_divide(1, ord, dtype=...) here instead

I think that there's a deeper problem here with numpy type promotion, and howscalar op= val gives a different promotion toarr op= val

@kmaehashi
Copy link
MemberAuthor

kmaehashi commentedFeb 26, 2018
edited
Loading

Ah, I see.

x=np.ndarray(1,dtype=np.float32)x**=1.0/2.0x.dtype# => float32x=np.float32(1)x**=1.0/2.0x.dtype# => float64

Thanks to#10374, np.reciprocal is unusable in generic code.

I think the code in this PR is generic (i.e., works with both scalar and array).
Do you mean such use ofnp.reciprocal is discouraged (even withdtype=ret.dtype)?

importnumpyasnpprint('scalar',np.linalg.norm(np.ones((2,),dtype=np.float32),3,0,False).dtype)print('array',np.linalg.norm(np.ones((2,2),dtype=np.float32),3,0,False).dtype)

Output from NumPy 1.14.1:

scalar float64array float32

Output from NumPy 1.14.1 + this PR:

scalar float32array float32

@charrischarris changed the titlefix norm type promotionBUG: Fix norm type promotionFeb 27, 2018
@eric-wieser
Copy link
Member

Fix looks good, but this needs a test.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@kmaehashi@eric-wieser@charris

[8]ページ先頭

©2009-2025 Movatter.jp