# Forward in Model Definition

**URL:** <https://discuss.pytorch.org/t/forward-in-model-definition/83028>\
**Category:** vision\
**Created:** [May 27, 2020, 1:23am UTC](https://discuss.pytorch.org/t/forward-in-model-definition/83028 "2020-05-27T01:23:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoobCoder](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/e9bcb4/32.png) [@NoobCoder](https://discuss.pytorch.org/u/NoobCoder)\
**Post date:** [May 27, 2020, 1:23am UTC](https://discuss.pytorch.org/t/forward-in-model-definition/83028/1 "2020-05-27T01:23:17Z")

</div>

Hello,

I am writing a model in Pytorch. At the last layer, I have a fully connected that gives 3 points (let’s assume {x, y, h} ).

Now, I want to add a function in a `forward` that it creates a tensor of size 5\*5 which has the value of axes in its. (for example tensor[1,1]=[1,1] ). I want to compute the euclidean distance of this tensor from the {x,y} which is the output of the fully connected layer from this [post](https://discuss.pytorch.org/t/efficient-distance-matrix-computation/9065/5). My question is that how to do so that in the backward, I do not occur any problem??

Here is the sample code for the model.

```auto

def euclidean_dist(tensor1, tensor2):
     return distnace

class MyModel(): 

     def __init__ ():
            
          some convulotion layers...
          self.fc = nn.Linear(num_features, 3)

     def forward(in):

         .
         .
         (x, y, h) = fc(in)

         # Create the tensor with the axes value
         temp = torch.tensor([[a, b] for a in range(5) for b in range(5)])
         
         center = torch.tensor([[x,y]])

         # Now compute the distance between this and points
         distance = euclidean_distance(temp, centers)

         out = distance.view(5,5)
         out = out * h

         return out

```

Is it right to do this in this way?

Thanks.
