Thanks, it seems that I forget to paste the whole codes here 
Here is another case
import torch
import torch.nn as nn
from torch.autograd import Variable

dtype = torch.FloatTensor
class SimpleNet(nn.Module):
    def __init__(self):
        super(SimpleNet, self).__init__()
        
        self.x_dim = 50
        self.hidden_size = 25
        self.w = Variable(torch.randn(self.x_dim, self.hidden_size).type(dtype), requires_grad=True)
        
        self.freeze_weights()
        
    def forward(self, x):
        return torch.mm(x, self.w)
    
    def freeze_weights(self):
        for p in self.modules():
            p.requires_grad = False

net = SimpleNet()
x = Variable(torch.randn(1, 25).type(dtype), requires_grad = True)
t = net(Variable(torch.randn(1, 50).type(dtype), requires_grad = False))

criterion = nn.MSELoss()
optimizer = torch.optim.Adam([x])

for i in range(5):
    loss = criterion(x, t)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

These code is similar to the one above, except that the target comes from SimpleNet.
But it would throw an error:
AssertionError: nn criterions don't compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients

And we still have the numpy codes here:
import numpy as np

M = 10
N = 5
x = np.random.randn(M)
y = np.random.randn(N)

w = np.random.randn(M, N)

learning_rate = 1e-2
for t in range(500):
    loss = np.square(x.dot(w) - y).sum()
    print(t, loss)
    # Back-propagate
    grad_y = 2.0 * (y - x.dot(w))
    # Update
    y -= learning_rate * grad_y

Is there anything wrong in my code?
Thanks.