Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Cannot unsqueeze empty tensor #2660

Closed
ksurya opened this issue Mar 28, 2019 · 4 comments · Fixed by #2697
Closed

Cannot unsqueeze empty tensor #2660

ksurya opened this issue Mar 28, 2019 · 4 comments · Fixed by #2697
Assignees

Comments

@ksurya
Copy link
Contributor

ksurya commented Mar 28, 2019

System (please complete the following information):

  • OS: OSX
  • Python version: 3.6.5
  • AllenNLP version: 0.8.1
  • PyTorch version: 0.4.1

Question
How can I have empty fields.ListField?

I am having fields.ListField([ text_field.empty_field() ]) as suggested in #1391

but Torch raises cannot unsqueeze empty tensor on executing instance.as_tensor_dict() or iterator(..)

image

@vidurj vidurj self-assigned this Mar 28, 2019
@brendan-ai2 brendan-ai2 assigned brendan-ai2 and unassigned vidurj Apr 6, 2019
@brendan-ai2
Copy link
Contributor

Here's a repro:

import torch
from allennlp.data.token_indexers.single_id_token_indexer import SingleIdTokenIndexer
token_indexers = {"tokens": SingleIdTokenIndexer()}
from allennlp.data.tokenizers.word_tokenizer import WordTokenizer
tokenizer = WordTokenizer()
tokens = tokenizer.tokenize("Foo")
from allennlp.data.fields.text_field import TextField
from allennlp.data.fields.list_field import ListField
text_field = TextField(tokens, token_indexers)
list_field = ListField([text_field.empty_field()])
fields = {'list': list_field}
from allennlp.data.instance import Instance
instance = Instance(fields)
instance.as_tensor_dict()

@brendan-ai2
Copy link
Contributor

I think this bug is due to there not be any non-empty lists in your batch. If there were, the aggregated padding would prevent this bug. I have a speculative fix in #2697, but I'm really not sure if it's the right approach.

@DeNeutoy
Copy link
Contributor

DeNeutoy commented Apr 8, 2019

Hi @ksurya - could you let us know a bit more about how you are using the ListField(textfield.empty_field())? We've found that in many cases, you might be wanting to model something in a way which might be easier if approached differently, but equally we don't want allennlp to crash in this scenario either. It would be helpful to see some code, (maybe your DatasetReader) and hear a bit about the model you are trying to build. Thanks!

@ksurya
Copy link
Contributor Author

ksurya commented Apr 9, 2019

Hi guys, appreciate your interest on this issue. I will get back with a Jupyter Notebook reproducing this problem and let you know.

brendan-ai2 added a commit that referenced this issue May 6, 2019
- There appears to be an edge case in our handling of empty ListFields such that we fail to tensorize a batch of entirely empty ones. 
- Proposed solution: Make `TextField` always return a minimum padding size of 1.
- Fixes #2660
reiyw pushed a commit to reiyw/allennlp that referenced this issue Nov 12, 2019
- There appears to be an edge case in our handling of empty ListFields such that we fail to tensorize a batch of entirely empty ones. 
- Proposed solution: Make `TextField` always return a minimum padding size of 1.
- Fixes allenai#2660
TalSchuster pushed a commit to TalSchuster/allennlp-MultiLang that referenced this issue Feb 20, 2020
- There appears to be an edge case in our handling of empty ListFields such that we fail to tensorize a batch of entirely empty ones. 
- Proposed solution: Make `TextField` always return a minimum padding size of 1.
- Fixes allenai#2660
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants