{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # 设置日志级别为ERROR,以减少警告信息\n", "# 禁用 Gemini 的底层库(gRPC 和 Abseil)在初始化日志警告\n", "os.environ[\"GRPC_VERBOSITY\"] = \"ERROR\"\n", "os.environ[\"GLOG_minloglevel\"] = \"3\" # 0: INFO, 1: WARNING, 2: ERROR, 3: FATAL\n", "os.environ[\"GLOG_minloglevel\"] = \"true\"\n", "import logging\n", "import tensorflow as tf\n", "tf.get_logger().setLevel(logging.ERROR)\n", "tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)\n", "!export TF_FORCE_GPU_ALLOW_GROWTH=true" ] }, { "cell_type": "markdown", "metadata": { "cellView": "form", "id": "BZSlp3DAjdYf" }, "source": [ "````{admonition} Copyright 2019 The TensorFlow Authors.\n", "```\n", "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "```\n", "````" ] }, { "cell_type": "markdown", "metadata": { "id": "3wF5wszaj97Y" }, "source": [ "# 初学者的 TensorFlow 2.0 教程" ] }, { "cell_type": "markdown", "metadata": { "id": "DUNzJc4jTj6G" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org 观看 在 Google Colab 中运行 在 GitHub 查看源代码 下载笔记本
" ] }, { "cell_type": "markdown", "metadata": { "id": "04QgGZc9bF5D" }, "source": [ "```{topic} 导航\n", "\n", "此简短介绍使用 [Keras](https://tensorflow.google.cn/guide/keras/overview) 进行以下操作:\n", "\n", "1. 加载预构建的数据集。\n", "2. 构建对图像进行分类的神经网络机器学习模型。\n", "3. 训练此神经网络。\n", "4. 评估模型的准确率。\n", "```" ] }, { "cell_type": "markdown", "metadata": { "id": "nnrWf3PCEzXL" }, "source": [ "## 设置 TensorFlow\n", "\n", "首先将 TensorFlow 导入到您的程序:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "0trJmd6DjqBZ" }, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": { "id": "7NAbSZiaoJ4z" }, "source": [ "如果您在自己的开发环境而不是 [Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/quickstart/beginner.ipynb) 中操作,请参阅设置 TensorFlow 以进行开发的[安装指南](https://tensorflow.google.cn/install)。\n", "\n", "注:如果您使用自己的开发环境,请确保您已升级到最新的 `pip` 以安装 TensorFlow 2 软件包。有关详情,请参阅[安装指南](https://tensorflow.google.cn/install)。\n", "\n", "## 加载数据集\n", "\n", "加载并准备 [MNIST 数据集](http://yann.lecun.com/exdb/mnist/)。图像的像素值范围从 0 到 255。通过将值除以 `255.0` 来将这些值缩放到 0 到 1 的范围。这还会将样本数据从整数转换为浮点数:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "7FP5258xjs-v" }, "outputs": [], "source": [ "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" ] }, { "cell_type": "markdown", "metadata": { "id": "BPZ68wASog_I" }, "source": [ "## 构建机器学习模型\n", "\n", "构建 `tf.keras.Sequential` 模型:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "h3IKyzTCDNGo" }, "outputs": [], "source": [ "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(128, activation='relu'),\n", " tf.keras.layers.Dropout(0.2),\n", " tf.keras.layers.Dense(10)\n", "])" ] }, { "cell_type": "markdown", "metadata": { "id": "l2hiez2eIUz8" }, "source": [ "[`Sequential`](https://tensorflow.google.cn/guide/keras/sequential_model) 对于堆叠层很有用,其中每层都有输入[张量](https://tensorflow.google.cn/guide/tensor)和输出张量。层是具有已知数学结构的函数,可以重复使用并具有可训练的变量。大多数 TensorFlow 模型都由层组成。此模型使用 [`Flatten`](https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Flatten)、[`Dense`](https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Dense) 和 [`Dropout`](https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Dropout) 层。\n", "\n", "对于每个样本,模型都会返回包含 [logits](https://developers.google.com/machine-learning/glossary#logits) 或 [log-odds](https://developers.google.com/machine-learning/glossary#log-odds) 分数的向量,每个类一个。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "OeOrNdnkEEcR" }, "outputs": [ { "data": { "text/plain": [ "array([[-0.29356095, -0.18187018, -0.09060116, 0.02143997, 0.24739322,\n", " 0.02414675, -0.34286305, 0.02412909, 0.39885187, 0.39236996]],\n", " dtype=float32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "predictions = model(x_train[:1]).numpy()\n", "predictions" ] }, { "cell_type": "markdown", "metadata": { "id": "tgjhDQGcIniO" }, "source": [ "{func}`tensorflow.nn.softmax` 函数将这些 logits 转换为每个类的*概率*: " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "zWSRnQ0WI5eq" }, "outputs": [ { "data": { "text/plain": [ "array([[0.07084628, 0.07921798, 0.08678835, 0.09707788, 0.12168877,\n", " 0.09734099, 0.06743812, 0.09733927, 0.14158855, 0.14067376]],\n", " dtype=float32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.nn.softmax(predictions).numpy()" ] }, { "cell_type": "markdown", "metadata": { "id": "he5u_okAYS4a" }, "source": [ "注:可以将 {func}`tensorflow.nn.softmax` 烘焙到网络最后一层的激活函数中。虽然这可以使模型输出更易解释,但不建议使用这种方式,因为在使用 `softmax` 输出时不可能为所有模型提供精确且数值稳定的损失计算。 " ] }, { "cell_type": "markdown", "metadata": { "id": "hQyugpgRIyrA" }, "source": [ "使用 {class}`~tensorflow.keras.losses.SparseCategoricalCrossentropy` 定义训练的损失函数:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "RSkzdv8MD0tT" }, "outputs": [], "source": [ "loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)" ] }, { "cell_type": "markdown", "metadata": { "id": "SfR4MsSDU880" }, "source": [ "损失函数采用真实值向量和逻辑向量,并返回每个样本的标量损失。此损失等于真实类的负对数概率:如果模型确定类正确,则损失为零。\n", "\n", "这个未经训练的模型给出的概率接近随机(每个类为 1/10),因此初始损失应该接近 `-tf.math.log(1/10) ~= 2.3`。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "NJWqEVrrJ7ZB" }, "outputs": [ { "data": { "text/plain": [ "2.329535" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loss_fn(y_train[:1], predictions).numpy()" ] }, { "cell_type": "markdown", "metadata": { "id": "ada44eb947d4" }, "source": [ "在开始训练之前,使用 Keras {meth}`~tensorflow.models.model.Model.compile` 配置和编译模型。将 [`optimizer`](https://tensorflow.google.cn/api_docs/python/tf/keras/optimizers) 类设置为 `adam`,将 `loss` 设置为您之前定义的 `loss_fn` 函数,并通过将 `metrics` 参数设置为 `accuracy` 来指定要为模型评估的指标。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "9foNKHzTD2Vo" }, "outputs": [], "source": [ "model.compile(optimizer='adam',\n", " loss=loss_fn,\n", " metrics=['accuracy'])" ] }, { "cell_type": "markdown", "metadata": { "id": "ix4mEL65on-w" }, "source": [ "## 训练并评估模型\n", "\n", "使用 `Model.fit` 方法调整您的模型参数并最小化损失: " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "y7suUbJXVLqP" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/5\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "I0000 00:00:1729733723.232793 3171586 service.cc:146] XLA service 0x7f3d74006ef0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:\n", "I0000 00:00:1729733723.232815 3171586 service.cc:154] StreamExecutor device (0): NVIDIA GeForce RTX 3090, Compute Capability 8.6\n", "I0000 00:00:1729733723.232833 3171586 service.cc:154] StreamExecutor device (1): NVIDIA GeForce RTX 2080 Ti, Compute Capability 7.5\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m 78/1875\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m3s\u001b[0m 2ms/step - accuracy: 0.5239 - loss: 1.5746" ] }, { "name": "stderr", "output_type": "stream", "text": [ "I0000 00:00:1729733725.890061 3171586 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m7s\u001b[0m 2ms/step - accuracy: 0.8585 - loss: 0.4892\n", "Epoch 2/5\n", "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9557 - loss: 0.1527\n", "Epoch 3/5\n", "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9675 - loss: 0.1089\n", "Epoch 4/5\n", "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9736 - loss: 0.0847\n", "Epoch 5/5\n", "\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9762 - loss: 0.0727\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(x_train, y_train, epochs=5)" ] }, { "cell_type": "markdown", "metadata": { "id": "4mDAAPFqVVgn" }, "source": [ "`Model.evaluate` 方法会检查模型的性能(通常是在[验证集](https://developers.google.com/machine-learning/glossary#validation-set)或[测试集](https://developers.google.com/machine-learning/glossary#test-set)上)。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "F7dTAzgHDUh7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "313/313 - 2s - 8ms/step - accuracy: 0.9769 - loss: 0.0729\n" ] }, { "data": { "text/plain": [ "[0.07293855398893356, 0.9768999814987183]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.evaluate(x_test, y_test, verbose=2)" ] }, { "cell_type": "markdown", "metadata": { "id": "T4JfEh7kvx6m" }, "source": [ "现在,这个照片分类器的准确度已经达到 98%。想要了解更多,请阅读 [TensorFlow 教程](https://tensorflow.google.cn/tutorials/)。" ] }, { "cell_type": "markdown", "metadata": { "id": "Aj8NrlzlJqDG" }, "source": [ "如果您想让模型返回概率,可以封装经过训练的模型,并将 softmax 附加到该模型:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "rYb6DrEH0GMv" }, "outputs": [], "source": [ "probability_model = tf.keras.Sequential([\n", " model,\n", " tf.keras.layers.Softmax()\n", "])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "cnqOZtUp1YR_" }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "probability_model(x_test[:5])" ] }, { "cell_type": "markdown", "metadata": { "id": "-47O6_GLdRuT" }, "source": [ "## 结论\n", "\n", "恭喜!您已经利用 [Keras](https://tensorflow.google.cn/guide/keras/overview) API 借助预构建数据集训练了一个机器学习模型。\n", "\n", "有关使用 Keras 的更多示例,请查阅[教程](https://tensorflow.google.cn/tutorials/keras/)。要详细了解如何使用 Keras 构建模型,请阅读[指南](https://tensorflow.google.cn/guide/keras)。如果您想详细了解如何加载和准备数据,请参阅有关[图像数据加载](https://tensorflow.google.cn/tutorials/load_data/images)或 [CSV 数据加载](https://tensorflow.google.cn/tutorials/load_data/csv)的教程。\n" ] } ], "metadata": { "colab": { "name": "beginner.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "xxx", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }