# RuntimeError: The size of tensor a (65537) must match the size of tensor b (50177) at non-singleton dimension 1

**URL:** <https://discuss.pytorch.org/t/runtimeerror-the-size-of-tensor-a-65537-must-match-the-size-of-tensor-b-50177-at-non-singleton-dimension-1/151666>\
**Category:** vision\
**Created:** [May 14, 2022, 11:59am UTC](https://discuss.pytorch.org/t/runtimeerror-the-size-of-tensor-a-65537-must-match-the-size-of-tensor-b-50177-at-non-singleton-dimension-1/151666 "2022-05-14T11:59:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Matias\_Vasquez](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/matias_vasquez/32/47847_2.png) [@Matias\_Vasquez](https://discuss.pytorch.org/u/Matias_Vasquez)\
**Post date:** [May 15, 2022, 4:38pm UTC](https://discuss.pytorch.org/t/runtimeerror-the-size-of-tensor-a-65537-must-match-the-size-of-tensor-b-50177-at-non-singleton-dimension-1/151666/2 "2022-05-15T16:38:19Z")

</div>

Here you can see what can happen when you use `nn.Sequential`.

> [@Wrapping submodules in Sequential](https://discuss.pytorch.org/t/wrapping-submodules-in-sequential/151700/2):
>
> Maybe the easiest way to understand this is with a little example. Here I have defined two Modules that perform functional stuff. The SimpleBlock multiplies by 2. The MainModel uses this SimpleBlock in two different places and also performs some functional stuff (multiply by 3 and add 3). This can, of course, be something more complex like using layers, batch normalization, flatten or whatever you want to do. class SimpleBlock(torch.nn.Module): def \_\_init\_\_(self): super().\_\_init\_\_…

If you inspect [here](https://github.com/facebookresearch/pytorchvideo/blob/825e8aa9430e6dc431c9b6548355820ba08dbf09/pytorchvideo/models/hub/x3d.py#L132) how the `x3d` is created, you will see that it uses `nn.Modules`. If you see my other answer then you will see why this works.

Now in contrast, MViT is using functional API on the `forward` pass. You can inspect the source code [here](https://github.com/facebookresearch/pytorchvideo/blob/825e8aa9430e6dc431c9b6548355820ba08dbf09/pytorchvideo/models/vision_transformers.py#L168).

If you use the `original_model`, then, as you said, you have to input the video in the shape `BxCxTxHxW` where `C=3`, `H=224` and `W=224`.

## Not changing anything

This will work.

```python
model_name = 'mvit_base_32x3'
original_model = torch.hub.load('facebookresearch/pytorchvideo', model_name, pretrained=True)

input_vid = torch.randn(1, 3,32,224,224)
original_model(input_vid).shape

```

```python
# Output
torch.Size([1, 400])

```

## Using nn.Sequential

Above I have linked another answer as to why it may not work with `nn.Sequential`.

```python
features = torch.nn.Sequential(*list(original_model.children())[:-1])
features(input_vid)

```

As you said, this will raise the following error

```python
--> 201 raise NotImplementedError

```

## Modifying the head

If you print the original model, you can see that you can access the head like this

```python
original_model.head

```

```python
# Output
VisionTransformerBasicHead(
  (sequence_pool): SequencePool()
  (dropout): Dropout(p=0.5, inplace=False)
  (proj): Linear(in_features=768, out_features=400, bias=True)
)

```

Here you can see that `original_model.head.proj` is a `Linear` layer. You can modify this to fit your needs.

For example

```python
model_name = 'mvit_base_32x3'
original_model = torch.hub.load('facebookresearch/pytorchvideo', model_name, pretrained=True)

N_CLASSES = 10
original_model.head.proj = torch.nn.Linear(768, N_CLASSES)

input_vid = torch.randn(1, 3,32,224,224)
original_model(input_vid).shape

```

This will work.

```python
# Output
torch.Size([1, 10])

```

If, for example you wanted to get rid of this `Linear` layer you could use `nn.Identity()` instead.

This will give you the output up until before the last `Linear` layer.

---

_[View the full topic](https://discuss.pytorch.org/t/runtimeerror-the-size-of-tensor-a-65537-must-match-the-size-of-tensor-b-50177-at-non-singleton-dimension-1/151666)._
