Skip to content

Commit 27d6664

Browse files
committed
添加了神经网络
Former-commit-id: 8301c71 Former-commit-id: d1225f33ff841e2f09af338566602607427490df [formerly bf3e2630812a87ae1ac3468ec1067091f90977cc] [formerly c8faad4d896cbe00c834d29b3f7c5a4bbb3920a2 [formerly 869ef0b5c9de15a0f0d7d599df90a99cd3799ec4]] [formerly 472d1e5ede4929ed3e3604c6dfaa1936b826cf04 [formerly 01dfea31d93ef3830048dcd6d4718bb3a85383db] [formerly 057df5dc64ff97be5210595298f803ba0355a489 [formerly 1fd58f979ef8c44c69f20065ed7727fcddb4d44b]]] [formerly c63e3c9bfe898a8524655652d6300e9c9178b74b [formerly b05d7c0164b0eed67c673266984cc528cff45b8f] [formerly 88f0cb0b96c598f7cf1db727934fb365fe8fc129 [formerly 99aeba6485e805d15bdafc1908a9a1b8a6485132]] [formerly 147b53b8afb05060eea47c5accb6b645a063de73 [formerly 87a2a06cc76a164cbaf1c9b094712a742aa6f34b] [formerly 7cd17a1144812306f6b9d0b1a2f395bbadc579e8 [formerly 757e60d]]]] Former-commit-id: 98ceeaa9e697523aaf772a61ed8f08e828418815 [formerly 7c88e9e7f2c24d96c4439b6fdf4997b117da3310] [formerly c113d6a25dd25e46d39e468600ee92e5bf0b5c7d [formerly da60d80a79451cb5cd27379d2f23b2f2502d3fae]] [formerly 5ad05370d9616be996fa56664a32e47387285ab1 [formerly 5796cda1f304b5e8b72bf016fb7407fc38a9b882] [formerly 782243b9a7e03fdce73f549376bb769afb0663d7 [formerly 13c14ffa7b594155ec3cfaaa18de3efaaa3db7c4]]] Former-commit-id: 09c1e84e2f12ee5adeff31ad7bb5e386b386450f [formerly 10a2dc6d93b5470217188140e9c06c87e79d07f4] [formerly 692b9990363012964706150bce6c8e80f1cc4f40 [formerly 070bde532d74da1f71e918c964b3a9c1aef914af]] Former-commit-id: 1fde201406af65c9a80310bccd3ac0a41e83326b [formerly a62141753254c0c9cac47a4a253235e6db73167b] Former-commit-id: b0c049a2990bea675d8b3ee6c96cbd664bff2026
1 parent 57881a3 commit 27d6664

8 files changed

