Thank you very much for the attention.
And in the backward process, I have to clear the retain_graph = True
        for p in range(0, matrix_location.shape[0]):
            i_index = int(matrix_positions[p][0])
            j_index = int(matrix_positions[p][1])
            lamda = lamda_vector[p]*1.0
            epion = torch.rand(1).detach()
            A0_B[i_index][j_index] = lamda * (1 + 0.5 * epion)
        for index in range(len(A0_B)):
            transition_timing = (A0_B[index].clone() + external_inputs[step]) * 1.0
            # qval[step][index] = torch.matmul(sm(gm(sm(tempd.clone()))), tempd.clone())
            qstep[index] = torch.matmul(soft_max(gumbel_softmax(soft_max(transition_timing))), transition_timing)  
        qval[step] = qstep
        qlog = torch.log(qstep)
        # Calculate the backpropagation
        loss = criterion(qlog,torch.log(states[step]))
        sum_loss += loss.item()
        Distribution_Optimizer.zero_grad()
        loss.backward()  
#        print(one_hot_location.grad)
        Distribution_Optimizer.step()