question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UnboundLocalError: local variable 'start' referenced before assignment

See original GitHub issue

Dear Author: I just found an code logical bug in histogram. I think here should be raise an Exception.

def make_histogram(values, bins):
    """Convert values into a histogram proto using logic from histogram.cc."""
    values = values.reshape(-1)
    counts, limits = np.histogram(values, bins=bins)
    limits = limits[1:]
    # void Histogram::EncodeToProto in histogram.cc
    for i, c in enumerate(counts):
        if c > 0:
            start = max(0, i - 1)
            break

    for i, c in enumerate(reversed(counts)):
        if c > 0:
            end = -(i)
            break

    counts = counts[start:end]
    limits = limits[start:end]
    sum_sq = values.dot(values)
    return HistogramProto(min=values.min(),
                          max=values.max(),
                          num=len(values),
                          sum=values.sum(),
                          sum_squares=sum_sq,
                          bucket_limit=limits,
                          bucket=counts)

if all the elements in counts is 0 .there will be error like this:

File "/home/shuxiaobo/TR-experiments/cli/train.py", line 62, in train
    writer.add_histogram(name + '/grad', param.grad.clone().cpu().data.numpy(), j)
  File "/home/shuxiaobo/python3/lib/python3.6/site-packages/tensorboardX/writer.py", line 395, in add_histogram
    self.file_writer.add_summary(histogram(tag, values, bins), global_step, walltime)
  File "/home/shuxiaobo/python3/lib/python3.6/site-packages/tensorboardX/summary.py", line 142, in histogram
    hist = make_histogram(values.astype(float), bins)
  File "/home/shuxiaobo/python3/lib/python3.6/site-packages/tensorboardX/summary.py", line 162, in make_histogram
    counts = counts[start:end]
UnboundLocalError: local variable 'start' referenced before assignment

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
yoonholeecommented, Mar 6, 2019

It turns out my values were NaN, I’ve fixed my bug. Still, it would have been helpful if the error message told me that the values being NaN was the problem.

1reaction
BPDanekcommented, Mar 21, 2019

It turns out my values were NaN, I’ve fixed my bug. Still, it would have been helpful if the error message told me that the values being NaN was the problem.

This is the exact issue I had. Thank you @yoonholee for the pointer, and yes I agree, there should be something indicating this. May be related to RL

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3: UnboundLocalError: local variable referenced ...
In your case, Var1 is considered as a local variable, and it's used before being set, thus the error. To solve this problem,...
Read more >
Local variable referenced before assignment in Python
The Python UnboundLocalError: Local variable referenced before assignment occurs when we reference a local variable before assigning a value ...
Read more >
Python local variable referenced before assignment Solution
The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it ......
Read more >
UnboundLocalError: local variable ... - Net-Informations.Com
The unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the...
Read more >
Local Variable Referenced Before Assignment - STechies
The “local variable referenced before assignment” error occurs when you give reference of a local variable without assigning any value. Example:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found