routine
This commit is contained in:
		
							
								
								
									
										327
									
								
								lab/7_Muticlass.ipynb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										327
									
								
								lab/7_Muticlass.ipynb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,327 @@
 | 
			
		||||
{
 | 
			
		||||
 "cells": [
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "markdown",
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# 多分类问题 - 手写数字识别\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "## 数据集\n",
 | 
			
		||||
    "- minst数据集(手写数字数据集)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "## 激活函数\n",
 | 
			
		||||
    "- softmax\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "## 损失函数\n",
 | 
			
		||||
    "- 交叉熵\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "## 优化器\n",
 | 
			
		||||
    "- 梯度下降\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "## 模型\n",
 | 
			
		||||
    "- 全连接层\n"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 7,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# 导库\n",
 | 
			
		||||
    "import tensorflow as tf\n",
 | 
			
		||||
    "from tensorflow.keras import Sequential\n",
 | 
			
		||||
    "from tensorflow.keras.layers import Dense\n",
 | 
			
		||||
    "from tensorflow.keras.losses import SparseCategoricalCrossentropy"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 10,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/html": [
 | 
			
		||||
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential_2\"</span>\n",
 | 
			
		||||
       "</pre>\n"
 | 
			
		||||
      ],
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "\u001b[1mModel: \"sequential_2\"\u001b[0m\n"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "display_data"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/html": [
 | 
			
		||||
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
 | 
			
		||||
       "┃<span style=\"font-weight: bold\"> Layer (type)                    </span>┃<span style=\"font-weight: bold\"> Output Shape           </span>┃<span style=\"font-weight: bold\">       Param # </span>┃\n",
 | 
			
		||||
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
 | 
			
		||||
       "│ dense_4 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                 │ ?                      │   <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
 | 
			
		||||
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
 | 
			
		||||
       "│ dense_5 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                 │ ?                      │   <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
 | 
			
		||||
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
 | 
			
		||||
       "│ dense_6 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                 │ ?                      │   <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
 | 
			
		||||
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
 | 
			
		||||
       "</pre>\n"
 | 
			
		||||
      ],
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
 | 
			
		||||
       "┃\u001b[1m \u001b[0m\u001b[1mLayer (type)                   \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape          \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m      Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
 | 
			
		||||
       "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
 | 
			
		||||
       "│ dense_4 (\u001b[38;5;33mDense\u001b[0m)                 │ ?                      │   \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
 | 
			
		||||
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
 | 
			
		||||
       "│ dense_5 (\u001b[38;5;33mDense\u001b[0m)                 │ ?                      │   \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
 | 
			
		||||
       "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
 | 
			
		||||
       "│ dense_6 (\u001b[38;5;33mDense\u001b[0m)                 │ ?                      │   \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
 | 
			
		||||
       "└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "display_data"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/html": [
 | 
			
		||||
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
 | 
			
		||||
       "</pre>\n"
 | 
			
		||||
      ],
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "display_data"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/html": [
 | 
			
		||||
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
 | 
			
		||||
       "</pre>\n"
 | 
			
		||||
      ],
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "display_data"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/html": [
 | 
			
		||||
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
 | 
			
		||||
       "</pre>\n"
 | 
			
		||||
      ],
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "display_data"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "None\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# TEST\n",
 | 
			
		||||
    "model = Sequential([Dense(units=25,activation='relu'),\n",
 | 
			
		||||
    "                    Dense(units=15,activation='relu'),\n",
 | 
			
		||||
    "                    Dense(units=10,activation='softmax')])\n",
 | 
			
		||||
    "model.compile(loss=SparseCategoricalCrossentropy())\n",
 | 
			
		||||
    "# 输出模型\n",
 | 
			
		||||
    "print(model.summary())"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 8,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Epoch 1/5\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stderr",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "2025-01-21 11:03:07.974903: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m16s\u001b[0m 8ms/step - accuracy: 0.8340 - loss: 0.5514\n",
 | 
			
		||||
      "Epoch 2/5\n",
 | 
			
		||||
      "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m16s\u001b[0m 8ms/step - accuracy: 0.8933 - loss: 0.3836\n",
 | 
			
		||||
      "Epoch 3/5\n",
 | 
			
		||||
      "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m15s\u001b[0m 8ms/step - accuracy: 0.8902 - loss: 0.4045\n",
 | 
			
		||||
      "Epoch 4/5\n",
 | 
			
		||||
      "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m15s\u001b[0m 8ms/step - accuracy: 0.8889 - loss: 0.4049\n",
 | 
			
		||||
      "Epoch 5/5\n",
 | 
			
		||||
      "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m15s\u001b[0m 8ms/step - accuracy: 0.8840 - loss: 0.4177\n",
 | 
			
		||||
      "\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 4ms/step - accuracy: 0.9039 - loss: 0.3451\n"
 | 
			
		||||
     ]
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
     "data": {
 | 
			
		||||
      "text/plain": [
 | 
			
		||||
       "[0.30404922366142273, 0.9150999784469604]"
 | 
			
		||||
      ]
 | 
			
		||||
     },
 | 
			
		||||
     "execution_count": 8,
 | 
			
		||||
     "metadata": {},
 | 
			
		||||
     "output_type": "execute_result"
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# 官方实例\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "import tensorflow as tf\n",
 | 
			
		||||
    "mnist = tf.keras.datasets.mnist\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n",
 | 
			
		||||
    "x_train, x_test = x_train / 255.0, x_test / 255.0\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "model = tf.keras.models.Sequential([\n",
 | 
			
		||||
    "  tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
 | 
			
		||||
    "  tf.keras.layers.Dense(128, activation='relu'),\n",
 | 
			
		||||
    "  tf.keras.layers.Dropout(0.2),\n",
 | 
			
		||||
    "  tf.keras.layers.Dense(10, activation='softmax')\n",
 | 
			
		||||
    "])\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n",
 | 
			
		||||
    "  loss=SparseCategoricalCrossentropy(),\n",
 | 
			
		||||
    "  metrics=['accuracy'])\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "model.fit(x_train, y_train, epochs=5)\n",
 | 
			
		||||
    "model.evaluate(x_test, y_test)"
 | 
			
		||||
   ]
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
   "cell_type": "code",
 | 
			
		||||
   "execution_count": 9,
 | 
			
		||||
   "metadata": {},
 | 
			
		||||
   "outputs": [
 | 
			
		||||
    {
 | 
			
		||||
     "name": "stdout",
 | 
			
		||||
     "output_type": "stream",
 | 
			
		||||
     "text": [
 | 
			
		||||
      "Epoch 1/5 - Loss: 0.4415533185513543\n",
 | 
			
		||||
      "Epoch 2/5 - Loss: 0.24540251612599726\n",
 | 
			
		||||
      "Epoch 3/5 - Loss: 0.19663310029716696\n",
 | 
			
		||||
      "Epoch 4/5 - Loss: 0.17048093510557338\n",
 | 
			
		||||
      "Epoch 5/5 - Loss: 0.16070798563876196\n",
 | 
			
		||||
      "Accuracy: 95.38%\n"
 | 
			
		||||
     ]
 | 
			
		||||
    }
 | 
			
		||||
   ],
 | 
			
		||||
   "source": [
 | 
			
		||||
    "# 用torch实现\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "import torch\n",
 | 
			
		||||
    "import torch.nn as nn\n",
 | 
			
		||||
    "import torch.optim as optim\n",
 | 
			
		||||
    "from torchvision import datasets, transforms\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 数据预处理\n",
 | 
			
		||||
    "transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 加载数据集\n",
 | 
			
		||||
    "trainset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)\n",
 | 
			
		||||
    "trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "testset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)\n",
 | 
			
		||||
    "testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 定义模型\n",
 | 
			
		||||
    "class SimpleNet(nn.Module):\n",
 | 
			
		||||
    "    def __init__(self):\n",
 | 
			
		||||
    "        super(SimpleNet, self).__init__()\n",
 | 
			
		||||
    "        self.flatten = nn.Flatten()\n",
 | 
			
		||||
    "        self.fc1 = nn.Linear(28 * 28, 128)\n",
 | 
			
		||||
    "        self.dropout = nn.Dropout(0.2)\n",
 | 
			
		||||
    "        self.fc2 = nn.Linear(128, 10)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "    def forward(self, x):\n",
 | 
			
		||||
    "        x = self.flatten(x)\n",
 | 
			
		||||
    "        x = torch.relu(self.fc1(x))\n",
 | 
			
		||||
    "        x = self.dropout(x)\n",
 | 
			
		||||
    "        x = self.fc2(x)\n",
 | 
			
		||||
    "        return x\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "model = SimpleNet()\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "if torch.backends.mps.is_available():\n",
 | 
			
		||||
    "    device = torch.device(\"mps\")\n",
 | 
			
		||||
    "else:\n",
 | 
			
		||||
    "    device = torch.device(\"cpu\")\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "model.to(device)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 定义损失函数和优化器\n",
 | 
			
		||||
    "criterion = nn.CrossEntropyLoss()\n",
 | 
			
		||||
    "optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 训练模型\n",
 | 
			
		||||
    "epochs = 5\n",
 | 
			
		||||
    "for epoch in range(epochs):\n",
 | 
			
		||||
    "    running_loss = 0\n",
 | 
			
		||||
    "    for images, labels in trainloader:\n",
 | 
			
		||||
    "        images, labels = images.to(device), labels.to(device)  # 将数据移动到设备上\n",
 | 
			
		||||
    "        optimizer.zero_grad()\n",
 | 
			
		||||
    "        output = model(images)\n",
 | 
			
		||||
    "        loss = criterion(output, labels)\n",
 | 
			
		||||
    "        loss.backward()\n",
 | 
			
		||||
    "        optimizer.step()\n",
 | 
			
		||||
    "        running_loss += loss.item()\n",
 | 
			
		||||
    "    print(f\"Epoch {epoch+1}/{epochs} - Loss: {running_loss/len(trainloader)}\")\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "# 测试模型\n",
 | 
			
		||||
    "correct = 0\n",
 | 
			
		||||
    "total = 0\n",
 | 
			
		||||
    "with torch.no_grad():\n",
 | 
			
		||||
    "    for images, labels in testloader:\n",
 | 
			
		||||
    "        images, labels = images.to(device), labels.to(device)  # 将数据移动到设备上\n",
 | 
			
		||||
    "        output = model(images)\n",
 | 
			
		||||
    "        _, predicted = torch.max(output, 1)\n",
 | 
			
		||||
    "        total += labels.size(0)\n",
 | 
			
		||||
    "        correct += (predicted == labels).sum().item()\n",
 | 
			
		||||
    "\n",
 | 
			
		||||
    "print(f\"Accuracy: {100 * correct / total}%\")"
 | 
			
		||||
   ]
 | 
			
		||||
  }
 | 
			
		||||
 ],
 | 
			
		||||
 "metadata": {
 | 
			
		||||
  "kernelspec": {
 | 
			
		||||
   "display_name": "ail",
 | 
			
		||||
   "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.16"
 | 
			
		||||
  }
 | 
			
		||||
 },
 | 
			
		||||
 "nbformat": 4,
 | 
			
		||||
 "nbformat_minor": 2
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user