Skip to content

Commit 754a6d5

Browse files
committed
添加了THEANO 中新的部分:条件语句(已完成),循环(未完成)
Former-commit-id: d0f7369 Former-commit-id: 830e102eb3fb331a5b9d5cef04035ebac58c1fe4 [formerly a74a6fa2bc5e126fc771e2b9fffd1127466655c0] [formerly a95a51beb664dd5f44ba31b69757a78884fec49b [formerly 5024c97f116b12566d0041d0c2d2763e299ed92f]] [formerly abc5fff86cee35a0453398c2215c54a64312de6b [formerly b893ba81d31ba86d58ac4d7485ee37956154fca4] [formerly 4ee8a24f7c9a690ad0477c3e875af230f365c085 [formerly 4816dd69373c205704811e22c0d3dc6686ddcb56]]] [formerly 83c9c62360418c82879fd3f8bdbd3dcf14ac715f [formerly cf4eccbcfa0fd33aee74ea64a324e62cb23ea938] [formerly f6d0328bd300e94cdedc1f98d985b8f9871f1fef [formerly 825c002aa06506676622d7672aea87dc4f69a6e8]] [formerly ca61cb190cfe285d61ebda83e9b899c541608f2f [formerly 13112ce28c1a83a07e6ff34ee04cd4a93540a0cf] [formerly 2e6e92068692127ce8bfe96caa3c8f498b9689c9 [formerly 47c0039]]]] Former-commit-id: aed0815352cbb90ebd870e0ae87ffc8a3cf1994f [formerly cccc4ecb14a70bf019a1c3e9f676a6b1f4900562] [formerly f2631154b38bae9b3d6c2d5ddb25f489019a6e16 [formerly 38f29af4a384f5307f074f5736c7833397a6ae4d]] [formerly b3982ab7425260f663ff9ace79accee5aa92ce97 [formerly f12026ece163fba818314e6a0b0529b814d89131] [formerly 705c62c50912239893e9cb12af82e15c8c279d0e [formerly 22f1fa88cb9c73a2b0a87392e93ef8ee2e57b733]]] Former-commit-id: d89b1e6618d41ea5e66a41b79d0479e5af45ffb6 [formerly e43c6b5a8bcb757ae9f14c0a213419ea118753a2] [formerly e03fecdf2108b995b10c78f0576d02dd4fa8fbde [formerly e0e189f56d02385bb26db357131d1c2b05c776f9]] Former-commit-id: 4c36a74815ed018469e50c2909645e0e7b113e95 [formerly 98d24e1371efa6164aa2a67d90d21baf4b737756] Former-commit-id: 5c40852455052bde2a93a8254eea9cafa5abfcb2
1 parent 1d4b3db commit 754a6d5

19 files changed

+210
-11
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c5f58238f46e59e2c4256b9b0ad3c55eb3509cba
1+
d3f594d42581790643932c108394ef97844d5dfc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8f1d5f2cb2318c8656db280b053c71757447764d

09. theano/09.04 linear regression.ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcc8a578e20cec3c5ec6eb4649ebc2ba422079e4

