Apply transformation for Tensor without including it in Backward
Let's say I have n layered neural network. After running l layers, I want to apply some transformation to the l^th layer output, without including that transformation in backpropagation. 

For e.g. : 

output_layer_n = self.LinearLayer(output_layer_prev)
#apply some transformation to output_layer_n, but don't want to take autograd w.r.t. this transformation, basically this transformation function doesn't have any parameter 
output_layer_n.data = TransformationFunction(output_layer_n.data) 


So how should I go about implementing it? What I want is not to take gradient accounted for TransformationFunction() in my code.

If you just don't want to compute gradients for your TransformationFunction, it is easiest to turn off gradient computation for all parameters involved in this computation by setting the requires_grad flag to False.


Excluding subgraphs from backward:


  If there’s a single input to an operation that requires gradient, its
  output will also require gradient. Conversely, only if all inputs
  don’t require gradient, the output also won’t require it. Backward
  computation is never performed in the subgraphs, where all Tensors
  didn’t require gradients.
  
  This is especially useful when you want to freeze part of your model,
  or you know in advance that you’re not going to use gradients w.r.t.
  some parameters. For example if you want to finetune a pretrained CNN,
  it’s enough to switch the requires_grad flags in the frozen base, and
  no intermediate buffers will be saved, until the computation gets to
  the last layer, where the affine transform will use weights that
  require gradient, and the output of the network will also require
  them.



Here is a small example which would do so:

import torch
import torch.nn as nn

# define layers
normal_layer = nn.Linear(5, 5)
TransformationFunction = nn.Linear(5, 5)
# disable gradient computation for parameters of TransformationFunction
# here weight and bias
TransformationFunction.weight.requires_grad = False
TransformationFunction.bias.requires_grad   = False

# input 
inp = torch.rand(1, 5)

# do computation
out = normal_layer(inp)
out = TransformationFunction(out)

# loss
loss = torch.sum(out)
# backward
loss.backward()

# gradient for l1
print('Gradients for "normal_layer"', normal_layer.weight.grad, normal_layer.bias.grad)
# gradient for l2
print('Gradients for "TransformationFunction"', TransformationFunction.weight.grad, TransformationFunction.bias.grad)


Output:

Gradients for "normal_layer" tensor([[0.1607, 0.0215, 0.0192, 0.2595, 0.0811],
        [0.0788, 0.0105, 0.0094, 0.1272, 0.0398],
        [0.1552, 0.0207, 0.0186, 0.2507, 0.0784],
        [0.1541, 0.0206, 0.0184, 0.2489, 0.0778],
        [0.2945, 0.0393, 0.0352, 0.4756, 0.1486]]) tensor([0.2975, 0.1458, 0.2874, 0.2853, 0.5452])
Gradients for "TransformationFunction" None None


I hope this is what you were looking for, if not please edit your question with more detail!
