francois-rozet / torchist Goto Github PK
View Code? Open in Web Editor NEWNumPy-style histograms in PyTorch
Home Page: https://pypi.org/project/torchist
License: MIT License
NumPy-style histograms in PyTorch
Home Page: https://pypi.org/project/torchist
License: MIT License
Runtime error when calculating bin edges using .min() and .max() functions, instead of specifying fixed numerical values.
RuntimeError: shape '[10]' is invalid for input of size 11
Using np.histogram
works correctly.
A minimal working example demonstrating the current behavior.
import torch
import torchist
import numpy as np
x = np.random.rand(10000)
x_min, x_max = x.min(), x.max()
edges = np.linspace(x_min, x_max, 11)
hist_np, _ = np.histogram(x, bins=edges)
print(hist_np.shape)
x_t = torch.from_numpy(x).cuda()
edges_t = torch.from_numpy(edges).cuda()
hist_t = torchist.histogram(x_t, edges=edges_t) # Error is reported here.
print(hist_t.shape)
Both torchist
and numpy
results should work and produce matching results.
Slightly changing the values of x_min
and x_max
solves the issue.
x_min, x_max = x.min()*1.01 , x.max()*1.01
First of all THANK YOU very much for this nice work: it helps me a lot, and I though contacting you because given your code you likely work on closely related topic with information topology problematic i work on. The problem (if it is not a misunderstanding of mine) encountered is:
if a 1 dimensional tensor (vector) is given in input to Histogramdd with sparse= True then one get the error:
"line 194, in histogramdd
hist = torch.sparse_coo_tensor(idx.t(), values, shape)
RuntimeError: number of dimensions must be sparse_dim (10) + dense_dim (0), but got 1"
A minimal working example demonstrating the current behavior.
import torch
import torchist
x = torch.rand(100, 1).cuda()
hist = torchist.histogramdd(x, bins=10, low=0.0, upp=1.0, sparse = True)
One could expect a sparse vector in output
Hi, thank you for the nice package. I am experiencing an error that I cannot figure out while using the histogramdd
function. I am making multiple histograms in a for-loop and I always feed the histogramdd
function tensors of the same shape.
>>> inputs = torch.stack([y[i], x[i], z[i]], -1) # shape [4000, 3]
>>> weights = w[i] # shape [4000]
>>> h0 = torchist.histogramdd(inputs, edges=[30, 32, 30], weights=weights)
It works fine most of the time, but then it breaks all of a sudden with this kind of error:
>>> h0 = torchist.histogramdd(inputs, edges=[30, 32, 30], weights=w[i])
File ".../torchist/__init__.py", line 225, in histogramdd
hist = idx.bincount(weights, minlength=shape.numel()).view(shape)
RuntimeError: shape '[30, 32, 30]' is invalid for input of size 29358
Since the shape of what I feed in is always the same I am confused about what might be generating the error. It looks like idx
has somehow the wrong shape, i.e. not 30x32x30. Or I am just misunderstanding something?
Edit: I am using version '0.1.8' and tensors are all CUDA tensors.
bug 1: assert torch.all(upp > low), "The upper bound must be strictly larger than the lower bound"
print(torch.all(upp > low), upp>low)
TypeError: all(): argument 'input' (position 1) must be Tensor, not bool
bug 2: func quantize:
quantize
x = torch.clip(x, min=0, max=bins - 1) # in [0, bins)
TypeError: clip() received an invalid combination of arguments - got (Tensor, max=Tensor, min=int), but expected one of:
* (Tensor input, Tensor min, Tensor max, *, Tensor out)
* (Tensor input, Number min, Number max, *, Tensor out)
bug 1: happens when low/upp is a list/sequence of scalars. torch.all requires tensor inputs, otherwise the output is False by default.
t_hist = torchist.histogramdd(t_tensor, bins =[64, 64, 32],
low=[-a, -b, -c], # variable low is a list/sequence of scalars
upp=[a, b, c] # variable low is a list/sequence of scalars
)
bug 2: bug2 appears after bug 1, because 'max=bins-1' is a tensor while 'min=0' is not.
Will add a minimal example later. Perhaps a pytorch version issue?
To fix:
bug 1: asset func stays after as_tensors;
bins = torch.as_tensor(bins).squeeze().long()
low = torch.as_tensor(low).squeeze().to(x)
upp = torch.as_tensor(upp).squeeze().to(x)
assert torch.all(upp > low), "The upper bound must be strictly larger than the lower bound"
bug 2: replace 'min=0' by 'min=x.new_zeros(bins.numel())'
x = torch.clip(x, min=x.new_zeros(bins.numel()), max=bins - 1) # in [0, bins)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.