09. theano/09.05 logistic regression .ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Theano 条件语句"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"`theano` 中提供了两种条件语句,`ifelse` 和 `switch`,两者都是用于在符号变量上使用条件语句:\n",
15+
"\n",
16+
"- `ifelse(condition, var1, var2)`\n",
17+
" - 如果 `condition` 为 `true`,返回 `var1`,否则返回 `var2`\n",
18+
"- `switch(tensor, var1, var2)`\n",
19+
" - Elementwise `ifelse` 操作,更一般化\n",
20+
"- `switch` 会计算两个输出,而 `ifelse` 只会根据给定的条件,计算相应的输出。\n",
21+
"\n",
22+
"`ifelse` 需要从 `theano.ifelse` 中导入,而 `switch` 在 `theano.tensor` 模块中。"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 1,
28+
"metadata": {
29+
"collapsed": false
30+
},
31+
"outputs": [
32+
{
33+
"name": "stderr",
34+
"output_type": "stream",
35+
"text": [
36+
"Using gpu device 1: Tesla K10.G2.8GB (CNMeM is disabled)\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"import theano, time\n",
42+
"import theano.tensor as T\n",
43+
"import numpy as np\n",
44+
"from theano.ifelse import ifelse"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"假设我们有两个标量参数:$a, b$,和两个矩阵 $\\mathbf{x, y}$,定义函数为:\n",
52+
"\n",
53+
"$$ \n",
54+
"\\mathbf z = f(a, b,\\mathbf{x, y}) = \\left\\{ \n",
55+
"\\begin{aligned}\n",
56+
" \\mathbf x & ,\\ a <= b\\\\\n",
57+
" \\mathbf y & ,\\ a > b\n",
58+
"\\end{aligned}\n",
59+
"\\right.\n",
60+
"$$\n",
61+
"\n",
62+
"定义变量:"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 2,
68+
"metadata": {
69+
"collapsed": true
70+
},
71+
"outputs": [],
72+
"source": [
73+
"a, b = T.scalars('a', 'b')\n",
74+
"x, y = T.matrices('x', 'y')"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"用 `ifelse` 构造,小于等于用 `T.lt()`,大于等于用 `T.gt()`:"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 3,
87+
"metadata": {
88+
"collapsed": false
89+
},
90+
"outputs": [],
91+
"source": [
92+
"z_ifelse = ifelse(T.lt(a, b), x, y)\n",
93+
"\n",
94+
"f_ifelse = theano.function([a, b, x, y], z_ifelse)"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"用 `switch` 构造:"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 4,
107+
"metadata": {
108+
"collapsed": true
109+
},
110+
"outputs": [],
111+
"source": [
112+
"z_switch = T.switch(T.lt(a, b), x, y)\n",
113+
"\n",
114+
"f_switch = theano.function([a, b, x, y], z_switch)"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"测试数据:"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 5,
127+
"metadata": {
128+
"collapsed": false
129+
},
130+
"outputs": [],
131+
"source": [
132+
"val1 = 0.\n",
133+
"val2 = 1.\n",
134+
"big_mat1 = np.ones((10000, 1000), dtype=theano.config.floatX)\n",
135+
"big_mat2 = np.ones((10000, 1000), dtype=theano.config.floatX)"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"比较两者的运行速度:"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 6,
148+
"metadata": {
149+
"collapsed": false
150+
},
151+
"outputs": [
152+
{
153+
"name": "stdout",
154+
"output_type": "stream",
155+
"text": [
156+
"time spent evaluating both values 0.638598 sec\n",
157+
"time spent evaluating one value 0.461249 sec\n"
158+
]
159+
}
160+
],
161+
"source": [
162+
"n_times = 10\n",
163+
"\n",
164+
"tic = time.clock()\n",
165+
"for i in xrange(n_times):\n",
166+
" f_switch(val1, val2, big_mat1, big_mat2)\n",
167+
"print 'time spent evaluating both values %f sec' % (time.clock() - tic)\n",
168+
"\n",
169+
"tic = time.clock()\n",
170+
"for i in xrange(n_times):\n",
171+
" f_ifelse(val1, val2, big_mat1, big_mat2)\n",
172+
"print 'time spent evaluating one value %f sec' % (time.clock() - tic)"
173+
]
174+
}
175+
],
176+
"metadata": {
177+
"kernelspec": {
178+
"display_name": "Python 2",
179+
"language": "python",
180+
"name": "python2"
181+
},
182+
"language_info": {
183+
"codemirror_mode": {
184+
"name": "ipython",
185+
"version": 2
186+
},
187+
"file_extension": ".py",
188+
"mimetype": "text/x-python",
189+
"name": "python",
190+
"nbconvert_exporter": "python",
191+
"pygments_lexer": "ipython2",
192+
"version": "2.7.6"
193+
}
194+
},
195+
"nbformat": 4,
196+
"nbformat_minor": 0
197+
}

09. theano/09.06 graph structures.ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3f389a74d04967245fcfa5d4a0beb383bac1892e

09. theano/09.07 softmax on mnist.ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7ffb9c93e0803a4d5191f25a9b5fc41a4303258b

09. theano/09.08 net.ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b560eed66baf65fe8138ad0e4a86ad711e262301
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8cc3cb86c9bf006a8ff1c1aa7142de47e9f5d626
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b1d7d6912e731f36d20a43170ccb234ab14e2345

README.md.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fe8c5f7007d1e7eeed5964012cbf361a46a77774
1+
ed87ddfcb9ad066c720ba2b22ff49ff5419ce600

generate index.ipynb.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3825657ce3f0755c059cb267f6891eab1ca17859
1+
357ff11713871daeb12059449831313bfac22851
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3b9d5e0e5d07fc34e2adce4ffdb8337721362a05
1+
0af47a8b54290e4c30ae0bad0fd0d344efa457a8

index.ipynb.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6473d67de2905f70a89195eb12c399283bbff027
1+
efdd25938c38bb6eb75a401b90b067e659434b97

index.md.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
431acf0104e674b63a4d7bed9a984756e28d65a5
1+
f0b84ed9100ca6c0175dec84eefa5e304c64191b

0 commit comments

Comments
 (0)