错误代码
错误的原因在于
y1 = 0.5*x*2-1.2*x
y2 = x**3
没有放到循环里面,没有随着 x 的优化而相应变化。
import torch
import numpy as np
import torch.optim as optim
torch.autograd.set_detect_anomaly(True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
x = torch.tensor([1.0, 2.0, 3.0,4.5], dtype=torch.float32, requires_grad=True, device=device)
y_GT= torch