Ok, I changed the code but got the same error?
def cost(var1, Y):
res = myNetowrkt(var1)
loss = torch.sqrt(torch.mean((res-Y)**2))
return loss
X    = torch.FloatTensor([[0, 0, 0, 0]])
Y    = torch.FloatTensor([[0, 1, 1]])
var1 = torch.tensor(3.14159, requires_grad=True)
opt  = torch.optim.Adam([var1], lr=0.5)
loss = cost(var1, Y)
print(loss)
opt.zero_grad()
loss.backward(retain_graph=True)