UnboundLocalError: local variable 'start' referenced before assignment
See original GitHub issueDear 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:
- Created 5 years ago
- Reactions:6
- Comments:7 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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