|
1 | 1 | { |
2 | | - "cells": [], |
3 | | - "metadata": {}, |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## 4.1.1 build model from block" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "name": "stdout", |
| 17 | + "output_type": "stream", |
| 18 | + "text": [ |
| 19 | + "2.0.0\n" |
| 20 | + ] |
| 21 | + } |
| 22 | + ], |
| 23 | + "source": [ |
| 24 | + "import tensorflow as tf\n", |
| 25 | + "import numpy as np\n", |
| 26 | + "print(tf.__version__)" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "class MLP(tf.keras.Model):\n", |
| 36 | + " def __init__(self):\n", |
| 37 | + " super().__init__()\n", |
| 38 | + " self.flatten = tf.keras.layers.Flatten() # Flatten层将除第一维(batch_size)以外的维度展平\n", |
| 39 | + " self.dense1 = tf.keras.layers.Dense(units=256, activation=tf.nn.relu)\n", |
| 40 | + " self.dense2 = tf.keras.layers.Dense(units=10)\n", |
| 41 | + "\n", |
| 42 | + " def call(self, inputs): \n", |
| 43 | + " x = self.flatten(inputs) \n", |
| 44 | + " x = self.dense1(x) \n", |
| 45 | + " output = self.dense2(x) \n", |
| 46 | + " return output" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 3, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "data": { |
| 56 | + "text/plain": [ |
| 57 | + "<tf.Tensor: id=62, shape=(2, 10), dtype=float32, numpy=\n", |
| 58 | + "array([[ 0.25095996, -0.24893613, -0.45473456, -0.04311958, -0.2295383 ,\n", |
| 59 | + " 0.27443463, 0.37376237, -0.14436285, -0.37055504, -0.00098359],\n", |
| 60 | + " [ 0.1769592 , -0.14420828, -0.3330693 , -0.14657606, -0.14307055,\n", |
| 61 | + " 0.27779722, 0.33585754, -0.14960271, -0.38496172, 0.10907309]],\n", |
| 62 | + " dtype=float32)>" |
| 63 | + ] |
| 64 | + }, |
| 65 | + "execution_count": 3, |
| 66 | + "metadata": {}, |
| 67 | + "output_type": "execute_result" |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "X = tf.random.uniform((2,20))\n", |
| 72 | + "net = MLP()\n", |
| 73 | + "net(X)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "## 4.1.2 Sequential" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 4, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "data": { |
| 90 | + "text/plain": [ |
| 91 | + "<tf.Tensor: id=117, shape=(2, 10), dtype=float32, numpy=\n", |
| 92 | + "array([[ 0.18656988, -0.24001493, -0.22437742, -0.15284519, -0.10276742,\n", |
| 93 | + " -0.04557859, 0.49852332, 0.04370471, -0.26982784, -0.14277868],\n", |
| 94 | + " [-0.11792175, -0.28876868, -0.4724299 , -0.08530779, -0.17974694,\n", |
| 95 | + " 0.07656129, 0.53052086, 0.13260414, -0.36211073, -0.08055006]],\n", |
| 96 | + " dtype=float32)>" |
| 97 | + ] |
| 98 | + }, |
| 99 | + "execution_count": 4, |
| 100 | + "metadata": {}, |
| 101 | + "output_type": "execute_result" |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "model = tf.keras.models.Sequential([\n", |
| 106 | + " tf.keras.layers.Flatten(),\n", |
| 107 | + " tf.keras.layers.Dense(256, activation=tf.nn.relu),\n", |
| 108 | + " tf.keras.layers.Dense(10),\n", |
| 109 | + "])\n", |
| 110 | + "\n", |
| 111 | + "model(X)" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "## 4.1.3 build complex model" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 5, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "class FancyMLP(tf.keras.Model):\n", |
| 128 | + " def __init__(self):\n", |
| 129 | + " super().__init__()\n", |
| 130 | + " self.flatten = tf.keras.layers.Flatten()\n", |
| 131 | + " self.rand_weight = tf.constant(\n", |
| 132 | + " tf.random.uniform((20,20)))\n", |
| 133 | + " self.dense = tf.keras.layers.Dense(units=20, activation=tf.nn.relu)\n", |
| 134 | + "\n", |
| 135 | + " def call(self, inputs): \n", |
| 136 | + " x = self.flatten(inputs) \n", |
| 137 | + " x = tf.nn.relu(tf.matmul(x, self.rand_weight) + 1)\n", |
| 138 | + " x = self.dense(x) \n", |
| 139 | + " while tf.norm(x) > 1:\n", |
| 140 | + " x /= 2\n", |
| 141 | + " if tf.norm(x) < 0.8:\n", |
| 142 | + " x *= 10\n", |
| 143 | + " return tf.reduce_sum(x)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": 6, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "data": { |
| 153 | + "text/plain": [ |
| 154 | + "<tf.Tensor: id=229, shape=(), dtype=float32, numpy=28.175375>" |
| 155 | + ] |
| 156 | + }, |
| 157 | + "execution_count": 6, |
| 158 | + "metadata": {}, |
| 159 | + "output_type": "execute_result" |
| 160 | + } |
| 161 | + ], |
| 162 | + "source": [ |
| 163 | + "net = FancyMLP()\n", |
| 164 | + "net(X)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "## debugging" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 7, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "ename": "TypeError", |
| 181 | + "evalue": "add() takes 2 positional arguments but 4 were given", |
| 182 | + "output_type": "error", |
| 183 | + "traceback": [ |
| 184 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 185 | + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 186 | + "\u001b[1;32m<ipython-input-7-ac9a7842702c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0mnet\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSequential\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[0mnet\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mNestMLP\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 16\u001b[0m \u001b[0mnet\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDense\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[0mnet\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mFancyMLP\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 187 | + "\u001b[1;32m<ipython-input-7-ac9a7842702c>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 5\u001b[0m self.net.add(tf.keras.layers.Flatten(),\n\u001b[0;32m 6\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDense\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m64\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mactivation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelu\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m tf.keras.layers.Dense(32, activation=tf.nn.relu))\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdense\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDense\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0munits\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m16\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mactivation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelu\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", |
| 188 | + "\u001b[1;32mD:\\dev_tools\\Anaconda3_2019.07\\envs\\tf2.0\\lib\\site-packages\\tensorflow_core\\python\\training\\tracking\\base.py\u001b[0m in \u001b[0;36m_method_wrapper\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 455\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_self_setattr_tracking\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 456\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 457\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 458\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 459\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_self_setattr_tracking\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mprevious_value\u001b[0m \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 189 | + "\u001b[1;31mTypeError\u001b[0m: add() takes 2 positional arguments but 4 were given" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "source": [ |
| 194 | + "class NestMLP(tf.keras.Model):\n", |
| 195 | + " def __init__(self):\n", |
| 196 | + " super().__init__()\n", |
| 197 | + " self.net = tf.keras.Sequential()\n", |
| 198 | + " self.net.add(tf.keras.layers.Flatten(),\n", |
| 199 | + " tf.keras.layers.Dense(64, activation=tf.nn.relu),\n", |
| 200 | + " tf.keras.layers.Dense(32, activation=tf.nn.relu))\n", |
| 201 | + " self.dense = tf.keras.layers.Dense(units=16, activation=tf.nn.relu)\n", |
| 202 | + "\n", |
| 203 | + " \n", |
| 204 | + " def call(self, inputs): \n", |
| 205 | + " return self.dense(self.net(inputs))\n", |
| 206 | + "\n", |
| 207 | + "net = tf.keras.Sequential()\n", |
| 208 | + "net.add(NestMLP())\n", |
| 209 | + "net.add(tf.keras.layers.Dense(20))\n", |
| 210 | + "net.add(FancyMLP())\n", |
| 211 | + "\n", |
| 212 | + "net(X)" |
| 213 | + ] |
| 214 | + } |
| 215 | + ], |
| 216 | + "metadata": { |
| 217 | + "kernelspec": { |
| 218 | + "display_name": "tf2.0", |
| 219 | + "language": "python", |
| 220 | + "name": "tf2.0" |
| 221 | + }, |
| 222 | + "language_info": { |
| 223 | + "codemirror_mode": { |
| 224 | + "name": "ipython", |
| 225 | + "version": 3 |
| 226 | + }, |
| 227 | + "file_extension": ".py", |
| 228 | + "mimetype": "text/x-python", |
| 229 | + "name": "python", |
| 230 | + "nbconvert_exporter": "python", |
| 231 | + "pygments_lexer": "ipython3", |
| 232 | + "version": "3.7.4" |
| 233 | + } |
| 234 | + }, |
4 | 235 | "nbformat": 4, |
5 | 236 | "nbformat_minor": 2 |
6 | 237 | } |
0 commit comments