+276
-70
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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+
"# 简介"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"`Theano` 是一个 `Python` 科学计算库,允许我们进行符号运算,并在 `CPU` 和 `GPU` 上执行。\n",
22+
"\n",
23+
"它最初由 `Montreal` 大学的机器学习研究者们所开发,用来进行机器学习的计算。\n",
24+
"\n",
25+
"按照[官网](http://deeplearning.net/software/theano/)上的说明,它拥有以下几个方面的特点:\n",
26+
"\n",
27+
"- 与 **Numpy, Scipy** 的紧密结合\n",
28+
"- **GPU** 加速\n",
29+
"- 高效的符号计算\n",
30+
"- 速度和稳定性\n",
31+
"- 动态生成 **C** 代码"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"## 使用 anaconda 安装 theano"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"`windows` 下,使用 `anaconda` 安装 `theano` 的命令为:\n",
46+
"\n",
47+
" conda install mingw libpython\n",
48+
" pip install theano\n",
49+
" \n",
50+
"`linux` 下,使用 `anaconda` 安装的命令为\n",
51+
" \n",
52+
" conda install theano\n",
53+
"\n",
54+
"安装好之后,还需要安装 `Cuda` 并进行 `GPU` 环境的配置,否则是不能利用 `GPU` 进行计算的,推荐使用 `linux/mac` 进行配置,具体方法可以参考[官网](http://deeplearning.net/software/theano/)上的配置说明。\n",
55+
"\n",
56+
"查看安装的版本:"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 1,
62+
"metadata": {
63+
"collapsed": false,
64+
"scrolled": true
65+
},
66+
"outputs": [
67+
{
68+
"data": {
69+
"text/plain": [
70+
"'0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4'"
71+
]
72+
},
73+
"execution_count": 1,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"import theano\n",
80+
"\n",
81+
"theano.__version__"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"查看当前使用的 device:"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 2,
94+
"metadata": {
95+
"collapsed": false
96+
},
97+
"outputs": [
98+
{
99+
"data": {
100+
"text/plain": [
101+
"'cpu'"
102+
]
103+
},
104+
"execution_count": 2,
105+
"metadata": {},
106+
"output_type": "execute_result"
107+
}
108+
],
109+
"source": [
110+
"theano.config.device"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"运行测试:"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": 3,
123+
"metadata": {
124+
"collapsed": false
125+
},
126+
"outputs": [
127+
{
128+
"name": "stderr",
129+
"output_type": "stream",
130+
"text": [
131+
"/usr/local/lib/python2.7/dist-packages/theano/misc/pycuda_init.py:34: UserWarning: PyCUDA import failed in theano.misc.pycuda_init\n",
132+
" warnings.warn(\"PyCUDA import failed in theano.misc.pycuda_init\")\n",
133+
"....................S..............."
134+
]
135+
},
136+
{
137+
"name": "stdout",
138+
"output_type": "stream",
139+
"text": [
140+
"Theano version 0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4\n",
141+
"theano is installed in /usr/local/lib/python2.7/dist-packages/theano\n",
142+
"NumPy version 1.10.1\n",
143+
"NumPy relaxed strides checking option: True\n",
144+
"NumPy is installed in /usr/lib/python2.7/dist-packages/numpy\n",
145+
"Python version 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]\n",
146+
"nose version 1.3.7\n"
147+
]
148+
},
149+
{
150+
"name": "stderr",
151+
"output_type": "stream",
152+
"text": [
153+
"\n",
154+
"----------------------------------------------------------------------\n",
155+
"Ran 37 tests in 37.919s\n",
156+
"\n",
157+
"OK (SKIP=1)\n"
158+
]
159+
},
160+
{
161+
"data": {
162+
"text/plain": [
163+
"<nose.result.TextTestResult run=37 errors=0 failures=0>"
164+
]
165+
},
166+
"execution_count": 3,
167+
"metadata": {},
168+
"output_type": "execute_result"
169+
}
170+
],
171+
"source": [
172+
"theano.test()"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"这里我已经在本地 `Windows` 配好了 `GPU` 的设置,如果没有配好,显示的结果可能不一样。\n",
180+
"\n",
181+
"`Windows` 下第一次运行可能会显示 `DEBUG: nvcc STDOUT` 等内容,**`Just ignore it!`**"
182+
]
183+
}
184+
],
185+
"metadata": {
186+
"kernelspec": {
187+
"display_name": "Python 2",
188+
"language": "python",
189+
"name": "python2"
190+
},
191+
"language_info": {
192+
"codemirror_mode": {
193+
"name": "ipython",
194+
"version": 2
195+
},
196+
"file_extension": ".py",
197+
"mimetype": "text/x-python",
198+
"name": "python",
199+
"nbconvert_exporter": "python",
200+
"pygments_lexer": "ipython2",
201+
"version": "2.7.6"
202+
}
203+
},
204+
"nbformat": 4,
205+
"nbformat_minor": 0
206+
}

09. theano/09.01 introduction and installation.ipynb.REMOVED.git-id

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
df42578b7bd3c7a9cea6ab49118537b543a60ec8
1+
c724d58f499a3332c788a91db5a4bc7888924179
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d00aaf25f64be888e5ce70962a07e9372126f5ba
1+
c5f58238f46e59e2c4256b9b0ad3c55eb3509cba
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e742f36bb55ffc69bcd84b3a7e8cb4d486a918a4
1+
b700b87c50e114ba229377e6248a2ba7591b9a11
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2a207a309f728f12e5f45faadafde766be259372

09. theano/download_mnist.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import os
2-
import os.path
3-
import urllib
4-
import gzip
5-
import shutil
6-
7-
if not os.path.exists('mnist'):
8-
os.mkdir('mnist')
9-
10-
def download_and_gzip(name):
11-
if not os.path.exists(name + '.gz'):
12-
urllib.urlretrieve('http://yann.lecun.com/exdb/' + name + '.gz', name + '.gz')
13-
if not os.path.exists(name):
14-
with gzip.open(name + '.gz', 'rb') as f_in, open(name, 'wb') as f_out:
15-
shutil.copyfileobj(f_in, f_out)
16-
17-
download_and_gzip('mnist/train-images-idx3-ubyte')
18-
download_and_gzip('mnist/train-labels-idx1-ubyte')
19-
download_and_gzip('mnist/t10k-images-idx3-ubyte')
1+
import os
2+
import os.path
3+
import urllib
4+
import gzip
5+
import shutil
6+
7+
if not os.path.exists('mnist'):
8+
os.mkdir('mnist')
9+
10+
def download_and_gzip(name):
11+
if not os.path.exists(name + '.gz'):
12+
urllib.urlretrieve('http://yann.lecun.com/exdb/' + name + '.gz', name + '.gz')
13+
if not os.path.exists(name):
14+
with gzip.open(name + '.gz', 'rb') as f_in, open(name, 'wb') as f_out:
15+
shutil.copyfileobj(f_in, f_out)
16+
17+
download_and_gzip('mnist/train-images-idx3-ubyte')
18+
download_and_gzip('mnist/train-labels-idx1-ubyte')
19+
download_and_gzip('mnist/t10k-images-idx3-ubyte')
2020
download_and_gzip('mnist/t10k-labels-idx1-ubyte')

