You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
I have checked the CHANGELOG and the commit log to find out if the bug was already fixed in the main branch.
I have included in the "Description" section below a traceback from any exceptions related to this bug.
I have included in the "Related issues or possible duplicates" section below all related issues and possible duplicate issues (If there are none, check this box anyway).
I have included in the "Environment" section below the name of the operating system and Python version that I was using when I discovered this bug.
I have included in the "Environment" section below the output of pip freeze.
I have included in the "Steps to reproduce" section below a minimally reproducible example.
Description
In integrated_gradient.py, the gradient is normed assuming the input is of shape (b, seq_len, emb_size...), but sometimes the input is more than one sequence. It will trigger the following error:
Python traceback:
<ipython-input-2-ba1db62dbe43> in <listcomp>(.0)
2 embedding_grad = numpy.sum(grad[0], axis=1)
3 norm = numpy.linalg.norm(embedding_grad, ord=1)
----> 4 normalized_grad = [math.fabs(e) / norm for e in embedding_grad]
5 return normalized_grad
TypeError: only size-1 arrays can be converted to Python scalars
run the following code, the old one is current way and the new one is proposed new way.
Example source:
# To add a new cell, type '# %%'# To add a new markdown cell, type '# %% [markdown]'# %%importnumpyimportmath# %%defold_norm_grad(grad:numpy.ndarray):
embedding_grad=numpy.sum(grad[0], axis=1)
norm=numpy.linalg.norm(embedding_grad, ord=1)
normalized_grad= [math.fabs(e) /normforeinembedding_grad]
returnnormalized_grad# %%defnew_norm_grad(grad: numpy.ndarray):
embedding_grad=numpy.sum(grad[0], axis=-1)
norm=numpy.linalg.norm(embedding_grad, ord=1, keepdims=True)
normalized_grad=embedding_grad/normreturnnormalized_grad# %%test_grad=numpy.random.rand(1,13,100)
numpy.testing.assert_array_equal(old_norm_grad(test_grad), new_norm_grad(test_grad)) # No error raised# %%test_grad=numpy.random.rand(1,2,13,100)
numpy.testing.assert_array_equal(old_norm_grad(test_grad), new_norm_grad(test_grad)) # raised TypeError: only size-1 arrays can be converted to Python scalars# %%
I have created a commit targeting this issue, if applicable I could pr anytime: zegnog@2415cb9
The text was updated successfully, but these errors were encountered:
Checklist
main
branch of AllenNLP.pip freeze
.Description
In
integrated_gradient.py
, the gradient is normed assuming the input is of shape (b, seq_len, emb_size...), but sometimes the input is more than one sequence. It will trigger the following error:Python traceback:
Related issues or possible duplicates
Environment
OS: MacOS
Python version: 3.8.3
Output of
pip freeze
:Steps to reproduce
run the following code, the old one is current way and the new one is proposed new way.
Example source:
I have created a commit targeting this issue, if applicable I could pr anytime:
zegnog@2415cb9
The text was updated successfully, but these errors were encountered: