import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
momentum = 0.9
step_size=0.05
n_epochs = 6000
n_hidden_1=32
n_hidden_2=32
d_out=1
mse_loss_fn=nn.MSELoss()
neural_network=nn.Sequential(
nn.Linear(d,n_hidden_1),
nn.Tanh(),
nn.Linear(n_hidden_1,n_hidden_2),
nn.Tanh(),
nn.Linear(n_hidden_2,d_out)
)
optim = torch.optim.SGD(neural_network.parameters(), lr=step_size,momentum=momentum)#可以直接调用SGD
for i in range(n_epochs):
y_hat=neural_network(X)
loss = mse_loss_fn(y_hat, y)
optim.zero_grad()
loss.backward()
optim.step()
if i % (n_epochs // 10) == 0:
print(’{},\t{:.2f}’.format(i, loss.item()))
X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
y_hat = neural_network(X_grid)
plt.scatter(X.numpy(),y.numpy())
plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)
plt.title(‘plot of $f(x)$ and $\hat{f}(x)$’)
plt.xlabel(’$x$’)
plt.ylabel(’$y$’)
plt.show()
This is my whole code. I checked that when the code<plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)> is commented out, there will be no error.But I can’t find the syntax error of this code.