Thank you.
I fixed the function named forward like below.
def forward(coordinate, w, layer_no):#layer_no from 0 to '(the length of eta_n) - 1'
    row1 = torch.Tensor([[1, delta_eta[layer_no]]])
    row2_col1 = torch.Tensor([- delta_eta[layer_no]])
    row2_col2 = w
    row2 = torch.cat([row2_col1, row2_col2], dim = 0).reshape(1, -1)
    matrix = torch.cat([row1, row2], dim = 0)

    return acti1(matrix.mm(coordinate), layer_no)
    

Also, I fixed
w = torch.randn(9, requires_grad = True)

into
w = torch.randn((9, 1) requires_grad = True)

in accordance with the change in function named forward. And the error was solved.
However, another error which is ‘RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time’ appeared. I reconstructed all codes to avoid the error what I can’t find a solution and the result is the simplified codes above. However the error appeared again. Would you help me again? I have done many googlings, but I couldn’t understand. And specifying retain_graph = True resulted in exponentially increasing operation time.