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 'start_idx' referenced before assignment in HiLAP/util.py #15

Open
hannabros opened this issue Jul 22, 2021 · 1 comment

Comments

@hannabros
Copy link

Hi, I found unboundlocalerror in util.py

Traceback (most recent call last):
  File "main.py", line 786, in <module>
    train_sl()
  File "main.py", line 523, in train_sl
    for ct, (tokens, doc_ids) in tqdm(enumerate(g)):
  File "/home/ubuntu/anaconda3/envs/HiAGM/lib/python3.6/site-packages/tqdm/std.py", line 1185, in __iter__
    for obj in iterable:
  File "/home/ubuntu/workspace/kb.d2c/HiLAP/util.py", line 233, in gen_minibatch
    for token, label in iterate_minibatches_order(args, tokens, labels, mini_batch_size):
  File "/home/ubuntu/workspace/kb.d2c/HiLAP/util.py", line 225, in iterate_minibatches_order
    if start_idx + batchsize < inputs.shape[0]:
UnboundLocalError: local variable 'start_idx' referenced before assignment

As the code below, start_idx in if loop is outside of for loop. I guess it should be inside of for loop.

def iterate_minibatches_order(args, inputs, targets, batchsize):
    assert inputs.shape[0] == targets.shape[0]
    if args.debug:
        for _ in range(300):
            yield inputs[:batchsize], targets[:batchsize]
        return
    indices = np.argsort([-len(doc) for doc in inputs])
    for start_idx in range(0, inputs.shape[0] - batchsize + 1, batchsize):
        excerpt = indices[start_idx:start_idx + batchsize]
        yield inputs[excerpt], targets[excerpt]
    if start_idx + batchsize < inputs.shape[0]:
        excerpt = indices[start_idx + batchsize:]
        yield inputs[excerpt], targets[excerpt]
@anj3412
Copy link

anj3412 commented Nov 22, 2023

Any updates for this? @morningmoni

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