{ "cells": [ { "cell_type": "markdown", "id": "6ca179ca-ae81-4cf1-955a-9bf9ea033107", "metadata": {}, "source": [ "# 逻辑回归的数学模型" ] }, { "cell_type": "markdown", "id": "acf7f852-d546-4eed-b00d-eaf0e42a98d5", "metadata": {}, "source": [ "- $y_{\\_} = sigmoid(xW + b)$" ] }, { "cell_type": "markdown", "id": "42ae1498-cb86-4263-8dce-7cd40cb818c5", "metadata": {}, "source": [ "- $l = loss(y, y_{\\_})$" ] }, { "cell_type": "markdown", "id": "022c8cd6-d615-40c2-948f-2eaf59d7493c", "metadata": {}, "source": [ "# 数据集(鸢尾花Iris)" ] }, { "cell_type": "code", "execution_count": 7, "id": "f66c9221-f4e0-4ee7-8866-e1605d00dd13", "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_iris\n", "import torch\n", "data, target = load_iris(return_X_y=True)\n", "# data, target\n", "x = data[50:150]\n", "y = target[:100]\n", "\n", "x = torch.Tensor(x)\n", "y = torch.Tensor(y).view(100, 1)" ] }, { "cell_type": "code", "execution_count": 8, "id": "3dc517ff-7cc5-470d-8009-62a894a006fb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[6.4000, 3.2000, 4.5000, 1.5000],\n", " [6.9000, 3.1000, 4.9000, 1.5000],\n", " [5.5000, 2.3000, 4.0000, 1.3000],\n", " [6.5000, 2.8000, 4.6000, 1.5000]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x [1:5]" ] }, { "cell_type": "code", "execution_count": 9, "id": "46231e73-6a41-411c-88dd-f86900642296", "metadata": {}, "outputs": [], "source": [ "# y" ] }, { "cell_type": "markdown", "id": "0e559c14-4f07-45eb-81bd-dcceee254ea7", "metadata": {}, "source": [ "# 使用梯度下降思维进行机器学习(分类)" ] }, { "cell_type": "markdown", "id": "d937048f-20c4-44d2-a166-9fa84a7147f6", "metadata": {}, "source": [ "- 随机一个w,b\n", "- 使用样本求预测值\n", "- 使用预测值与真实值求误差\n", "- 对误差函数求导\n", "- 使用导数 * 因子(学习率)更新w, b\n", "- 反复循环\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "eee5279b-6a81-4d85-96b7-98d888af5cb7", "metadata": {}, "outputs": [], "source": [ "w1 = torch.randn(1, 4)\n", "b1 = torch.randn(1)\n", "\n", "w1.requires_grad = True\n", "b1.requires_grad = True\n", "\n", "# w1 = torch.randn(12, 4)\n", "# b1 = torch.randn(12)\n", "\n", "# w1.requires_grad = True\n", "# b1.requires_grad = True\n", "\n", "# w2 = torch.randn(9, 12)\n", "# b2 = torch.randn(9)\n", "\n", "# w2.requires_grad = True\n", "# b2.requires_grad = True\n", "\n", "# w3 = torch.randn(1, 9)\n", "# b3 = torch.randn(1)\n", "\n", "# w3.requires_grad = True\n", "# b3.requires_grad = True" ] }, { "cell_type": "code", "execution_count": 14, "id": "ec28048f-f04d-49a6-9ec7-3f3f2e1ea37b", "metadata": {}, "outputs": [], "source": [ "epoch = 100000\n", "lr = 0.001" ] }, { "cell_type": "code", "execution_count": 15, "id": "1d4525da-fe4a-4710-8d18-457df68f554f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "损失值:0.922623,\t准确率: 50.00%\n", "损失值:0.437713,\t准确率: 83.00%\n", "损失值:0.385350,\t准确率: 88.00%\n", "损失值:0.347146,\t准确率: 91.00%\n", "损失值:0.318071,\t准确率: 91.00%\n", "损失值:0.295188,\t准确率: 92.00%\n", "损失值:0.276688,\t准确率: 93.00%\n", "损失值:0.261402,\t准确率: 95.00%\n", "损失值:0.248542,\t准确率: 95.00%\n", "损失值:0.237560,\t准确率: 95.00%\n", "损失值:0.228061,\t准确率: 95.00%\n", "损失值:0.219754,\t准确率: 95.00%\n", "损失值:0.212422,\t准确率: 95.00%\n", "损失值:0.205897,\t准确率: 95.00%\n", "损失值:0.200048,\t准确率: 96.00%\n", "损失值:0.194771,\t准确率: 96.00%\n", "损失值:0.189983,\t准确率: 96.00%\n", "损失值:0.185618,\t准确率: 96.00%\n", "损失值:0.181617,\t准确率: 97.00%\n", "损失值:0.177934,\t准确率: 97.00%\n" ] } ], "source": [ "for e in range(epoch):\n", " # 预测\n", " # y_ = w1 @ x.T + b1\n", " y_ = torch.nn.functional.linear(input=x, weight=w1, bias=b1)\n", " # y_ = torch.nn.functional.relu(y_)\n", " # y_ = torch.nn.functional.linear(input=y_, weight=w2, bias=b2)\n", " # y_ = torch.nn.functional.relu(y_)\n", " # y_ = torch.nn.functional.linear(input=y_, weight=w3, bias=b3)\n", " y_ = torch.sigmoid(y_)\n", " # 求误差\n", " loss = torch.nn.functional.binary_cross_entropy(y_, y)\n", " # 自动求导\n", " loss.backward()\n", " with torch.no_grad():\n", " # 使用梯度更新权重系数\n", " w1 -= w1.grad * lr\n", " b1 -= b1.grad * lr\n", " # 梯度清零\n", " w1.grad.zero_()\n", " b1.grad.zero_()\n", "\n", " # 统计下预测效果\n", " y_ [y_ >=0.5] = 1\n", " y_ [y_ <0.5] = 0\n", "\n", " correct_rate = (y_ == y).float().mean()\n", " if e % 5000 == 0:\n", " print(F\"损失值:{loss:8.6f},\\t准确率:{correct_rate*100: 5.2f}%\")" ] }, { "cell_type": "markdown", "id": "c3a81e87-e742-49f3-9d0c-e5d046252c46", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "6bdbf6e4-6397-46a7-bcc6-133e4ce91c2b", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "3bc7a505-0529-4d93-9551-5aee5efb553a", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.7" } }, "nbformat": 4, "nbformat_minor": 5 }