Skip to content

Commit 835b95d

Browse files
author
perhapszzy
committed
add examples from tensorflow book.
1 parent 270ea5d commit 835b95d

3,803 files changed

Lines changed: 120164 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 1. 定义两个不同的图"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {
14+
"collapsed": false
15+
},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"[ 0.]\n",
22+
"[ 1.]\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"import tensorflow as tf\n",
28+
"\n",
29+
"g1 = tf.Graph()\n",
30+
"with g1.as_default():\n",
31+
" v = tf.get_variable(\"v\", [1], initializer = tf.zeros_initializer) # 设置初始值为0\n",
32+
"\n",
33+
"g2 = tf.Graph()\n",
34+
"with g2.as_default():\n",
35+
" v = tf.get_variable(\"v\", [1], initializer = tf.ones_initializer()) # 设置初始值为1\n",
36+
" \n",
37+
"with tf.Session(graph = g1) as sess:\n",
38+
" tf.global_variables_initializer().run()\n",
39+
" with tf.variable_scope(\"\", reuse=True):\n",
40+
" print(sess.run(tf.get_variable(\"v\")))\n",
41+
"\n",
42+
"with tf.Session(graph = g2) as sess:\n",
43+
" tf.global_variables_initializer().run()\n",
44+
" with tf.variable_scope(\"\", reuse=True):\n",
45+
" print(sess.run(tf.get_variable(\"v\")))"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {
51+
"collapsed": true
52+
},
53+
"source": [
54+
"#### 2. 张量的概念"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 2,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"Tensor(\"add:0\", shape=(2,), dtype=float32)\n",
69+
"[ 3. 5.]\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"import tensorflow as tf\n",
75+
"a = tf.constant([1.0, 2.0], name=\"a\")\n",
76+
"b = tf.constant([2.0, 3.0], name=\"b\")\n",
77+
"result = a + b\n",
78+
"print result\n",
79+
"\n",
80+
"sess = tf.InteractiveSession ()\n",
81+
"print(result.eval())\n",
82+
"sess.close()"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"#### 3. 会话的使用"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"3.1 创建和关闭会话"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 3,
102+
"metadata": {
103+
"collapsed": false
104+
},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"[ 3. 5.]\n"
111+
]
112+
}
113+
],
114+
"source": [
115+
"# 创建一个会话。\n",
116+
"sess = tf.Session()\n",
117+
"\n",
118+
"# 使用会话得到之前计算的结果。\n",
119+
"print(sess.run(result))\n",
120+
"\n",
121+
"# 关闭会话使得本次运行中使用到的资源可以被释放。\n",
122+
"sess.close()"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"3.2 使用with statement 来创建会话"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 4,
135+
"metadata": {
136+
"collapsed": false
137+
},
138+
"outputs": [
139+
{
140+
"name": "stdout",
141+
"output_type": "stream",
142+
"text": [
143+
"[ 3. 5.]\n"
144+
]
145+
}
146+
],
147+
"source": [
148+
"with tf.Session() as sess:\n",
149+
" print(sess.run(result))"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"3.3 指定默认会话"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 5,
162+
"metadata": {
163+
"collapsed": false
164+
},
165+
"outputs": [
166+
{
167+
"name": "stdout",
168+
"output_type": "stream",
169+
"text": [
170+
"[ 3. 5.]\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"sess = tf.Session()\n",
176+
"with sess.as_default():\n",
177+
" print(result.eval())"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 6,
183+
"metadata": {
184+
"collapsed": false
185+
},
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"[ 3. 5.]\n",
192+
"[ 3. 5.]\n"
193+
]
194+
}
195+
],
196+
"source": [
197+
"sess = tf.Session()\n",
198+
"\n",
199+
"# 下面的两个命令有相同的功能。\n",
200+
"print(sess.run(result))\n",
201+
"print(result.eval(session=sess))"
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"metadata": {},
207+
"source": [
208+
"#### 4. 使用tf.InteractiveSession构建会话"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 7,
214+
"metadata": {
215+
"collapsed": false
216+
},
217+
"outputs": [
218+
{
219+
"name": "stdout",
220+
"output_type": "stream",
221+
"text": [
222+
"[ 3. 5.]\n"
223+
]
224+
}
225+
],
226+
"source": [
227+
"sess = tf.InteractiveSession ()\n",
228+
"print(result.eval())\n",
229+
"sess.close()"
230+
]
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"#### 5. 通过ConfigProto配置会话"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 8,
242+
"metadata": {
243+
"collapsed": true
244+
},
245+
"outputs": [],
246+
"source": [
247+
"config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)\n",
248+
"sess1 = tf.InteractiveSession(config=config)\n",
249+
"sess2 = tf.Session(config=config)"
250+
]
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "Python 2",
256+
"language": "python",
257+
"name": "python2"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 2
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython2",
269+
"version": "2.7.10"
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 1
274+
}

0 commit comments

Comments
 (0)