-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Fix dtype of matutils.unitvec
. Fix #1722
#1761
Conversation
@@ -426,7 +426,7 @@ def unitvec(vec, norm='l2'): | |||
if norm == 'l2': | |||
veclen = blas_nrm2(vec) | |||
if veclen > 0.0: | |||
return blas_scal(1.0 / veclen, vec) | |||
return blas_scal(1.0 / veclen, vec).astype(vec.dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @accraze , thanks for the fix! A better solution here might be to modify the hardcoded dtype in line 423 above, it simplifies the logic, and also ensures that the dtype is consistent for vectors with all zeros too (a rather trivial and probably uncommon case, of course)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jayantj, I looked into this, however blas_scal
returns an array of type float
(see line 398). Not sure if there is a better way to handle this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jayantj What else needs to be done? blas_scal
is not being used anywhere else. So, should I define blas_scal
before line 429 and remove hardcoded float from its the definition?
Ping @accraze, are you planning to finish this PR? |
@menshikh-iv yes will get it finished this week |
@accraze tests failed because next problem in |
matutils.unitvec currently returns a unitvector of a different dtype from the input vector if the input dtype isn't np.float. we should make the return type consistent with the input type. fixes piskvorky#1722
ping @accraze |
Ping @accraze, are you planning to finish this PR? |
@menshikh-iv I'm still looking for a solution, it seems that the
@pushpankar I've tried moving |
if norm == 'l1': | ||
veclen = np.sum(np.abs(vec)) | ||
if norm == 'l2': | ||
veclen = blas_nrm2(vec) | ||
if veclen > 0.0: | ||
return blas_scal(1.0 / veclen, vec) | ||
return blas_scal(1.0 / veclen, vec).astype(vec.dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this cast at the end astype(vec.dtype)
is responsible for causing the returned matrix to be an int-matrix with all zeros. It is impossible to maintain consistency in types between the input array and the returned array for an int input array (since a normalized vector can almost never be an int array). A solution could be to check if the input array is int-like, and handle it accordingly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have issued a new pull request addressing this.
matutils.unitvec
. Fix #1722
Looks abandoned, I close this PR. |
matutils.unitvec
currently returns a unitvector of a different dtype from the input vector if the input dtype isn'tnp.float
we should make the return type consistent with the input type.
fixes #1722