mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-24 02:23:45 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file download_mnist.py
|
||||
import os
|
||||
import os.path
|
||||
@@ -33,7 +33,7 @@ download_and_gzip('mnist/t10k-labels-idx1-ubyte')
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting download_mnist.py
|
||||
|
||||
```
|
||||
@@ -42,7 +42,7 @@ Overwriting download_mnist.py
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
%run download_mnist.py
|
||||
|
||||
```
|
||||
@@ -53,7 +53,7 @@ In [2]:
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file load.py
|
||||
import numpy as np
|
||||
import os
|
||||
@@ -106,7 +106,7 @@ def mnist(ntrain=60000,ntest=10000,onehot=True):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Overwriting load.py
|
||||
|
||||
```
|
||||
@@ -123,7 +123,7 @@ Overwriting load.py
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
import theano
|
||||
from theano import tensor as T
|
||||
import numpy as np
|
||||
@@ -131,7 +131,7 @@ from load import mnist
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
```
|
||||
@@ -142,7 +142,7 @@ Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
def floatX(X):
|
||||
return np.asarray(X, dtype=theano.config.floatX)
|
||||
|
||||
@@ -155,7 +155,7 @@ def init_weights(shape):
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
A = T.matrix()
|
||||
|
||||
B = T.nnet.softmax(A)
|
||||
@@ -173,7 +173,7 @@ print b.sum(1)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(3, 4)
|
||||
[ 1.00000012 1\. 1\. ]
|
||||
|
||||
@@ -185,7 +185,7 @@ print b.sum(1)
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
def model(X, w):
|
||||
return T.nnet.softmax(T.dot(X, w))
|
||||
|
||||
@@ -195,7 +195,7 @@ def model(X, w):
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
trX, teX, trY, teY = mnist(onehot=True)
|
||||
|
||||
```
|
||||
@@ -204,7 +204,7 @@ trX, teX, trY, teY = mnist(onehot=True)
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
X = T.fmatrix()
|
||||
Y = T.fmatrix()
|
||||
|
||||
@@ -216,7 +216,7 @@ w = init_weights((784, 10))
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
py_x = model(X, w)
|
||||
y_pred = T.argmax(py_x, axis=1)
|
||||
|
||||
@@ -226,7 +226,7 @@ y_pred = T.argmax(py_x, axis=1)
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y))
|
||||
gradient = T.grad(cost=cost, wrt=w)
|
||||
update = [[w, w - gradient * 0.05]]
|
||||
@@ -237,7 +237,7 @@ update = [[w, w - gradient * 0.05]]
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
train = theano.function(inputs=[X, Y], outputs=cost, updates=update, allow_input_downcast=True)
|
||||
predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True)
|
||||
|
||||
@@ -247,7 +247,7 @@ predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True)
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
for i in range(100):
|
||||
for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
|
||||
cost = train(trX[start:end], trY[start:end])
|
||||
@@ -255,7 +255,7 @@ for i in range(100):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
000 0.8862
|
||||
001 0.8985
|
||||
002 0.9042
|
||||
|
||||
Reference in New Issue
Block a user