|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "WARNING:tensorflow:From c:\\Users\\yuanshuai\\AppData\\Local\\anaconda3\\envs\\tensorflow2\\Lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n", |
| 13 | + "\n" |
| 14 | + ] |
| 15 | + } |
| 16 | + ], |
| 17 | + "source": [ |
| 18 | + "import tensorflow as tf\n", |
| 19 | + "import keras\n", |
| 20 | + "from keras import layers\n", |
| 21 | + "\n", |
| 22 | + "### Sequential模型" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 3, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [ |
| 30 | + { |
| 31 | + "name": "stdout", |
| 32 | + "output_type": "stream", |
| 33 | + "text": [ |
| 34 | + "WARNING:tensorflow:From c:\\Users\\yuanshuai\\AppData\\Local\\anaconda3\\envs\\tensorflow2\\Lib\\site-packages\\keras\\src\\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", |
| 35 | + "\n" |
| 36 | + ] |
| 37 | + } |
| 38 | + ], |
| 39 | + "source": [ |
| 40 | + "### 2.1.1 何时使用顺序模型\n", |
| 41 | + "#序列模型适用于每个层恰好有一个输入张量和一个输出张量的普通层堆栈。\n", |
| 42 | + "\n", |
| 43 | + "#示意性地,以下模型:Sequential\n", |
| 44 | + "# Define Sequential model with 3 layers\n", |
| 45 | + "model = keras.Sequential(\n", |
| 46 | + " [\n", |
| 47 | + " layers.Dense(2, activation=\"relu\", name=\"layer1\"),\n", |
| 48 | + " layers.Dense(3, activation=\"relu\", name=\"layer2\"),\n", |
| 49 | + " layers.Dense(4, name=\"layer3\"),\n", |
| 50 | + " ]\n", |
| 51 | + ")\n", |
| 52 | + "# Call model on a test input\n", |
| 53 | + "x = tf.ones((3, 3))\n", |
| 54 | + "y = model(x)" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 4, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "#等效于此函数:\n", |
| 64 | + "# Create 3 layers\n", |
| 65 | + "layer1 = layers.Dense(2, activation=\"relu\", name=\"layer1\")\n", |
| 66 | + "layer2 = layers.Dense(3, activation=\"relu\", name=\"layer2\")\n", |
| 67 | + "layer3 = layers.Dense(4, name=\"layer3\")\n", |
| 68 | + "\n", |
| 69 | + "# Call layers on a test input\n", |
| 70 | + "x = tf.ones((3, 3))\n", |
| 71 | + "y = layer3(layer2(layer1(x)))" |
| 72 | + ] |
| 73 | + }, |
3 | 74 | { |
4 | 75 | "cell_type": "code", |
5 | 76 | "execution_count": null, |
6 | 77 | "metadata": {}, |
7 | 78 | "outputs": [], |
8 | 79 | "source": [ |
9 | | - "#TODO\n", |
10 | | - "#INFO: demo用,单输入单输出 " |
| 80 | + "## 在以下情况下,顺序模型不适用:\n", |
| 81 | + "# 您的模型具有多个输入或多个输出\n", |
| 82 | + "# 任何图层都具有多个输入或多个输出\n", |
| 83 | + "# 您需要进行图层共享\n", |
| 84 | + "# 您需要非线性拓扑(例如残差连接、多分支 模型)\n", |
| 85 | + "\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 5, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "### 2.1.2 创建顺序模型\n", |
| 95 | + "#您可以通过将层列表传递给 Sequentiald的构造函数来创建 Sequential模型:\n", |
| 96 | + "model = keras.Sequential(\n", |
| 97 | + " [\n", |
| 98 | + " layers.Dense(2, activation=\"relu\"),\n", |
| 99 | + " layers.Dense(3, activation=\"relu\"),\n", |
| 100 | + " layers.Dense(4),\n", |
| 101 | + " ]\n", |
| 102 | + ")" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 6, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "data": { |
| 112 | + "text/plain": [ |
| 113 | + "[<keras.src.layers.core.dense.Dense at 0x1ba1a83a010>,\n", |
| 114 | + " <keras.src.layers.core.dense.Dense at 0x1ba1a6e1410>,\n", |
| 115 | + " <keras.src.layers.core.dense.Dense at 0x1ba18782190>]" |
| 116 | + ] |
| 117 | + }, |
| 118 | + "execution_count": 6, |
| 119 | + "metadata": {}, |
| 120 | + "output_type": "execute_result" |
| 121 | + } |
| 122 | + ], |
| 123 | + "source": [ |
| 124 | + "#其图层可通过以下属性访问:layers\n", |
| 125 | + "model.layers" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": 8, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "#您还可以通过以下方法以增量方式创建顺序模型:add()\n", |
| 135 | + "model = keras.Sequential()\n", |
| 136 | + "model.add(layers.Dense(2, activation=\"relu\"))\n", |
| 137 | + "model.add(layers.Dense(3, activation=\"relu\"))\n", |
| 138 | + "model.add(layers.Dense(4))" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 9, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [ |
| 146 | + { |
| 147 | + "name": "stdout", |
| 148 | + "output_type": "stream", |
| 149 | + "text": [ |
| 150 | + "2\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "#请注意,还有一种相应的方法可以删除图层: 顺序模型的行为非常类似于层列表。pop()\n", |
| 156 | + "model.pop()\n", |
| 157 | + "print(len(model.layers)) # 2" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": 10, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "#另请注意,Sequential 构造函数接受一个参数,就像 Keras 中的任何图层或模型。\n", |
| 167 | + "#这对于注释 TensorBoard 图形很有用 具有语义上有意义的名称。name\n", |
| 168 | + "\n", |
| 169 | + "model = keras.Sequential(name=\"my_sequential\")\n", |
| 170 | + "model.add(layers.Dense(2, activation=\"relu\", name=\"layer1\"))\n", |
| 171 | + "model.add(layers.Dense(3, activation=\"relu\", name=\"layer2\"))\n", |
| 172 | + "model.add(layers.Dense(4, name=\"layer3\"))" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 11, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "[]" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 11, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "### 2.1.3 提前指定输入形状\n", |
| 193 | + "#通常,Keras中的所有层都需要知道其输入的形状,以便能够创建其权重。\n", |
| 194 | + "#因此,当您创建这样的层时,最初它没有权重:\n", |
| 195 | + "layer = layers.Dense(3)\n", |
| 196 | + "layer.weights # Empty" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "code", |
| 201 | + "execution_count": 12, |
| 202 | + "metadata": {}, |
| 203 | + "outputs": [ |
| 204 | + { |
| 205 | + "data": { |
| 206 | + "text/plain": [ |
| 207 | + "[<tf.Variable 'dense_6/kernel:0' shape=(4, 3) dtype=float32, numpy=\n", |
| 208 | + " array([[ 0.51047623, -0.17564982, -0.6607744 ],\n", |
| 209 | + " [ 0.7689657 , 0.17553306, 0.82802343],\n", |
| 210 | + " [-0.13806897, -0.24232322, 0.18856204],\n", |
| 211 | + " [ 0.36269712, -0.23856878, -0.25139934]], dtype=float32)>,\n", |
| 212 | + " <tf.Variable 'dense_6/bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>]" |
| 213 | + ] |
| 214 | + }, |
| 215 | + "execution_count": 12, |
| 216 | + "metadata": {}, |
| 217 | + "output_type": "execute_result" |
| 218 | + } |
| 219 | + ], |
| 220 | + "source": [ |
| 221 | + "#它在第一次调用输入时创建其权重,因为形状 权重取决于输入的形状:\n", |
| 222 | + "# Call layer on a test input\n", |
| 223 | + "x = tf.ones((1, 4))\n", |
| 224 | + "y = layer(x)\n", |
| 225 | + "layer.weights # Now it has weights, of shape (4, 3) and (3,)" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": 13, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [ |
| 233 | + { |
| 234 | + "name": "stdout", |
| 235 | + "output_type": "stream", |
| 236 | + "text": [ |
| 237 | + "Number of weights after calling the model: 6\n" |
| 238 | + ] |
| 239 | + } |
| 240 | + ], |
| 241 | + "source": [ |
| 242 | + "#当然,这也适用于Sequential模型。当您实例化一个没有输入形状的Sequential模型时,\n", |
| 243 | + "#它不是“构建”的:它没有权重(调用model.weights会导致一个错误,说明这一点)。\n", |
| 244 | + "#权重是在模型第一次看到一些输入数据时创建的:\n", |
| 245 | + "model = keras.Sequential(\n", |
| 246 | + " [\n", |
| 247 | + " layers.Dense(2, activation=\"relu\"),\n", |
| 248 | + " layers.Dense(3, activation=\"relu\"),\n", |
| 249 | + " layers.Dense(4),\n", |
| 250 | + " ]\n", |
| 251 | + ") # No weights at this stage!\n", |
| 252 | + "\n", |
| 253 | + "# At this point, you can't do this:\n", |
| 254 | + "# model.weights\n", |
| 255 | + "\n", |
| 256 | + "# You also can't do this:\n", |
| 257 | + "# model.summary()\n", |
| 258 | + "\n", |
| 259 | + "# Call the model on a test input\n", |
| 260 | + "x = tf.ones((1, 4))\n", |
| 261 | + "y = model(x)\n", |
| 262 | + "print(\"Number of weights after calling the model:\", len(model.weights)) # 6" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "cell_type": "code", |
| 267 | + "execution_count": 14, |
| 268 | + "metadata": {}, |
| 269 | + "outputs": [ |
| 270 | + { |
| 271 | + "name": "stdout", |
| 272 | + "output_type": "stream", |
| 273 | + "text": [ |
| 274 | + "Model: \"sequential_3\"\n", |
| 275 | + "_________________________________________________________________\n", |
| 276 | + " Layer (type) Output Shape Param # \n", |
| 277 | + "=================================================================\n", |
| 278 | + " dense_7 (Dense) (1, 2) 10 \n", |
| 279 | + " \n", |
| 280 | + " dense_8 (Dense) (1, 3) 9 \n", |
| 281 | + " \n", |
| 282 | + " dense_9 (Dense) (1, 4) 16 \n", |
| 283 | + " \n", |
| 284 | + "=================================================================\n", |
| 285 | + "Total params: 35 (140.00 Byte)\n", |
| 286 | + "Trainable params: 35 (140.00 Byte)\n", |
| 287 | + "Non-trainable params: 0 (0.00 Byte)\n", |
| 288 | + "_________________________________________________________________\n" |
| 289 | + ] |
| 290 | + } |
| 291 | + ], |
| 292 | + "source": [ |
| 293 | + "#“构建”模型后,可以调用其方法来显示其 内容:summary()\n", |
| 294 | + "model.summary()" |
11 | 295 | ] |
12 | 296 | } |
13 | 297 | ], |
14 | 298 | "metadata": { |
| 299 | + "kernelspec": { |
| 300 | + "display_name": "Python 3", |
| 301 | + "language": "python", |
| 302 | + "name": "python3" |
| 303 | + }, |
15 | 304 | "language_info": { |
16 | | - "name": "python" |
| 305 | + "codemirror_mode": { |
| 306 | + "name": "ipython", |
| 307 | + "version": 3 |
| 308 | + }, |
| 309 | + "file_extension": ".py", |
| 310 | + "mimetype": "text/x-python", |
| 311 | + "name": "python", |
| 312 | + "nbconvert_exporter": "python", |
| 313 | + "pygments_lexer": "ipython3", |
| 314 | + "version": "3.11.8" |
17 | 315 | } |
18 | 316 | }, |
19 | 317 | "nbformat": 4, |
|
0 commit comments