ValueError: can't optimize a non-leaf Tensor” for multidimensional tensor

i have output x from network 1 and another tensor y (with require grad = True) ! now i want to concatenate tensor x and y to feed in network2 but when optimizing it raises an error and y.grad tensor is full of zeroes!!
how to solve this problem??

Based on the error it seems you are trying to pass a non-leaf tensor (e.g. an output activation) to the optimizer, which is not supported. Could this be the case?