Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

line 81, in __getitem__\n fn_str = self.vocab[\'program_idx_to_token\'][fn_idx]\nKeyError: tensor(1)\n' #47

Open
PuPuLL opened this issue May 20, 2019 · 2 comments

Comments

@PuPuLL
Copy link

PuPuLL commented May 20, 2019

I used python 3.7 and pytorch 1.0.
when I try to train on CLEVR it occur a program like below:
python scripts/train_model.py \

--model_type PG
--num_train_samples 18000
--num_iterations 20000
--checkpoint_every 1000
--checkpoint_path data/program_generator.pt
Reading features from data/train_features.h5
Reading questions from data/train_questions.h5
Reading question data into memory
Reading features from data/val_features.h5
Reading questions from data/val_questions.h5
Reading question data into memory
Here is the program generator:
Seq2Seq(
(encoder_embed): Embedding(93, 300)
(encoder_rnn): LSTM(300, 256, num_layers=2, batch_first=True)
(decoder_embed): Embedding(44, 300)
(decoder_rnn): LSTM(556, 256, num_layers=2, batch_first=True)
(decoder_linear): Linear(in_features=256, out_features=44, bias=True)
)
train_loader has 18000 samples
val_loader has 10000 samples
Starting epoch 1
Traceback (most recent call last):
File "scripts/train_model.py", line 492, in
main(args)
File "scripts/train_model.py", line 153, in main
train_loop(args, train_loader, val_loader)
File "scripts/train_model.py", line 210, in train_loop
for batch in train_loader:
File "/home/nicholas/anaconda3/envs/py/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 637, in next
return self._process_next_batch(batch)
File "/home/nicholas/anaconda3/envs/py/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
KeyError: 'Traceback (most recent call last):\n File "/home/nicholas/anaconda3/envs/py/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop\n samples = collate_fn([dataset[i] for i in batch_indices])\n File "/home/nicholas/anaconda3/envs/py/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 138, in \n samples = collate_fn([dataset[i] for i in batch_indices])\n File "/home/nicholas/code/clevr-iep-master/iep/data.py", line 81, in getitem\n fn_str = self.vocab['program_idx_to_token'][fn_idx]\nKeyError: tensor(1)\n'

is my python&pytorch version didn't fit it?
If anyone can solve this I'll be really appreciate
thx

@1245994042
Copy link

I have a same problem.Did you solve it ?

@PuPuLL
Copy link
Author

PuPuLL commented Jun 14, 2019

I solved the issue
it occurs because of the indices
the 'program_idx_to_token' needs a number, but got a tensor(1)
so you need to change the code at data.py ,line 81,into the following:
def getitem(self, index):
question = self.all_questions[index]
image_idx = self.all_image_idxs[index]
answer = self.all_answers[index]
program_seq = None
if self.all_programs is not None:
program_seq = self.all_programs[index]
image = None
if self.image_h5 is not None:
image = self.image_h5['images'][image_idx]
image = torch.FloatTensor(np.asarray(image, dtype=np.float32))
feats = self.feature_h5['features'][image_idx]
feats = torch.FloatTensor(np.asarray(feats, dtype=np.float32))
program_json = None
if program_seq is not None:
program_json_seq = []
for fn_idx in program_seq:
fn_idx = fn_idx.item()
fn_str = self.vocab['program_idx_to_token'][fn_idx]
if fn_str == '' or fn_str == '': continue
fn = iep.programs.str_to_function(fn_str)
program_json_seq.append(fn)
if self.mode == 'prefix':
program_json = iep.programs.prefix_to_list(program_json_seq)
elif self.mode == 'postfix':
program_json = iep.programs.postfix_to_list(program_json_seq)
return (question, image, feats, answer, program_seq, program_json)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants