ValueError: Expected more than 1 spatial element when training, got input size torch.Size([16, 2048, 1])

Hi all. I am using CycleGAN on 1-D tensors and keep getting this error. I searched for this error all over the blogs and everyone is saying it is because of batchnorm. Yet I only use instancenorm in my model.
Some say, increase your batchsize, I started with batch size=1 and increased to different numbers, did not work. Not sure at this point what to do.

fake_horse = gen_H(zebra)
Traceback (most recent call last):

File “”, line 1, in
fake_horse = gen_H(zebra)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1051, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\furka\Downloads\GANs\CycleGAN\generator_model.py”, line 51, in forward
x = layer(x)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1051, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\furka\Downloads\GANs\CycleGAN\generator_model.py”, line 16, in forward
return self.conv(x)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1051, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\container.py”, line 139, in forward
input = module(input)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1051, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\modules\instancenorm.py”, line 57, in forward
return F.instance_norm(

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\functional.py”, line 2325, in instance_norm
_verify_spatial_size(input.size())

File “C:\Users\furka\anaconda3\lib\site-packages\torch\nn\functional.py”, line 2292, in _verify_spatial_size
raise ValueError(“Expected more than 1 spatial element when training, got input size {}”.format(size))

ValueError: Expected more than 1 spatial element when training, got input size torch.Size([16, 2048, 1])

As described in the docs:

The mean and standard-deviation are calculated per-dimension separately for each object in a mini-batch.

so I think this is expected as your the stats would be calculated from each sample in the temporal dimension, which won’t be possible for a single value (the stddev would be NaN).

1 Like