Skip to content
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

TypeError: unsupported operand type(s) for -: 'int' and '_dynet.Expression' #9

Open
sfedia opened this issue Jul 10, 2024 · 0 comments

Comments

@sfedia
Copy link

sfedia commented Jul 10, 2024

Hi! I've encountered an issue related to scalar-matrix arithmetic operations.
Whenever a multiplication, addition, subtraction, or division operation occurs between a float/int and an instance of dynet.Expression, this exception is raised if the float/int precedes a dynet.Expression instance.
If, on the contrary, an instance of dynet.Expression is placed before float/int, no exception occurs. I suppose that this happens due to the strict order of arguments in methods for scalar-matrix operators (matrix goes first, then scalar goes): https://dynet.readthedocs.io/en/latest/operations.html

Traceback (most recent call last):
  File "postcorrection/multisource_wrapper.py", line 63, in <module>
    pretrainer = PretrainHandler(
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/pretrain_handler.py", line 81, in __init__
    self.pretrain_model(pretrain_src1, pretrain_src2, pretrain_tgt, epochs)
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/pretrain_handler.py", line 88, in pretrain_model
    self.seq2seq_trainer.train(
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/seq2seq_trainer.py", line 53, in train
    losses.append(self.model.get_loss(src1, src2, tgt))
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/multisource_model.py", line 295, in get_loss
    return self.decode_loss(src1, src2, tgt)
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/multisource_model.py", line 278, in decode_loss
    probs, _ = self.get_pointergen_probs(
  File "/Users/fyodorsizov/Documents/git/ocr-post-correction/postcorrection/multisource_model.py", line 204, in get_pointergen_probs
    copy_probs = a_t * (1 - p_gen)
TypeError: unsupported operand type(s) for -: 'int' and '_dynet.Expression'

Have you seen something like this before? Any idea how to fix this other than changing the order of terms inside the code or overriding __mul__, __add__, etc methods?

sfedia added a commit to sfedia/ocr-post-correction that referenced this issue Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant