mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-03 22:45:12 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import theano
|
||||
import theano.tensor as T
|
||||
import numpy as np
|
||||
@@ -16,7 +16,7 @@ def floatX(X):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
```
|
||||
@@ -27,7 +27,7 @@ Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
def dropout(X, prob=0.):
|
||||
if prob > 0:
|
||||
X *= srng.binomial(X.shape, p=1-prob, dtype = theano.config.floatX)
|
||||
@@ -50,7 +50,7 @@ $$ \begin{aligned} & h_1 = \text{rectify}(W_{h_1} \ x) \\ & h_2 = \text{rectify}
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
def softmax(X):
|
||||
e_x = T.exp(X - X.max(axis=1).dimshuffle(0, 'x'))
|
||||
return e_x / e_x.sum(axis=1).dimshuffle(0, 'x')
|
||||
@@ -85,7 +85,7 @@ def model(X, w_h1, w_h2, w_o, p_drop_input, p_drop_hidden):
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
def init_weights(shape):
|
||||
return theano.shared(floatX(np.random.randn(*shape) * 0.01))
|
||||
|
||||
@@ -99,7 +99,7 @@ w_o = init_weights((625, 10))
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
X = T.matrix()
|
||||
Y = T.matrix()
|
||||
|
||||
@@ -109,7 +109,7 @@ Y = T.matrix()
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
def RMSprop(cost, params, accs, lr=0.001, rho=0.9, epsilon=1e-6):
|
||||
grads = T.grad(cost=cost, wrt=params)
|
||||
updates = []
|
||||
@@ -127,7 +127,7 @@ def RMSprop(cost, params, accs, lr=0.001, rho=0.9, epsilon=1e-6):
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
# 有 dropout,用来训练
|
||||
noise_h1, noise_h2, noise_py_x = model(X, w_h1, w_h2, w_o, 0.2, 0.5)
|
||||
cost = T.mean(T.nnet.categorical_crossentropy(noise_py_x, Y))
|
||||
@@ -143,7 +143,7 @@ train = theano.function(inputs=[X, Y], outputs=cost, updates=updates, allow_inpu
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
# 没有 dropout,用来预测
|
||||
h1, h2, py_x = model(X, w_h1, w_h2, w_o, 0., 0.)
|
||||
# 预测的结果
|
||||
@@ -156,7 +156,7 @@ predict = theano.function(inputs=[X], outputs=y_x, allow_input_downcast=True)
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
trX, teX, trY, teY = mnist(onehot=True)
|
||||
|
||||
for i in range(50):
|
||||
@@ -166,7 +166,7 @@ for i in range(50):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
iter 001 accuracy: 0.943
|
||||
iter 002 accuracy: 0.9665
|
||||
iter 003 accuracy: 0.9732
|
||||
|
||||
Reference in New Issue
Block a user