Creating a PyTorch model to fit a curve

A_mour: <p>import torch<br>
import matplotlib<br>
matplotlib.use(‘TkAgg’)<br>
import matplotlib.pyplot as plt<br>
from torch import nn<br>
import os<br>
import numpy as np</p>
<h1><a name="h-1" class="anchor" href="#h-1"></a>从文件中加载数据</h1>
<p>def load_data_from_file(file_path):<br>
if not os.path.isfile(file_path):<br>
raise FileNotFoundError(f"File {file_path} does not exist.")<br>
data = np.loadtxt(file_path)<br>
x = torch.tensor(data[:, 0], dtype=torch.float32)  # 第一列<br>
Ps = torch.tensor(data[:, 1], dtype=torch.float32)  # 第二列<br>
y = torch.tensor(data[:, 2], dtype=torch.float32)  # 第三列<br>
return x, Ps, y</p>
<h1><a name="h-2" class="anchor" href="#h-2"></a>加载数据</h1>
<p>file_path = ‘D:/2-test/data.txt’  # 修改为实际路径<br>
x, Ps, y = load_data_from_file(file_path)</p>
<h1><a name="nnnnmodule-3" class="anchor" href="#nnnnmodule-3"></a>定义NN模型，继承自nn.Module</h1>
<p>class NeuralNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(NeuralNetwork, self).<strong>init</strong>()<br>
# 使用 nn.Parameter 定义待优化的参数<br>
self.N0 = nn.Parameter(torch.tensor(1.0, dtype=torch.float32), requires_grad=True)<br>
self.Pn0 = nn.Parameter(torch.tensor(1.0, dtype=torch.float32), requires_grad=True)<br>
self.P1 = nn.Parameter(torch.tensor(1.0, dtype=torch.float32), requires_grad=True)<br>
self.Pg0 = nn.Parameter(torch.tensor(1.0, dtype=torch.float32), requires_grad=True)<br>
self.eta = nn.Parameter(torch.tensor(1.0, dtype=torch.float32), requires_grad=True)<br>
self.Rn = torch.tensor(3.1e-10, dtype=torch.float32)  # 固定值<br>
self.Vv0 = nn.Parameter(torch.tensor(0.1, dtype=torch.float32), requires_grad=True)  # 初始孔洞体积</p>
<pre><code>def forward(self, Ps):
    dt = 1e-12
    # 计算成核率
    N_dot = torch.where(Ps &gt; self.Pn0, self.N0 * torch.exp((Ps - self.Pn0) / self.P1), torch.tensor(0.0, dtype=torch.float32))
    # 计算孔洞成核体积
    Delta_Vn = (8 * torch.pi * N_dot * dt * self.Rn ** 3)
    # 计算孔洞生长体积
    Vg = self.Vv0 * 3 / 4 * (Ps - self.Pg0) / self.eta * dt
    # 总体积
    Vv = Vg + Delta_Vn
    return Vv
</code></pre>
<h1><a name="h-4" class="anchor" href="#h-4"></a>创建模型</h1>
<p>model = NeuralNetwork()</p>
<h1><a name="h-5" class="anchor" href="#h-5"></a>定义损失函数和优化器</h1>
<p>loss_fn = nn.MSELoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)</p>
<h1><a name="h-6" class="anchor" href="#h-6"></a>训练过程</h1>
<p>batches = 5000<br>
plt.figure(“regression”)</p>
<p>def calculate_rmse(predictions, targets):<br>
return torch.sqrt(loss_fn(predictions, targets))</p>
<p>for i in range(batches):<br>
Vv_pred = model(Ps)<br>
loss = loss_fn(Vv_pred, y)<br>
rmse = calculate_rmse(Vv_pred, y)</p>
<pre><code>optimizer.zero_grad()
loss.backward()
optimizer.step()

if i % 100 == 0:
    loss_value = loss.item()
    rmse_value = rmse.item()
    print(f'loss: {loss_value} | RMSE: {rmse_value} | Batch: {i}')
    plt.cla()
    plt.plot(x.numpy(), y.numpy(), label='Actual')
    plt.plot(x.numpy(), Vv_pred.detach().numpy(), label='Predicted')
    plt.legend()
    plt.draw()
    plt.pause(0.01)
</code></pre>
<h1><a name="h-7" class="anchor" href="#h-7"></a>关闭交互模式</h1>
<p>plt.ioff()<br>
plt.cla()<br>
plt.plot(x.numpy(), y.numpy(), label=‘Actual’)<br>
plt.plot(x.numpy(), Vv_pred.detach().numpy(), label=‘Predicted’)<br>
plt.legend()<br>
plt.xlabel(‘x’)<br>
plt.ylabel(‘y’)<br>
plt.title(‘Final Regression Result’)<br>
plt.savefig(“final_plot.png”)<br>
plt.show()</p>
<h1><a name="h-8" class="anchor" href="#h-8"></a>打印模型参数</h1>
<p>print(“N0:”, model.N0.item())<br>
print(“Pn0:”, model.Pn0.item())<br>
print(“P1:”, model.P1.item())<br>
print(“Pg0:”, model.Pg0.item())<br>
print(“eta:”, model.eta.item())<br>
But this code doesn’t optimize the parameters, here’s the result：<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2300<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2400<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2500<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2600<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2700<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2800<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 2900<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3000<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3100<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3200<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3300<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3400<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3500<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3600<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3700<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3800<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 3900<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4000<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4100<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4200<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4300<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4400<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4500<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4600<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4700<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4800<br>
loss: 0.10101878643035889 | RMSE: 0.3178345263004303 | Batch: 4900<br>
N0: 1.0<br>
Pn0: 1.0<br>
P1: 1.0<br>
Pg0: 1.0<br>
eta: 1.0</p>
<p>进程已结束，退出代码 0</p>
<p>I would like to know how I can modify to optimize these parameters</p>

ptrblck: <p>Check if any trainable parameters have a valid gradient after the backward pass as it seems your training is stuck.</p>

A_mour: <p>Below is a partial gradient：Batch 9000<br>
N0: 1.0 | grad: -4.91339694994675e-34<br>
Pn0: 1000000.0 | grad: 4.9133966275359996e-39<br>
P1: 100000.0 | grad: 2.1853009929108123e-38<br>
Pg0: 1000000.0 | grad: 1.487217987650169e-10<br>
eta: 0.06056000292301178 | grad: -0.0023958131205290556<br>
Vv0: -0.00014509086031466722 | grad: -0.9999971985816956<br>
loss: 1.1067631244659424 | RMSE: 1.0520280599594116 | Batch: 9000<br>
Batch 9100<br>
N0: 1.0 | grad: -4.923352376907757e-34<br>
Pn0: 1000000.0 | grad: 4.9233528531250274e-39<br>
P1: 100000.0 | grad: 2.189718165930057e-38<br>
Pg0: 1000000.0 | grad: 1.4765616507261825e-10<br>
eta: 0.060560375452041626 | grad: -0.0023792905267328024<br>
Vv0: -0.00014409114373847842 | grad: -0.9999971389770508<br>
loss: 1.087465524673462 | RMSE: 1.042816162109375 | Batch: 9100<br>
Batch 9200<br>
N0: 1.0 | grad: -4.933487801441234e-34<br>
Pn0: 1000000.0 | grad: 4.9334870436190245e-39<br>
P1: 100000.0 | grad: 2.1942153530916146e-38<br>
Pg0: 1000000.0 | grad: 1.4659039260234152e-10<br>
eta: 0.06056074798107147 | grad: -0.0023627683985978365<br>
Vv0: -0.00014309142716228962 | grad: -0.9999972581863403<br>
loss: 1.0683401823043823 | RMSE: 1.0336054563522339 | Batch: 9200<br>
Batch 9300<br>
N0: 1.0 | grad: -4.9438073561445075e-34<br>
Pn0: 1000000.0 | grad: 4.9438062055103125e-39<br>
P1: 100000.0 | grad: 2.1987939556939495e-38<br>
Pg0: 1000000.0 | grad: 1.455244397208233e-10<br>
eta: 0.06056112051010132 | grad: -0.0023462465032935143<br>
Vv0: -0.00014209171058610082 | grad: -0.9999972581863403<br>
loss: 1.0493865013122559 | RMSE: 1.0243957042694092 | Batch: 9300<br>
Batch 9400<br>
N0: 1.0 | grad: -4.9543137960824625e-34<br>
Pn0: 1000000.0 | grad: 4.954314542694284e-39<br>
P1: 100000.0 | grad: 2.203456355944451e-38<br>
Pg0: 1000000.0 | grad: 1.4445832030585137e-10<br>
eta: 0.060561493039131165 | grad: -0.0023297248408198357<br>
Vv0: -0.00014109199400991201 | grad: -0.9999972581863403<br>
loss: 1.030604600906372 | RMSE: 1.0151870250701904 | Batch: 9400<br>
Batch 9500<br>
N0: 1.0 | grad: -4.965017223159676e-34<br>
Pn0: 1000000.0 | grad: 4.965016259066333e-39<br>
P1: 100000.0 | grad: 2.208205076180355e-38<br>
Pg0: 1000000.0 | grad: 1.4339203435742576e-10<br>
eta: 0.06056186556816101 | grad: -0.0023132034111768007<br>
Vv0: -0.0001400922774337232 | grad: -0.9999973177909851<br>
loss: 1.0119946002960205 | RMSE: 1.0059794187545776 | Batch: 9500<br>
Batch 9600<br>
N0: 1.0 | grad: -4.975917178198668e-34<br>
Pn0: 1000000.0 | grad: 4.97591836111878e-39<br>
P1: 100000.0 | grad: 2.2130417979598185e-38<br>
Pg0: 1000000.0 | grad: 1.4232554024218302e-10<br>
eta: 0.06056223809719086 | grad: -0.002296681981533766<br>
Vv0: -0.0001390925608575344 | grad: -0.9999973177909851<br>
loss: 0.9935567378997803 | RMSE: 0.9967731833457947 | Batch: 9600<br>
Batch 9700<br>
N0: 1.0 | grad: -4.987025140636458e-34<br>
Pn0: 1000000.0 | grad: 4.9870250527470185e-39<br>
P1: 100000.0 | grad: 2.2179703047886953e-38<br>
Pg0: 1000000.0 | grad: 1.412588657156988e-10<br>
eta: 0.0605626106262207 | grad: -0.002280161017552018<br>
Vv0: -0.0001380928442813456 | grad: -0.9999974370002747<br>
loss: 0.9752906560897827 | RMSE: 0.9875680804252625 | Batch: 9700<br>
Batch 9800<br>
N0: 1.0 | grad: -4.998342028828007e-34<br>
Pn0: 1000000.0 | grad: 4.99834334044337e-39<br>
P1: 100000.0 | grad: 2.2229918578356033e-38<br>
Pg0: 1000000.0 | grad: 1.4019198302239744e-10<br>
eta: 0.06056298315525055 | grad: -0.002263639820739627<br>
Vv0: -0.0001370931277051568 | grad: -0.9999974966049194<br>
loss: 0.9571964144706726 | RMSE: 0.9783641695976257 | Batch: 9800<br>
Batch 9900<br>
N0: 1.0 | grad: -5.009877485500413e-34<br>
Pn0: 1000000.0 | grad: 5.009878829401692e-39<br>
P1: 100000.0 | grad: 2.2281099603467033e-38<br>
Pg0: 1000000.0 | grad: 1.3912490604006678e-10<br>
eta: 0.060563355684280396 | grad: -0.0022471188567578793<br>
Vv0: -0.000136093411128968 | grad: -0.9999974370002747<br>
loss: 0.9392740726470947 | RMSE: 0.9691615104675293 | Batch: 9900</p>