Skip to content

Commit 9e60630

Browse files
committed
update 4.1
1 parent 11fe80c commit 9e60630

File tree

4 files changed

+706
-4
lines changed

4 files changed

+706
-4
lines changed
Lines changed: 233 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,237 @@
11
{
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+
},
4235
"nbformat": 4,
5236
"nbformat_minor": 2
6237
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
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+
"2.0.0\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"import tensorflow as tf\n",
18+
"import numpy as np\n",
19+
"print(tf.__version__)"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"<tf.Tensor: id=62, shape=(2, 10), dtype=float32, numpy=\n",
31+
"array([[ 0.47627547, 0.12908635, -0.04767011, 0.03762572, -0.22968994,\n",
32+
" 0.07292195, -0.20956676, 0.24949205, 0.14892189, -0.14833519],\n",
33+
" [ 0.391208 , 0.26524487, -0.05629939, 0.00716697, -0.06539508,\n",
34+
" 0.17122187, -0.16350499, 0.5076758 , 0.07857557, -0.05519376]],\n",
35+
" dtype=float32)>"
36+
]
37+
},
38+
"execution_count": 2,
39+
"metadata": {},
40+
"output_type": "execute_result"
41+
}
42+
],
43+
"source": [
44+
"net = tf.keras.models.Sequential()\n",
45+
"net.add(tf.keras.layers.Flatten())\n",
46+
"net.add(tf.keras.layers.Dense(256,activation=tf.nn.relu))\n",
47+
"net.add(tf.keras.layers.Dense(10))\n",
48+
"\n",
49+
"X = tf.random.uniform((2,20))\n",
50+
"Y = net(X)\n",
51+
"Y"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## 4.2.1 access model parameters"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 10,
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"(<tf.Variable 'sequential/dense/kernel:0' shape=(20, 256) dtype=float32, numpy=\n",
70+
" array([[-2.02483535e-02, 2.85128355e-02, -7.38415122e-03, ...,\n",
71+
" 7.84173608e-03, -1.25107020e-02, 8.92712176e-02],\n",
72+
" [ 2.29488611e-02, -1.39583871e-01, -1.23225398e-01, ...,\n",
73+
" 7.12196082e-02, -2.21055746e-03, -4.10574600e-02],\n",
74+
" [ 5.77151179e-02, -1.44912243e-01, 1.00331202e-01, ...,\n",
75+
" -1.19500108e-01, -7.26372823e-02, -1.00700825e-01],\n",
76+
" ...,\n",
77+
" [ 1.46352559e-01, 3.99160534e-02, -1.04723126e-01, ...,\n",
78+
" 1.44056857e-01, -6.36567399e-02, -4.77666780e-02],\n",
79+
" [-2.45300457e-02, -7.79614747e-02, -6.78218603e-02, ...,\n",
80+
" 1.62270069e-02, -1.57367885e-02, -7.33146816e-02],\n",
81+
" [-2.91189030e-02, 1.42401308e-01, -1.06634945e-02, ...,\n",
82+
" -7.19291270e-02, -2.29924917e-05, 1.06444657e-01]], dtype=float32)>,\n",
83+
" tensorflow.python.ops.resource_variable_ops.ResourceVariable)"
84+
]
85+
},
86+
"execution_count": 10,
87+
"metadata": {},
88+
"output_type": "execute_result"
89+
}
90+
],
91+
"source": [
92+
"net.weights[0], type(net.weights[0])"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": 14,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"## initialize params"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": []
110+
}
111+
],
112+
"metadata": {
113+
"kernelspec": {
114+
"display_name": "tf2.0",
115+
"language": "python",
116+
"name": "tf2.0"
117+
},
118+
"language_info": {
119+
"codemirror_mode": {
120+
"name": "ipython",
121+
"version": 3
122+
},
123+
"file_extension": ".py",
124+
"mimetype": "text/x-python",
125+
"name": "python",
126+
"nbconvert_exporter": "python",
127+
"pygments_lexer": "ipython3",
128+
"version": "3.7.4"
129+
}
130+
},
131+
"nbformat": 4,
132+
"nbformat_minor": 2
133+
}

0 commit comments

Comments
 (0)