linear_regression_2.ipynb

{

"cells": [

  {

  "cell_type": "code",

  "execution_count": 5,

  "metadata": {},

  "outputs": [],

  "source": [

    "%matplotlib inline\n",

    "%reload_ext autoreload\n",

    "%autoreload 1\n",

    "%aimport d2lzh_pytorch\n",

    "import torch\n",

    "import torch.utils.data as Data\n",

    "from IPython import display\n",

    "from matplotlib import pyplot as plt\n",

    "import numpy as np\n",

    "import random\n",

    "\n",

    "num_inputs = 2\n",

    "num_examples = 1000\n",

    "true_w = [2, -3.4]\n",

    "true_b = 4.2\n",

    "features = torch.randn(num_examples, num_inputs, dtype=torch.float32)\n",

    "labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b\n",

    "labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()), dtype=torch.float32)\n",

    "batch_size = 10\n",

    "# 将训练数据的特征和标签组合\n",

    "dataset = Data.TensorDataset(features, labels)\n",

    "# 随机读取小批量\n",

    "data_iter = Data.DataLoader(dataset, batch_size, shuffle=True)"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 6,

  "metadata": {},

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "tensor([[-1.1650e+00,  3.9608e-01],\n",

      "        [-8.4998e-01, -1.5327e+00],\n",

      "        [-8.0021e-02,  5.4315e-01],\n",

      "        [-1.1686e+00,  1.3427e-03],\n",

      "        [ 5.8616e-01,  5.0903e-01],\n",

      "        [-1.2848e+00,  1.2465e+00],\n",

      "        [ 1.1104e-01,  1.7384e+00],\n",

      "        [-3.6663e-01,  2.8090e-01],\n",

      "        [ 2.9642e-02,  2.5131e-01],\n",

      "        [-1.2431e+00,  1.6079e+00]]) tensor([ 0.5214,  7.7054,  2.1807,  1.8651,  3.6499, -2.6117, -1.4787,  2.5015,\n",

      "        3.4148, -3.7473])\n"

    ]

    }

  ],

  "source": [

    "for X, y in data_iter:\n",

    "    print(X, y)\n",

    "    break"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 9,

  "metadata": {

    "tags": [

    "定义模型"

    ]

  },

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "Sequential(\n",

      "  (0): Linear(in_features=2, out_features=1, bias=True)\n",

      ")\n"

    ]

    }

  ],

  "source": [

    "import torch.nn as nn\n",

    "net = nn.Sequential(nn.Linear(num_inputs, 1))\n",

    "print(net) # 使用print可以打印出网络的结构"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 10,

  "metadata": {},

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "Parameter containing:\n",

      "tensor([[0.0303, 0.3476]], requires_grad=True)\n",

      "Parameter containing:\n",

      "tensor([0.2868], requires_grad=True)\n"

    ]

    }

  ],

  "source": [

    "for param in net.parameters():\n",

    "    print(param)"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 11,

  "metadata": {

    "tags": [

    "初始化模型参数"

    ]

  },

  "outputs": [

    {

    "data": {

      "text/plain": [

      "Parameter containing:\n",

      "tensor([0.], requires_grad=True)"

      ]

    },

    "execution_count": 11,

    "metadata": {},

    "output_type": "execute_result"

    }

  ],

  "source": [

    "from torch.nn import init\n",

    "# net[0]这样根据下标访问子模块的写法只有当net是个ModuleList或者Sequential实例时才可以\n",

    "init.normal_(net[0].weight, mean=0, std=0.01)\n",

    "init.constant_(net[0].bias, val=0)  # 也可以直接修改bias的data: net[0].bias.data.fill_(0)"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 12,

  "metadata": {

    "tags": [

    "定义损失函数"

    ]

  },

  "outputs": [],

  "source": [

    "loss = nn.MSELoss()"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 13,

  "metadata": {

    "tags": [

    "定义优化算法"

    ]

  },

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "SGD (\n",

      "Parameter Group 0\n",

      "    dampening: 0\n",

      "    lr: 0.03\n",

      "    momentum: 0\n",

      "    nesterov: False\n",

      "    weight_decay: 0\n",

      ")\n"

    ]

    }

  ],

  "source": [

    "import torch.optim as optim\n",

    "\n",

    "optimizer = optim.SGD(net.parameters(), lr=0.03)\n",

    "print(optimizer)"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 14,

  "metadata": {

    "tags": [

    "训练模型"

    ]

  },

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "epoch 1, loss: 0.000241\n",

      "epoch 2, loss: 0.000118\n",

      "epoch 3, loss: 0.000060\n"

    ]

    }

  ],

  "source": [

    "# 在使用Gluon训练模型时,我们通过调用optim实例的step函数来迭代模型参数。\n",

    "#按照小批量随机梯度下降的定义,我们在step函数中指明批量大小,从而对批量中样本梯度求平均。\n",

    "num_epochs = 3\n",

    "for epoch in range(1, num_epochs + 1):\n",

    "    for X, y in data_iter:\n",

    "        output = net(X)\n",

    "        l = loss(output, y.view(-1, 1))\n",

    "        optimizer.zero_grad() # 梯度清零,等价于net.zero_grad()\n",

    "        l.backward()\n",

    "        optimizer.step()\n",

    "    print('epoch %d, loss: %f' % (epoch, l.item()))"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": 15,

  "metadata": {},

  "outputs": [

    {

    "name": "stdout",

    "output_type": "stream",

    "text": [

      "[2, -3.4] Parameter containing:\n",

      "tensor([[ 1.9996, -3.3990]], requires_grad=True)\n",

      "4.2 Parameter containing:\n",

      "tensor([4.1998], requires_grad=True)\n"

    ]

    }

  ],

  "source": [

    "dense = net[0]\n",

    "print(true_w, dense.weight)\n",

    "print(true_b, dense.bias)"

  ]

  },

  {

  "cell_type": "code",

  "execution_count": null,

  "metadata": {},

  "outputs": [],

  "source": []

  }

],

"metadata": {

  "celltoolbar": "Tags",

  "kernelspec": {

  "display_name": "Python 3",

  "language": "python",

  "name": "python3"

  },

  "language_info": {

  "codemirror_mode": {

    "name": "ipython",

    "version": 3

  },

  "file_extension": ".py",

  "mimetype": "text/x-python",

  "name": "python",

  "nbconvert_exporter": "python",

  "pygments_lexer": "ipython3",

  "version": "3.6.10"

  }

},

"nbformat": 4,

"nbformat_minor": 4

}

你可能感兴趣的:(linear_regression_2.ipynb)