1
0
This commit is contained in:
2025-01-25 22:33:14 +08:00
parent 81367980c1
commit f138818abc
14 changed files with 602 additions and 371 deletions

4
.gitignore vendored
View File

@@ -8,4 +8,6 @@
**/.ipynb_checkpoints **/.ipynb_checkpoints
.vscode .vscode
lab/data

View File

@@ -1,5 +1,7 @@
# AI-Learning # AI-Learning
- 所有的ipynb结果均为最优保存
## BASE ENV ## BASE ENV
## MAC ## MAC

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -2,20 +2,70 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/wolves/Downloads/project/python/AI-learning/lab\n"
]
}
],
"source": [
"import os\n",
"print(os.getcwd())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-20T09:22:08.756378Z",
"start_time": "2025-01-20T09:22:08.738301Z"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# 导库\n", "# 导库\n",
"from sklearn.datasets import make_classification\n",
"import tensorflow as tf\n", "import tensorflow as tf\n",
"import numpy as np\n", "import numpy as np\n",
"import matplotlib.pyplot as plt\n", "import matplotlib.pyplot as plt\n",
"from matplotlib.lines import Line2D\n", "from matplotlib.lines import Line2D\n",
"from matplotlib.colors import ListedColormap\n", "from matplotlib.colors import ListedColormap\n",
"from tensorflow.keras.layers import Dense\n", "from tensorflow.keras.layers import Dense\n",
"\n", "from sklearn.metrics import accuracy_score\n",
"# 使用GPU 6\n", "from tensorflow.keras.models import Sequential"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# 限制每个GPU使用4GB显存 通用gpu启用\n",
"gpus = tf.config.experimental.list_physical_devices('GPU')\n",
"if gpus:\n",
" try:\n",
" for gpu in gpus:\n",
" tf.config.experimental.set_virtual_device_configuration(\n",
" gpu,\n",
" [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)] # 限制每个GPU使用4GB显存\n",
" )\n",
" except RuntimeError as e:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 使用GPU 6 cuda启用\n",
"gpus = tf.config.experimental.list_physical_devices('GPU')\n", "gpus = tf.config.experimental.list_physical_devices('GPU')\n",
"if gpus:\n", "if gpus:\n",
" try:\n", " try:\n",
@@ -85,13 +135,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 17,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"模型运行在GPU设备: /device:GPU:0\n",
"Epoch 1/100\n" "Epoch 1/100\n"
] ]
}, },
@@ -99,287 +150,255 @@
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n", "2025-01-20 17:58:23.192972: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n", "2025-01-20 17:58:23.192998: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
"I0000 00:00:1736497931.531534 21216 service.cc:146] XLA service 0x7f49f4954650 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:\n",
"I0000 00:00:1736497931.531557 21216 service.cc:154] StreamExecutor device (0): NVIDIA A800 80GB PCIe, Compute Capability 8.0\n",
"2025-01-10 08:32:11.540274: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.\n",
"2025-01-10 08:32:11.571961: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:531] Loaded cuDNN version 90600\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"I0000 00:00:1736497931.646086 21216 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n"
] ]
}, },
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"2/2 [==============================] - 2s 10ms/step - loss: 0.7364\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 103ms/step - loss: 60.6063\n",
"Epoch 2/100\n", "Epoch 2/100\n",
"2/2 [==============================] - 0s 5ms/step - loss: 0.7210\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 28.2659\n",
"Epoch 3/100\n", "Epoch 3/100\n",
"2/2 [==============================] - 0s 6ms/step - loss: 0.7066\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 1.4893\n",
"Epoch 4/100\n", "Epoch 4/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6929\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 4.0465\n",
"Epoch 5/100\n", "Epoch 5/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6786\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 6.5380\n",
"Epoch 6/100\n", "Epoch 6/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6668\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 8.9314\n",
"Epoch 7/100\n", "Epoch 7/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6531\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 8.9564\n",
"Epoch 8/100\n", "Epoch 8/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6411\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 10.6051\n",
"Epoch 9/100\n" "Epoch 9/100\n",
] "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 12.0071\n",
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n",
"'+ptx85' is not a recognized feature for this target (ignoring feature)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"2/2 [==============================] - 0s 7ms/step - loss: 0.6245\n",
"Epoch 10/100\n", "Epoch 10/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.6015\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 10.6521\n",
"Epoch 11/100\n", "Epoch 11/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.5786\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 14.1705\n",
"Epoch 12/100\n", "Epoch 12/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.5240\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 13.1806\n",
"Epoch 13/100\n", "Epoch 13/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.5058\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 12.1059\n",
"Epoch 14/100\n", "Epoch 14/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4971\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 13.4850\n",
"Epoch 15/100\n", "Epoch 15/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4868\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 12.3003\n",
"Epoch 16/100\n", "Epoch 16/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4798\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 11.7990\n",
"Epoch 17/100\n", "Epoch 17/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4722\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 10.3683\n",
"Epoch 18/100\n", "Epoch 18/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4654\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 9.7598 \n",
"Epoch 19/100\n", "Epoch 19/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4600\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 10.2508\n",
"Epoch 20/100\n", "Epoch 20/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4560\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 8.2063\n",
"Epoch 21/100\n", "Epoch 21/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4506\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 6.3501\n",
"Epoch 22/100\n", "Epoch 22/100\n",
"2/2 [==============================] - 0s 6ms/step - loss: 0.4460\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 6.1186\n",
"Epoch 23/100\n", "Epoch 23/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4432\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 4.8664\n",
"Epoch 24/100\n", "Epoch 24/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4404\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 4.0431\n",
"Epoch 25/100\n", "Epoch 25/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4380\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 3.5386\n",
"Epoch 26/100\n", "Epoch 26/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4349\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 2.7600\n",
"Epoch 27/100\n", "Epoch 27/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4335\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 1.3724\n",
"Epoch 28/100\n", "Epoch 28/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4313\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.5045\n",
"Epoch 29/100\n", "Epoch 29/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4301\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 1.9855\n",
"Epoch 30/100\n", "Epoch 30/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4293\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 1.0879\n",
"Epoch 31/100\n", "Epoch 31/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4278\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.6851\n",
"Epoch 32/100\n", "Epoch 32/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4270\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 1.2047\n",
"Epoch 33/100\n", "Epoch 33/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4259\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 1.5541\n",
"Epoch 34/100\n", "Epoch 34/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4257\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 1.2758\n",
"Epoch 35/100\n", "Epoch 35/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4249\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 1.3003\n",
"Epoch 36/100\n", "Epoch 36/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4245\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.8837\n",
"Epoch 37/100\n", "Epoch 37/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4247\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.5647\n",
"Epoch 38/100\n", "Epoch 38/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4239\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.6855\n",
"Epoch 39/100\n", "Epoch 39/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4238\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.5609\n",
"Epoch 40/100\n", "Epoch 40/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4238\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 0.4828\n",
"Epoch 41/100\n", "Epoch 41/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4232\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 29ms/step - loss: 0.6055\n",
"Epoch 42/100\n", "Epoch 42/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4232\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 65ms/step - loss: 0.5680\n",
"Epoch 43/100\n", "Epoch 43/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4232\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 0.3890\n",
"Epoch 44/100\n", "Epoch 44/100\n",
"2/2 [==============================] - 0s 8ms/step - loss: 0.4229\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4543\n",
"Epoch 45/100\n", "Epoch 45/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4229\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4878\n",
"Epoch 46/100\n", "Epoch 46/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4230\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 0.4287\n",
"Epoch 47/100\n", "Epoch 47/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4229\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4933\n",
"Epoch 48/100\n", "Epoch 48/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 27ms/step - loss: 0.5080\n",
"Epoch 49/100\n", "Epoch 49/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4485\n",
"Epoch 50/100\n", "Epoch 50/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4195\n",
"Epoch 51/100\n", "Epoch 51/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4215\n",
"Epoch 52/100\n", "Epoch 52/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4477\n",
"Epoch 53/100\n", "Epoch 53/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 32ms/step - loss: 0.4759\n",
"Epoch 54/100\n", "Epoch 54/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4077\n",
"Epoch 55/100\n", "Epoch 55/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4073\n",
"Epoch 56/100\n", "Epoch 56/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.3936\n",
"Epoch 57/100\n", "Epoch 57/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4006\n",
"Epoch 58/100\n", "Epoch 58/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4254\n",
"Epoch 59/100\n", "Epoch 59/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4196\n",
"Epoch 60/100\n", "Epoch 60/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4221\n",
"Epoch 61/100\n", "Epoch 61/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4271\n",
"Epoch 62/100\n", "Epoch 62/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4239\n",
"Epoch 63/100\n", "Epoch 63/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.3905\n",
"Epoch 64/100\n", "Epoch 64/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4415\n",
"Epoch 65/100\n", "Epoch 65/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 66ms/step - loss: 0.4230\n",
"Epoch 66/100\n", "Epoch 66/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 0.3950\n",
"Epoch 67/100\n", "Epoch 67/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4236\n",
"Epoch 68/100\n", "Epoch 68/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4291\n",
"Epoch 69/100\n", "Epoch 69/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4066\n",
"Epoch 70/100\n", "Epoch 70/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 30ms/step - loss: 0.4228\n",
"Epoch 71/100\n", "Epoch 71/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.3788\n",
"Epoch 72/100\n", "Epoch 72/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4248\n",
"Epoch 73/100\n", "Epoch 73/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4265\n",
"Epoch 74/100\n", "Epoch 74/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4165\n",
"Epoch 75/100\n", "Epoch 75/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4262\n",
"Epoch 76/100\n", "Epoch 76/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4238\n",
"Epoch 77/100\n", "Epoch 77/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4055\n",
"Epoch 78/100\n", "Epoch 78/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4483\n",
"Epoch 79/100\n", "Epoch 79/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4379\n",
"Epoch 80/100\n", "Epoch 80/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4697\n",
"Epoch 81/100\n", "Epoch 81/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4096\n",
"Epoch 82/100\n", "Epoch 82/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4069\n",
"Epoch 83/100\n", "Epoch 83/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4518\n",
"Epoch 84/100\n", "Epoch 84/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4145\n",
"Epoch 85/100\n", "Epoch 85/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.3853\n",
"Epoch 86/100\n", "Epoch 86/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 60ms/step - loss: 0.4064\n",
"Epoch 87/100\n", "Epoch 87/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4533\n",
"Epoch 88/100\n", "Epoch 88/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4798\n",
"Epoch 89/100\n", "Epoch 89/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4093\n",
"Epoch 90/100\n", "Epoch 90/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 0.4176\n",
"Epoch 91/100\n", "Epoch 91/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 0.4351\n",
"Epoch 92/100\n", "Epoch 92/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4422\n",
"Epoch 93/100\n", "Epoch 93/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 26ms/step - loss: 0.4550\n",
"Epoch 94/100\n", "Epoch 94/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4387\n",
"Epoch 95/100\n", "Epoch 95/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step - loss: 0.4847\n",
"Epoch 96/100\n", "Epoch 96/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4777\n",
"Epoch 97/100\n", "Epoch 97/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4228\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4309\n",
"Epoch 98/100\n", "Epoch 98/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 25ms/step - loss: 0.4221\n",
"Epoch 99/100\n", "Epoch 99/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 23ms/step - loss: 0.4159\n",
"Epoch 100/100\n", "Epoch 100/100\n",
"2/2 [==============================] - 0s 7ms/step - loss: 0.4227\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 22ms/step - loss: 0.3821\n",
"2/2 [==============================] - 0s 2ms/step\n", "\u001b[1m2/2\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 34ms/step\n",
"模型的精准度: 0.85\n" "模型的精准度: 0.85\n"
] ]
} }
], ],
"source": [ "source": [
"# 输出模型当时是在什么设备上运行\n",
"device_name = tf.test.gpu_device_name()\n",
"if device_name:\n",
" print(f\"模型运行在GPU设备: {device_name}\")\n",
"else:\n",
" print(\"模型运行在CPU设备\")\n",
"\n", "\n",
"# 写法1\n",
"# model = tf.keras.Sequential()\n",
"# layer_1 = Dense(units=3,activation='sigmoid')\n",
"# layer_2 = Dense(units=1,activation='sigmoid')\n",
"# model.add(layer_1)\n",
"# model.add(layer_2)\n",
"\n",
"#写法2\n",
"# layer_1 = Dense(units=3,activation='sigmoid')\n",
"# layer_2 = Dense(units=1,activation='sigmoid')\n",
"# model = Sequential([layer_1, layer_2])\n",
"\n",
"# 写法3\n",
"model = Sequential([Dense(units=10,activation='relu'),\n",
" Dense(units=5,activation='relu'),\n",
" Dense(units=1,activation='sigmoid')])\n",
"\n", "\n",
"model = tf.keras.Sequential()\n",
"layer_1 = Dense(units=3,activation='sigmoid')\n",
"layer_2 = Dense(units=1,activation='sigmoid')\n",
"model.add(layer_1)\n",
"model.add(layer_2)\n",
"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),loss='binary_crossentropy')\n", "model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),loss='binary_crossentropy')\n",
"model.fit(x,y,epochs=100)\n", "model.fit(x,y,epochs=100)\n",
"a = model.predict(x)\n", "a = model.predict(x)\n",
"\n", "\n",
"# 计算并输出模型的精准度\n", "# 计算并输出模型的精准度\n",
"from sklearn.metrics import accuracy_score\n",
"\n", "\n",
"# 将预测结果转换为二进制标签\n", "# 将预测结果转换为二进制标签\n",
"predicted_labels = (a > 0.5).astype(int)\n", "predicted_labels = (a > 0.5).astype(int)\n",
"\n", "\n",
"# 计算精准度\n", "# 计算精准度\n",
"accuracy = accuracy_score(y, predicted_labels)\n", "accuracy = accuracy_score(y, predicted_labels)\n",
"print(f\"模型的精准度: {accuracy:.2f}\")\n", "print(f\"模型的精准度: {accuracy:.2f}\")"
"\n",
"\n"
] ]
} }
], ],
@@ -388,6 +407,18 @@
"display_name": "Python 3 (ipykernel)", "display_name": "Python 3 (ipykernel)",
"language": "python", "language": "python",
"name": "python3" "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": 4,

