I was trying to use the CutMix callback today with the fastai2 audio library and was getting the following error:
AttributeError: 'float' object has no attribute 'unsqueeze'
Which I traced back to line 29 in the cutmix callback:
if not self.stack_y: ny_dims = len(self.y.size()) --> self.learn.yb = tuple(L(self.yb1,self.yb).map_zip(torch.lerp,weight=unsqueeze(self.lam, n=ny_dims-1)))
self.lam being the offending float:
self.lam = (1 - ((x2-x1)*(y2-y1))/float(W*H)).item()
Which gets sent to
def unsqueeze(x, dim=-1, n=1): "Same as `torch.unsqueeze` but can add `n` dims" for _ in range(n): x = x.unsqueeze(dim) return x
It worked by removing the
.item() and so keeping it a tensor rather than turning it into a python float i.e.
self.lam = (1 - ((x2-x1)*(y2-y1))/float(W*H)). But I’m not sure why it has the
.item() in the first place?