{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "86b62c48-7f5e-4af5-bb60-056698ee7854",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jovyan/work/d2l/notebooks/d2l.py:119: SyntaxWarning: assertion is always true, perhaps remove parentheses?\n",
" assert(self, 'net'), 'Neural network is defined'\n",
"/home/jovyan/work/d2l/notebooks/d2l.py:123: SyntaxWarning: assertion is always true, perhaps remove parentheses?\n",
" assert(self, 'trainer'), 'trainer is not inited'\n"
]
}
],
"source": [
"import random\n",
"import torch\n",
"import d2l\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import torch.nn as nn\n",
"\n",
"\n",
"class LinearRegression(d2l.Module):\n",
" def __init__(self, lr):\n",
" super().__init__()\n",
" self.save_hyperparameters()\n",
" self.net = nn.LazyLinear(1)\n",
" self.net.weight.data.normal_(0, 0.01)\n",
" self.net.bias.data.fill_(0)\n",
" \n",
" def forward(self, X):\n",
" return self.net(X)\n",
" \n",
" def loss(self, y_hat, y):\n",
" fn = nn.MSELoss(reduction='mean')\n",
" return fn(y_hat, y)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "c9b5115f-e68c-4f4b-9c3e-a34c0fdc6a07",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(Parameter containing:\n",
" tensor([[ 1.9981, -3.3884]], requires_grad=True),\n",
" Parameter containing:\n",
" tensor([4.1935], requires_grad=True))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"