Repository: pytorch
Issue: Better Error report in torch.distribution.*.sample (when passing a non-iterable)
Labels: module: distributions,module: error checking,triaged

### 🐛 Describe the bug

```python
In [1]: import torch

In [2]: dist = torch.distributions

In [3]: dist.Normal(0.5, 1).sample(100)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [3], in <module>
----> 1 dist.Normal(0.5, 1).sample(100)

File ~/miniforge3/lib/python3.9/site-packages/torch/distributions/normal.py:62, in Normal.sample(self, sample_shape)
     61 def sample(self, sample_shape=torch.Size()):
---> 62     shape = self._extended_shape(sample_shape)
     63     with torch.no_grad():
     64         return torch.normal(self.loc.expand(shape), self.scale.expand(shape))

File ~/miniforge3/lib/python3.9/site-packages/torch/distributions/distribution.py:247, in Distribution._extended_shape(self, sample_shape)
    237 """
    238 Returns the size of the sample returned by the distribution, given
    239 a `sample_shape`. Note, that the batch and event shapes of a distribution
   (...)
    244     sample_shape (torch.Size): the size of the sample to be drawn.
    245 """
    246 if not isinstance(sample_shape, torch.Size):
--> 247     sample_shape = torch.Size(sample_shape)
    248 return sample_shape + self._batch_shape + self._event_shape

TypeError: 'int' object is not iterable
```


While for someone experienced, the error might be obvious. For someone looking at this the first time, I wonder if a better message might be that Expected `torch.Size()` but found `int` followed by a quick sample usage also shown as a part of the error. This should be a quick and easy fix. Perhaps there are more such places where such better error messages might be useful.

```python
>>>import torch
>>>dist = torch.distributions
>>>dist.Normal(0.5, 1).sample(torch.Size([100]))
```



### Versions

PyTorch version: 1.10.2
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.2.1 (arm64)
GCC version: Could not collect
Clang version: 13.0.0 (clang-1300.0.29.30)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:24:02)  [Clang 11.1.0 ] (64-bit runtime)
Python platform: macOS-12.2.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.5
[pip3] torch==1.10.2
[pip3] torchvision==0.2.2
[conda] mypy_extensions           0.4.3            py39h2804cbe_4    conda-forge
[conda] numpy                     1.21.5           py39h47fb9ce_0    conda-forge
[conda] pytorch                   1.10.2          cpu_py39hbfdb42d_0    conda-forge
[conda] torchvision               0.2.2                      py_3    pytorch

cc @fritzo @neerajprad @alicanb @nikitaved