2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -12,7 +12,7 @@
In [1]:
```
```py
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
@@ -21,7 +21,7 @@ from theano import tensor as T
```
```
```py
Using gpu device 0: GeForce GTX 850M
```
@@ -32,7 +32,7 @@ Using gpu device 0: GeForce GTX 850M
In [2]:
```
```py
a = T.scalar()
b = T.scalar()
@@ -44,7 +44,7 @@ y = a * b
In [3]:
```
```py
multiply = theano.function(inputs=[a, b], outputs=y)
```
@@ -53,13 +53,13 @@ multiply = theano.function(inputs=[a, b], outputs=y)
In [4]:
```
```py
print multiply(3, 2) # 6
print multiply(4, 5) # 20
```
```
```py
6.0
20.0
@@ -71,7 +71,7 @@ print multiply(4, 5) # 20
In [5]:
```
```py
train_X = np.linspace(-1, 1, 101)
train_Y = 2 * train_X + 1 + np.random.randn(train_X.size) * 0.33
@@ -81,7 +81,7 @@ train_Y = 2 * train_X + 1 + np.random.randn(train_X.size) * 0.33
In [6]:
```
```py
plt.scatter(train_X, train_Y)
plt.show()
@@ -209,7 +209,7 @@ RU5ErkJggg==
In [7]:
```
```py
X = T.scalar()
Y = T.scalar()
@@ -219,7 +219,7 @@ Y = T.scalar()
In [8]:
```
```py
X.name = 'x'
Y.name = 'y'
@@ -229,7 +229,7 @@ Y.name = 'y'
In [9]:
```
```py
def model(X, w, b):
return X * w + b
@@ -241,7 +241,7 @@ def model(X, w, b):
In [10]:
```
```py
w = theano.shared(np.asarray(0., dtype=theano.config.floatX))
w.name = 'w'
b = theano.shared(np.asarray(0., dtype=theano.config.floatX))
@@ -253,7 +253,7 @@ b.name = 'b'
In [11]:
```
```py
Y_bar = model(X, w, b)
theano.pp(Y_bar)
@@ -262,7 +262,7 @@ theano.pp(Y_bar)
Out[11]:
```
```py
'((x * HostFromGpu(w)) + HostFromGpu(b))'
```
@@ -270,7 +270,7 @@ Out[11]:
In [12]:
```
```py
cost = T.mean(T.sqr(Y_bar - Y))
grads = T.grad(cost=cost, wrt=[w, b])
@@ -280,7 +280,7 @@ grads = T.grad(cost=cost, wrt=[w, b])
In [13]:
```
```py
lr = 0.01
updates = [[w, w - grads[0] * lr],
[b, b - grads[1] * lr]]
@@ -293,7 +293,7 @@ updates = [[w, w - grads[0] * lr],
In [14]:
```
```py
train_model = theano.function(inputs=[X,Y],
outputs=cost,
updates=updates,
@@ -307,7 +307,7 @@ train_model = theano.function(inputs=[X,Y],
In [15]:
```
```py
for i in xrange(100):
for x, y in zip(train_X, train_Y):
train_model(x, y)
@@ -318,7 +318,7 @@ for i in xrange(100):
In [16]:
```
```py
print w.get_value() # 接近 2
print b.get_value() # 接近 1
@@ -329,7 +329,7 @@ plt.show()
```
```
```py
1.94257426262
1.00938093662