UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach()
I'm new on PyTorch and I'm trying to code with it
so I have a function called OH which tack a number and return a vector like this
  def OH(x,end=10,l=12):
    x = T.LongTensor([[x]])
    end = T.LongTensor([[end]])
    one_hot_x = T.FloatTensor(1,l)
    one_hot_end = T.FloatTensor(1,l)
    first=one_hot_x.zero_().scatter_(1,x,1)
    second=one_hot_end.zero_().scatter_(1,end,1)
    vector=T.cat((one_hot_x,one_hot_end),dim=1)
    return vector
OH(0)
output:
tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0., 0., 0., 0., 1., 0.]])

now I have a NN that takes this output and return number but this warning always appear in my compiling
online.act(OH(obs))
output:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:17: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
4

I tried to to use online.act(OH(obs).clone().detach()) but it give me the same warning
and the code works fine and give good results but I need to understand this warning
Edit
the following is my NN that has the act function
class Network(nn.Module):
  def __init__(self,lr,n_action,input_dim):
    super(Network,self).__init__()
    self.f1=nn.Linear(input_dim,128)
    self.f2=nn.Linear(128,64)
    self.f3=nn.Linear(64,32)
    self.f4=nn.Linear(32,n_action)
    #self.optimizer=optim.Adam(self.parameters(),lr=lr)
    #self.loss=nn.MSELoss()
    self.device=T.device('cuda' if T.cuda.is_available() else 'cpu')
    self.to(self.device)

  def forward(self,x):
    x=F.relu(self.f1(x))
    x=F.relu(self.f2(x))
    x=F.relu(self.f3(x))
    x=self.f4(x)
    return x

  def act(self,obs):
    state=T.tensor(obs).to(device)
    actions=self.forward(state)
    action=T.argmax(actions).item()

    return action


the problem is that you are receiving a tensor on the act function on the Network and then save it as a tensor
just remove the tensor in the action like this
  def act(self,obs):
    #state=T.tensor(obs).to(device)
    state=obs.to(device)
    actions=self.forward(state)
    action=T.argmax(actions).item()

