# How can I replace the forward method of a predefined torchvision model with my customized forward function?

**URL:** <https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224>\
**Category:** Uncategorized\
**Created:** [August 24, 2019, 9:19pm UTC](https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224 "2019-08-24T21:19:03Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![ccalafiore](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/ba9def/32.png) [@ccalafiore](https://discuss.pytorch.org/u/ccalafiore)\
**Post date:** [February 10, 2022, 1:26pm UTC](https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224/11 "2022-02-10T13:26:34Z")

</div>

An alternative to @ptrblck 's solution is this:

```auto
import torchvision.models as models
import torch

def new_forward(self, x):
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)

    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    return x

# define a resnet instance
resent = models.resnet18()

# add new_forward function to the resnet instance as a class method
bound_method = new_forward. __get__ (resent, resent. __class__ )
setattr(resent, 'forward', bound_method)

# you can also remove the 2 layers resent.avgpool and resent.fc because you are not using them in the new forward method
delattr(resent, 'avgpool')
delattr(resent, 'fc')

# call the new forward method
inputs = torch.rand(1, 3, 224, 224)
outputs = resent(inputs)

print('type(resent) = ', type(resent))
print('type(resent.forward) = ', type(resent.forward))
print('outputs.shape = ', outputs.shape)

```

---

_[View the full topic](https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224)._
