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' referenced before assignment #222

Closed
shuxiaobo opened this Issue Sep 6, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@shuxiaobo

shuxiaobo commented Sep 6, 2018

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

@lanpa lanpa closed this in 936acdc Sep 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment