|
365 | 365 | " possible_b.shape[0]\n", |
366 | 366 | "))\n", |
367 | 367 | "\n", |
368 | | - "for i, m_hat in enumerate(possible_m):\n", |
369 | | - " for j, b_hat in enumerate(possible_b):\n", |
370 | | - " line.layer.weight[0, 0] = m_hat\n", |
371 | | - " line.layer.bias[0] = b_hat\n", |
372 | | - " y_hat = line(to_tensor(x))\n", |
373 | | - " _loss = (y_hat - to_tensor(y)).abs().mean()\n", |
374 | | - " loss[i, j] = _loss\n", |
| 368 | + "with torch.no_grad():\n", |
| 369 | + " for i, m_hat in enumerate(possible_m):\n", |
| 370 | + " for j, b_hat in enumerate(possible_b):\n", |
| 371 | + " line.layer.weight[0, 0] = m_hat\n", |
| 372 | + " line.layer.bias[0] = b_hat\n", |
| 373 | + " y_hat = line(to_tensor(x))\n", |
| 374 | + " _loss = (y_hat - to_tensor(y)).abs().mean()\n", |
| 375 | + " loss[i, j] = _loss\n", |
375 | 376 | "\n", |
376 | 377 | "plt.title('2D Visualization of Loss Landscape')\n", |
377 | 378 | "plt.pcolormesh(possible_m, possible_b, loss, shading='auto')\n", |
|
442 | 443 | } |
443 | 444 | ], |
444 | 445 | "source": [ |
445 | | - "line.layer.weight[0, 0] = m_hat\n", |
446 | | - "line.layer.bias[0] = b_hat\n", |
| 446 | + "with torch.no_grad():\n", |
| 447 | + " line.layer.weight[0, 0] = m_hat\n", |
| 448 | + " line.layer.bias[0] = b_hat\n", |
447 | 449 | "\n", |
448 | | - "y_hat = line(to_tensor(x))\n", |
| 450 | + " y_hat = line(to_tensor(x))\n", |
449 | 451 | "\n", |
450 | 452 | "plt.title(\"Training data + network predictions\")\n", |
451 | 453 | "plt.scatter(x, y, label='Training data')\n", |
|
1014 | 1016 | "name": "python", |
1015 | 1017 | "nbconvert_exporter": "python", |
1016 | 1018 | "pygments_lexer": "ipython3", |
1017 | | - "version": "3.8.5" |
| 1019 | + "version": "3.8.8" |
1018 | 1020 | } |
1019 | 1021 | }, |
1020 | 1022 | "nbformat": 4, |
|
0 commit comments