09. theano/load.py

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
import numpy as np
2-
import os
3-
4-
datasets_dir = './'
5-
6-
def one_hot(x,n):
7-
if type(x) == list:
8-
x = np.array(x)
9-
x = x.flatten()
10-
o_h = np.zeros((len(x),n))
11-
o_h[np.arange(len(x)),x] = 1
12-
return o_h
13-
14-
def mnist(ntrain=60000,ntest=10000,onehot=True):
15-
data_dir = os.path.join(datasets_dir,'mnist/')
16-
fd = open(os.path.join(data_dir,'train-images-idx3-ubyte'))
17-
loaded = np.fromfile(file=fd,dtype=np.uint8)
18-
trX = loaded[16:].reshape((60000,28*28)).astype(float)
19-
20-
fd = open(os.path.join(data_dir,'train-labels-idx1-ubyte'))
21-
loaded = np.fromfile(file=fd,dtype=np.uint8)
22-
trY = loaded[8:].reshape((60000))
23-
24-
fd = open(os.path.join(data_dir,'t10k-images-idx3-ubyte'))
25-
loaded = np.fromfile(file=fd,dtype=np.uint8)
26-
teX = loaded[16:].reshape((10000,28*28)).astype(float)
27-
28-
fd = open(os.path.join(data_dir,'t10k-labels-idx1-ubyte'))
29-
loaded = np.fromfile(file=fd,dtype=np.uint8)
30-
teY = loaded[8:].reshape((10000))
31-
32-
trX = trX/255.
33-
teX = teX/255.
34-
35-
trX = trX[:ntrain]
36-
trY = trY[:ntrain]
37-
38-
teX = teX[:ntest]
39-
teY = teY[:ntest]
40-
41-
if onehot:
42-
trY = one_hot(trY, 10)
43-
teY = one_hot(teY, 10)
44-
else:
45-
trY = np.asarray(trY)
46-
teY = np.asarray(teY)
47-
1+
import numpy as np
2+
import os
3+
4+
datasets_dir = './'
5+
6+
def one_hot(x,n):
7+
if type(x) == list:
8+
x = np.array(x)
9+
x = x.flatten()
10+
o_h = np.zeros((len(x),n))
11+
o_h[np.arange(len(x)),x] = 1
12+
return o_h
13+
14+
def mnist(ntrain=60000,ntest=10000,onehot=True):
15+
data_dir = os.path.join(datasets_dir,'mnist/')
16+
fd = open(os.path.join(data_dir,'train-images-idx3-ubyte'))
17+
loaded = np.fromfile(file=fd,dtype=np.uint8)
18+
trX = loaded[16:].reshape((60000,28*28)).astype(float)
19+
20+
fd = open(os.path.join(data_dir,'train-labels-idx1-ubyte'))
21+
loaded = np.fromfile(file=fd,dtype=np.uint8)
22+
trY = loaded[8:].reshape((60000))
23+
24+
fd = open(os.path.join(data_dir,'t10k-images-idx3-ubyte'))
25+
loaded = np.fromfile(file=fd,dtype=np.uint8)
26+
teX = loaded[16:].reshape((10000,28*28)).astype(float)
27+
28+
fd = open(os.path.join(data_dir,'t10k-labels-idx1-ubyte'))
29+
loaded = np.fromfile(file=fd,dtype=np.uint8)
30+
teY = loaded[8:].reshape((10000))
31+
32+
trX = trX/255.
33+
teX = teX/255.
34+
35+
trX = trX[:ntrain]
36+
trY = trY[:ntrain]
37+
38+
teX = teX[:ntest]
39+
teY = teY[:ntest]
40+
41+
if onehot:
42+
trY = one_hot(trY, 10)
43+
teY = one_hot(teY, 10)
44+
else:
45+
trY = np.asarray(trY)
46+
teY = np.asarray(teY)
47+
4848
return trX,teX,trY,teY

0 commit comments

Comments
 (0)