mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-30 13:32:26 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
%matplotlib inline
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -11,7 +11,7 @@ import theano.tensor as T
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Using gpu device 0: GeForce GTX 850M
|
||||
|
||||
```
|
||||
@@ -24,7 +24,7 @@ Using gpu device 0: GeForce GTX 850M
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = T.matrix('x')
|
||||
s = 1 / (1 + T.exp(-x))
|
||||
sigmoid = theano.function([x], s, allow_input_downcast=True)
|
||||
@@ -35,7 +35,7 @@ sigmoid = theano.function([x], s, allow_input_downcast=True)
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
sigmoid([[ 0, 1],
|
||||
[-1,-2]])
|
||||
|
||||
@@ -43,7 +43,7 @@ sigmoid([[ 0, 1],
|
||||
|
||||
Out[3]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[ 0.5 , 0.7310586 ],
|
||||
[ 0.26894143, 0.11920293]], dtype=float32)
|
||||
```
|
||||
@@ -52,7 +52,7 @@ array([[ 0.5 , 0.7310586 ],
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
X = np.linspace(-6, 6, 100)
|
||||
X = X[np.newaxis,:]
|
||||
|
||||
@@ -325,7 +325,7 @@ tZIkSUo9i1pJkiSl3v8H7tR/QglpVT0AAAAASUVORK5CYII=
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
s2 = (1 + T.tanh(x / 2)) / 2
|
||||
|
||||
sigmoid2 = theano.function([x], s2)
|
||||
@@ -337,7 +337,7 @@ sigmoid2([[ 0, 1],
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[ 0.5 , 0.7310586 ],
|
||||
[ 0.26894143, 0.11920291]], dtype=float32)
|
||||
```
|
||||
@@ -366,7 +366,7 @@ $$- y \log(h_{w,b}(x)) - (1-y) \log(1-h_{w,b}(x))$$
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
rng = np.random
|
||||
|
||||
# 数据大小和规模
|
||||
@@ -382,7 +382,7 @@ D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = T.matrix('x')
|
||||
y = T.vector('y')
|
||||
|
||||
@@ -396,7 +396,7 @@ b = theano.shared(0., name='b')
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
h = 1 / (1 + T.exp(-T.dot(x, w) - b))
|
||||
|
||||
```
|
||||
@@ -405,7 +405,7 @@ h = 1 / (1 + T.exp(-T.dot(x, w) - b))
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
prediction = h > 0.5
|
||||
|
||||
```
|
||||
@@ -414,7 +414,7 @@ prediction = h > 0.5
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
cost = - T.mean(y * T.log(h) + (1 - y) * T.log(1 - h)) + 0.01 * T.sum(w ** 2) # 正则项,防止过拟合
|
||||
gw, gb = T.grad(cost, [w, b])
|
||||
|
||||
@@ -424,7 +424,7 @@ gw, gb = T.grad(cost, [w, b])
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
train = theano.function(inputs=[x, y],
|
||||
outputs=cost,
|
||||
updates=[[w, w - 0.1 * gw], [b, b - 0.1 * gb]],
|
||||
@@ -438,7 +438,7 @@ predict = theano.function(inputs=[x],
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
for i in xrange(10001):
|
||||
err = train(D[0], D[1])
|
||||
if i % 1000 == 0:
|
||||
@@ -446,7 +446,7 @@ for i in xrange(10001):
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
iter 0, error 19.295896
|
||||
iter 1000, error 0.210341
|
||||
iter 2000, error 0.126124
|
||||
@@ -465,12 +465,12 @@ iter 10000, error 0.124845
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
print D[1]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 1 0 0 0 0 1 0
|
||||
1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0
|
||||
0 1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 1 1 0 1 1 0 1 0 0 1 0 0 1 0 1 1 1 1 0 1 0
|
||||
@@ -487,12 +487,12 @@ print D[1]
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
print predict(D[0])
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0 1 0 1 0 0 1 1 0 0 0 0 1 0
|
||||
1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0
|
||||
0 1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 1 1 0 1 1 0 1 0 0 1 0 0 1 0 1 1 1 1 0 1 0
|
||||
|
||||
Reference in New Issue
Block a user