I want to initialize tensor “a” as shown in below code and want to access it in forward call. But it shows the " NameError: name ‘a’ is not defined "
import torch
import torch.nn as nn
from torch.autograd import Function
class run(Function):
def init():
a=torch.linspace(0,1,0.2)
@staticmethod
def forward(cxt,input):
cxt.save_for_backward(input)
print(" a is ",a)
return input
@staticmethod
def backward(self,cxt, grad_output):
input, = cxt.saved_tensors
grad_input = grad_output.clone()
grad_input[input < 0] = 0
return grad_input
fun1 = run.apply
if name == “main”:
from torch.autograd import Variable
x=torch.tensor(2.0,requires_grad=True)
vc=fun1(x)