# ViTMAEModel With model.eval(), get two different representations?

**URL:** <https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466>\
**Category:** 🤗Transformers\
**Created:** [August 10, 2022, 8:10am UTC](https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466 "2022-08-10T08:10:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMing](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mming/32/57751_2.png) [@MMing](https://discuss.huggingface.co/u/MMing)\
**Post date:** [August 10, 2022, 8:10am UTC](https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466/1 "2022-08-10T08:10:16Z")

</div>

My code as following, how to get equal encodings from same image?

```auto
import torch
from transformers import ViTMAEModel

pixel_value = torch.randn([1, 3, 224, 224])

model = ViTMAEModel.from_pretrained("facebook/vit-mae-base").eval() # convert to evaluate mode.

encoding_a = model(pixel_value).last_hidden_state[:, 1:, :].mean(dim=1)
encoding_b = model(pixel_value).last_hidden_state[:, 1:, :].mean(dim=1)

# print(encoding_a == encoding_b )
assert torch.equal(encoding_a , encoding_b ) # not equal!

```

---

<div class="post-metadata">

**Author:** ![MMing](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mming/32/57751_2.png) [@MMing](https://discuss.huggingface.co/u/MMing)\
**Post date:** [August 10, 2022, 9:23am UTC](https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466/2 "2022-08-10T09:23:03Z")

</div>

I have handle it problem: just set ‘mask\_ratio = 0’, I don’t known whether this way is apply to inference?

```auto
model = ViTMAEModel.from_pretrained("facebook/vit-mae-base", mask_ratio=0.0).eval() # convert to evaluate mode.

```

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [August 10, 2022, 9:27am UTC](https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466/3 "2022-08-10T09:27:56Z")

</div>

Hi,

The model internally generates a random boolean mask as seen [here](https://github.com/huggingface/transformers/blob/4a51075a96d2049f368b5f3dd6c0e9f08f599b62/src/transformers/models/vit_mae/modeling_vit_mae.py#L272).

To make it reproducable, one can provide a `noise` argument to the forward method (to make sure the same boolean mask is applied):

```auto
import numpy as np
import torch
from transformers import ViTMAEModel

pixel_values = torch.randn([1, 3, 224, 224])

model = ViTMAEModel.from_pretrained("facebook/vit-mae-base")

noise = num_patches = int((model.config.image_size // model.config.patch_size) ** 2)
noise = np.random.uniform(size=(1, num_patches))

encoding_a = model(pixel_values, noise=torch.from_numpy(noise)).last_hidden_state[:, 1:, :].mean(dim=1)
encoding_b = model(pixel_values, noise=torch.from_numpy(noise)).last_hidden_state[:, 1:, :].mean(dim=1)
assert torch.equal(encoding_a , encoding_b)

```

---

<div class="post-metadata">

**Author:** ![MMing](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mming/32/57751_2.png) [@MMing](https://discuss.huggingface.co/u/MMing)\
**Post date:** [August 10, 2022, 12:24pm UTC](https://discuss.huggingface.co/t/vitmaemodel-with-model-eval-get-two-different-representations/21466/5 "2022-08-10T12:24:51Z")

</div>

Thanks your reply.

But I found that we just need to reload the model with parameter ‘mask\_ratio = 0.0’( equates to let model see all patchs), then could get reproducable encoding from same image.
