Hello, I am trying to compute the std for an image, but I want a different std for each channel. Suppose we have an image with the shape [channel,x,y]
, in numpy we can achieve this by using multi-axis, like: img.std(axis=(1,2))
Unfortunately, the torch std implementation does not support multi-axis, so I tryed to implement my own function:
def std_for_channel(img):
if (torch.typename(img) == 'torch.autograd.variable.Variable'):
num_channels = img.size()[0]
else:
num_channels = img.shape[0]
out = torch.zeros(num_channels)
for i in range(num_channels):
out[i] = img[i,:,:].std(unbiased=False)
return out
while this works when passing a tensor, I keep getting error when using this on a Variable (getting ‘RuntimeError: can’t assign a Variable to a scalar value of type float’). Since I want to use this std information in my custom loss, how can I implement it correctly so backpropagation will be able to train the weights of the network?