morningmoni / HiLAP

Code for paper "Hierarchical Text Classification with Reinforced Label Assignment" EMNLP 2019
140 stars 34 forks source link

UnboundLocalError: local variable 'start_idx' referenced before assignment in HiLAP/util.py #15

Open hannabros opened 3 years ago

hannabros commented 3 years ago

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 commented 1 year ago

Any updates for this? @morningmoni