View File

@@ -2,8 +2,54 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-20T07:32:36.354335Z",
"start_time": "2025-01-20T07:32:35.224080Z"
}
},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [
"\n",
"# 设置使用gpu7 cuda\n",
"device = torch.device(\"cuda:7\" if torch.cuda.is_available() and torch.cuda.get_device_properties(0).total_memory >= 6*1024**3 else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-20T07:32:38.297401Z",
"start_time": "2025-01-20T07:32:38.261009Z"
}
},
"outputs": [],
"source": [
"# 设置使用mps mps设备当前未支持限制内存\n",
"device = torch.device(\"mps\" if torch.backends.mps.is_available() else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-20T07:32:39.972353Z",
"start_time": "2025-01-20T07:32:39.958549Z"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@@ -20,12 +66,6 @@
} }
], ],
"source": [ "source": [
"import torch\n",
"import torch.nn as nn\n",
"\n",
"# 设置使用gpu7\n",
"device = torch.device(\"cuda:7\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"# 定义一个简单的神经元层\n", "# 定义一个简单的神经元层\n",
"class MultiLayerPerceptron(nn.Module):\n", "class MultiLayerPerceptron(nn.Module):\n",
" def __init__(self, input_size, hidden_size1, hidden_size2, output_size):\n", " def __init__(self, input_size, hidden_size1, hidden_size2, output_size):\n",
@@ -55,7 +95,7 @@
"mlp = MultiLayerPerceptron(input_size=10, hidden_size1=20, hidden_size2=10, output_size=2)\n", "mlp = MultiLayerPerceptron(input_size=10, hidden_size1=20, hidden_size2=10, output_size=2)\n",
"\n", "\n",
"# 打印模型结构\n", "# 打印模型结构\n",
"print(mlp)\n" "print(mlp)"
] ]
} }
], ],
@@ -75,7 +115,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.9" "version": "3.10.14"
} }
}, },
"nbformat": 4, "nbformat": 4,

327
lab/7_Muticlass.ipynb Normal file
View 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
}

View File

Before

Width:  |  Height:  |  Size: 493 KiB

After

Width:  |  Height:  |  Size: 493 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -4,4 +4,5 @@ pillow
matplotlib matplotlib
ipywidgets ipywidgets
jupyter jupyter
scikit-learn scikit-learn
mnist