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

UnboundLocalError: local variable 'test_loss' referenced before assignment #36

Closed
ls-simon-he opened this issue Aug 29, 2020 · 2 comments

Comments

@ls-simon-he
Copy link

ls-simon-he commented Aug 29, 2020

When the training process finished the first epoch and saved the model, it threw out the error:

epoch 1 iter 8713: train loss 0.25403. lr 3.000169e-04: 100%|██████████| 8714/8714 [4:12:50<00:00,  1.74s/it]  
Traceback (most recent call last):
  File "try.py", line 105, in <module>
    trainer.train()
  File "/home/ec2-user/minGPT/mingpt/trainer.py", line 129, in train
    best_loss = test_loss
UnboundLocalError: local variable 'test_loss' referenced before assignment

Environment: Python3 + pytorch 1.6

@tawnkramer
Copy link

you have to prepare a test set and pass it into the trainer. I made a method of class CharDataset to split it into two sets:

def split(self, perc):
        iTrim = round(len(self.data) * perc)
        iKeep = len(self.data) - iTrim
        test = self.data[iKeep:]
        train = self.data[:iKeep]
        self.data = train

        test_set = CharDataset(test, self.block_size)
        # maintain same dict
        test_set.stoi = self.stoi
        test_set.itos = self.itos

        return test_set

then

test_dataset = train_dataset.split(0.2) # take last 20% for a test set
tconf.ckpt_path = "mycheck.ckpt"
trainer = Trainer(model, train_dataset, test_dataset, tconf)

@ls-simon-he
Copy link
Author

Yeh, that makes sense. Thanks @tawnkramer.

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

2 participants