学习资源:

Numpy

  • Matrix arithmetic
  • Tensor (aka. “arrays”)(note the name: Tensorflow)
  • 1-D tensor(vector), 2-D tensor(matix)
  • Arithmetic: +,-,*,/
  • Matrix multiply == dot/inner product (np.dot)
  • Element-wise multiply(*)

Matplotlib

  • Line charts
  • Scatter plots

Pandas

  • Loading in tabular data

Scipy(power version of numpy)

  • Satistics, optimization, linear algebra, signal processing

Scikit-Learn

  • basic machine learning
  • Classification and regression
  • Machine learning as geometry rather than magic
  • Shape of data(X and Y)

The Steps of a Machine Learning Script

  • Load in the data
    1. X and Y
    2. Typically use Pandas unless the data is to complex
  • Split into train/test sets (sometimes)
    1. Sometimes “test” and “validation” are used interchangebly, and the “true test set” is something else- don’t get frazzled over this.
  • Build a model
    1. OOP (object oriented programming)
    2. Tensorflow 2.0 standard is Keras API, very similar to Scikit-learn
  • Fit the model(gradinet descent)
    1. model.fit(X,Y)
  • Evaluate the model
  • Make predictions
    1. model.predict(X)

Outline

What is machine learning

look for patterns for data and make better prediction in furture.

traning a nueral network is just to find a weight of minimum of cost function.

Classfication and Regression

Classfication:

  • predict a category/ lable
  • yes or not / dog or cat
  • SVM

Regression: (more features,nonlinear makes problem complicated)

  • Fit a line or curve

 = mx + b

The Decision Rule

  • sigmoid functions

 = σ(a), a = w1x1 + w2x2 + b

Probabilistic Interpretation

p(y = 1|x) = σ(a)

Logistic Regression with >2 Inputs

  • What if we have > 2 input?
  • Use matrices/ vectors

$p(y= 1| x)=\sigma(w^T x + b)= \sigma (\sum_{d=1}^D w_d x_d +b)$

Sigmoid neurons are similar to perceptrons, but modified so that small changes in their weights and bias cause only a small change in their output.

Those say that $\triangle output$ is a linear function of the changes $\triangle w_j$ and $\triangle b$ in the weights and bias. make it much easier to figure out how changing the weights and biases will change the output.

sigmoid function is defined by:

https://i.loli.net/2020/12/13/pjS2kOK9HsB1weT.png

ReLU may be even more biological plausible than sigmoid;

pRlCeYALUZmhbVT

https://i.loli.net/2020/12/13/pjS2kOK9HsB1weT.png

In Tnesorflow 2.0

  • Is mostly an exercise in “API hunting”
  • The actual math isn’t written in code (by you)
  • Your job is to find the function that implements the math in question
  • tf.keras.layers.Dense(Output_size)
1
model = tf.keras.models.Sequential([    tf.keras.layers.Input(shape=(D,)),    tf.keras.layers.Dense(1, activation='sigmoid')])
  • cost function
1
model.compile (optimizer = 'adam',                             loss = 'binary_crossentropy',                             metrics = ['accuracy'])

https://i.loli.net/2020/12/13/TpoVjywRnrqxOMX.png

  • Accuracy
1
model.compile(optimizer='adam',                            loss = 'binary_crossentropy',                            metrics = ['accuracy'])
  • Training / Fitting
1
r = model.fit (X_train, y_train,                             validation_data = (X_test, y_test),                             epochs=100)plt.plot(r.history['loss'],label='loss')plt.plot(r.history['val_loss'],label='val_loss')

To be conclude,

  • Prediction = Round( σ(Wx + b) )
  • Implemented with Keras Dense layer
  • compile() to specify optimizer (adam), loss (binary cross entropy), metrics(accuracy)
  • fit() returns a history object, so we can plot loss per iteration

Loss

1
model.compile (optimizer = tf.keras.optimizers.SGD(0.001, 0.9), loss='mse')
  • MSE = mean squared error

$MSE={1\over N} \sum_{i=1}^N (y_i -\hat{y_i})^2$

https://i.loli.net/2020/12/13/ovTb9lGDyKBLEWH.png

Minimizing Cost

https://i.loli.net/2020/12/13/ivJCbIKwQyXa1lW.png

The multidimensional equivalnet of derivative is the gradient

$\nabla_w J =({\partial J\over \partial w_1},{\partial J\over \partial w_2},…,{\partial J \over \partial w_D})$

$Set: \nabla_w J =0, solve \space for \space w$

TensorFlow done this for us by automatic differentitation

  • Tensorflow automatically finds the gradient of loss wrt all tour model weights
  • Tensorflow uses the gradients to train your model

Gradient Descent

  • In most cases, it is not possible to solve for $anbla J=0$, using training iterative instead. (Linear Regression is the exception.)
  • gradient descent algorithm is inside the Keras fit function:
1
2
3
4
5
6
w,b = randomly initialized
for epoch in range (epochs):
w = w - \eta * \nabla_w J
b = b - \eta * \nabla_b J
# \eta is called learning rate

https://i.loli.net/2020/12/13/6DtZx4F1NLkhCyq.png

https://i.loli.net/2020/12/13/3QxRBDhT6oa4FnK.png

https://i.loli.net/2020/12/13/h8V9EMfx7uadzYk.png

The aim of our traning algorithm will be to minimize the cost $C(w,b)$ as a fucntion of the weights and biases. In other words, we want to find a set of weights and biases which make the cost as small as possible.

Gradient descent can be viewed as a way of taking small steps in the direction which does the most to immediately decrease C.

Stochastic gradient descent(SGD) used to speed uop learning.

The idea is to estimate the gradient $\nabla C$ by computing $\nabla C_x$ for a small sample of randomly chosen training inputs. By averaging over this small sample it turns out that we can quickly get a good estimate of the true gradient $\nabla C$/

The Cauchy-Schwarz inequality:

for all vectors u and v of an inner product space, it is true that:

构建一个network(2,3,1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Network(object):

def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y,1) for y in sizes[1:]]
self.weights= [np.random.randn(y,x)
for x,y in zip(sizes[:-1], sizes[1:])]
net = Network([2, 3, 1])

# Out:
# The biases and weights in the Network object are all initialized randomly
# using the Numpy np.random.randn function to generate Gaussian distributions with mean 0 and standard deviation 1
biases= [array([[-1.53072684],
[-0.08241161],
[-1.59648056]]),
array([[-0.85710365]])]
weights= [array([[ 1.08006753, -0.59936474], # 代表虚线
[-0.57578445, -0.50663782],
[ 1.16481607, 0.06044497]]),
array([[ 0.5560195 , -2.12356058, 2.19147343]])]

network

接下来,就要计算每一层的输出:

1
2
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
1
2
3
4
5
def feedforward(self, a):
"""Return the output of the network if "a" is input."""
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a)+b)
return a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def SGD(self, training_data, epochs, mini_batch_size, eta,
test_data=None):
"""Train the neural network using mini-batch stochastic
gradient descent. The "training_data" is a list of tuples
"(x, y)" representing the training inputs and the desired
outputs. The other non-optional parameters are
self-explanatory. If "test_data" is provided then the
network will be evaluated against the test data after each
epoch, and partial progress printed out. This is useful for
tracking progress, but slows things down substantially."""
"""eta is the learning rate. If the optional argument test_data is
supplied, then the program will evaluate the network after each
epoch of training, and print out partial progress. This is useful
for tracking progress, but slows things down substantially."""
if test_data: n_test = len(test_data)
n = len(training_data)
for j in xrange(epochs):
random.shuffle(training_data) #洗牌
mini_batches = [
training_data[k:k+mini_batch_size]
for k in xrange(0, n, mini_batch_size)]
#for each mini_batch we apply a single step of gradient descent,which updates the network weights and biases according to a single iteration of gradient descent.
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta)
if test_data:
print "Epoch {0}: {1} / {2}".format(
j, self.evaluate(test_data), n_test)
else:
print "Epoch {0} complete".format(j)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def update_mini_batch(self, mini_batch, eta):
"""Update the network's weights and biases by applying
gradient descent using backpropagation to a single mini batch.
The "mini_batch" is a list of tuples "(x, y)", and "eta"
is the learning rate."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
# the backpropagation algorithm,which is a fast way of computing the gradient of the cost function.
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights =[w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]

具体代码可以在github上找到:地址 python3.5版本

1
2
3
4
Finished training network.
Best validation accuracy of 98.98% obtained at iteration 234999
Corresponding test accuracy of 99.07%

如何计算backprogation?

​ 该算法的核心是快速计算相对于每一个weight w 和每一个bias b的cost function 的偏导数:

weight

先给weights一个标记,如上图所示。

basis

同时,还有basis的标签。

这样我们就有了输入输出之间的联系:

写成矩阵的形式,或者说爱因斯坦简化形式:

突然发现,这个$w_{jk}^l$其实就是所谓的三阶张量呀,但这个张量没层数量可能不相同!怪不得叫tensorflow。

对于cost function的两个假设

  • the cost function 为单个训练集的平均,即

  • the cost is a function of the outputs a:

例如,quadratic cost:

接着,引入中间变量,error $\delta_j^l$

我们将得到四个关于误差的表达式(chain rule)

An equation for the error in the output layer, $\delat^L$:

​ 简写为Hadmard product形式:

简写为矩阵形式:

$\sigma’(z_j^L)$无非就是sigma函数的的在$z_j^l$的导数,计算得到$z_j^l$后,在其附近添加一个差值,利用差分非常好求。C是给定的一个形式,例如上面的quadratic cost,那对a求导也非常容易,得:

​ y是标签的值,a是训练的得到的值。

An equation for the error $\delta^l$ in terms of the error in the next layer, $\delat^{l+1}$:

Differentiating, we obtain

Substituting back we obtain:

矩阵形式:

其中,$(w^{l+1})^T$ is the transpose of the weight matrix $w^{l+1}$ for the $(l+1)^{th}$ layer. 假定我们知道上一$l+1$层的误差$\delta^{l+1}$。When we apply the transpose weight matrix, $(w^{l+1})^T$, we can think intuitively of this as moving the error backward through the network, giving us some sort of meaure of the error at the output of the l layer.

如此,通过计算L层的误差,然后通过迭代就可以倒退回每一层的误差。

如果将其在RNN上应用,类似于dynamical system里也有类似累积的效果,每一层相当于每一个时间步。误差类比于流动稳定性。流动在某个临界条件会发生不稳定性,往往这个临界点是我们非常关心的。。。。后面如何和chaotic系统结合还有待思考🤔貌似一个是正问题,一个是逆问题。

  • 后面MIT的课程上,通过forward propagation dual number 来求解。ODE关联,优化问题。都是梯度下架问题。在ODE问题中,用隐式算法,同样会涉及逆问题。最后,貌似都转化为同一个问题。

An equation for the rate of change of the cost with respect to any bias in the network:

通过这个公式,我们就获得了b的偏导数。

An equation for the rate of change of the cost with respect to any weight in the net work:

24ALMmlORrDUseg

When $a_in$ is small, the gradient terms ${\partial C \over \partial w}$ will also tend to be small, it learns slowly. — low-activation

Besides, consider the term $\sigma’(z_j^L)$ become very flat when approach to 0 or 1, it also learns slowly. — saturated

The backprogation algorithm

  1. Input x: Set the corresponding activation $a^1$ for the input layer.

  2. Feedforward: For each l=2,3,…,L compute $z^l = w^l a^{l-1}+b^l$ and $a^l=\sigma(z^l)$.

  3. Output error $\delta^L$: Compute the vector $\delta^L = \nabla_a C \odot \sigma’(z^L).$

  4. Backpropagate the error: For each l = L-1,L-2,…,2 compute

  5. Output: The gradient of the cost function is given by

The backprogation algorithm with a set of training examples

​ 计算过程不变,只不过最终梯度下架为计算的平均值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Network(object):
...
def update_mini_batch(self, mini_batch, eta):
"""Update the network's weights and biases by applying
gradient descent using backpropagation to a single mini batch.
The "mini_batch" is a list of tuples "(x, y)", and "eta"
is the learning rate."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Network(object):
...
def backprop(self, x, y):
"""Return a tuple "(nabla_b, nabla_w)" representing the
gradient for the cost function C_x. "nabla_b" and
"nabla_w" are layer-by-layer lists of numpy arrays, similar
to "self.biases" and "self.weights"."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = self.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# Note that the variable l in the loop below is used a little
# differently to the notation in Chapter 2 of the book. Here,
# l = 1 means the last layer of neurons, l = 2 is the
# second-last layer, and so on. It's a renumbering of the
# scheme in the book, used here to take advantage of the fact
# that Python can use negative indices in lists.
for l in xrange(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)

...

def cost_derivative(self, output_activations, y):
"""Return the vector of partial derivatives \partial C_x /
\partial a for the output activations."""
return (output_activations-y)

def sigmoid(z):
"""The sigmoid function."""
return 1.0/(1.0+np.exp(-z))

def sigmoid_prime(z):
"""Derivative of the sigmoid function."""
return sigmoid(z)*(1-sigmoid(z))

learning rate is low, means that the partial derivatives of the cost function, $\partial C/\partial w$ and $\partial C / \partial b$ is small.

Better choice of cost function: the cross-entropy

对单个输入的神经网络来说,

则带来的好处是:

训练误差越大,则学习速度越快!

“Regularization” methods (L1 and L2 regularization, dropout, and artificial expansion of the traning data)

过拟合问题。

regularized cross-entropy

The effect of regularization is to make it so the network prefers to learn small weights, all other things being equal. Regularization can be viewed as a way of compromising between finding small weights and minimizing the original cost function.

L1 regularization

相比L2 regularization,

In both expressions, the effect of regularization is to shrink the weights. This accords with our intuition that both kinds of regularization penalize large weights(罚函数)。In L1 regularization, the weights shrink by a constant amount toward 0. In L2 regularization, the weights shrink by an amount which is proportion to w. And so when a particular weight has a large magnitude, |w|, L1 regularization shrinks the weight much less than L2 regularization does. By contrast, when |w| is small, L1 regularization shrinks the weight much more than L2 regularization. z最终

在这两个表达式中,正则化的效果都是缩小权重。这符合我们的直觉,即两种正则化都会惩罚较大的权重。但是权重缩减的方式是不同的。在L1正则化中,权重保持不变。在L2正则化中,权重与w成比例。因此,当|w|较大时,L1正则化比L2正则化缩小的慢。相比之下,当|w|较小,L1正则化比L2正则化压缩权重更多。最终结果是,L1正则化倾向于将网络的权重集中在相对少量的高重要性连接中,而其他权重则趋向于零。

Dropout

modify the network itself.

改进代码执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""network2.py
~~~~~~~~~~~~~~

An improved version of network.py, implementing the stochastic
gradient descent learning algorithm for a feedforward neural network.
Improvements include the addition of the cross-entropy cost function,
regularization, and better initialization of network weights. Note
that I have focused on making the code simple, easily readable, and
easily modifiable. It is not optimized, and omits many desirable
features.

"""

#### Libraries
# Standard library
import json
import random
import sys

# Third-party libraries
import numpy as np


#### Define the quadratic and cross-entropy cost functions

class QuadraticCost(object):

@staticmethod # just as ordinary but now pass self
def fn(a, y):
"""Return the cost associated with an output ``a`` and desired output
``y``.

"""
return 0.5*np.linalg.norm(a-y)**2

@staticmethod
def delta(z, a, y):
"""Return the error delta from the output layer."""
return (a-y) * sigmoid_prime(z)


class CrossEntropyCost(object):

@staticmethod
def fn(a, y):
"""Return the cost associated with an output ``a`` and desired output
``y``. Note that np.nan_to_num is used to ensure numerical
stability. In particular, if both ``a`` and ``y`` have a 1.0
in the same slot, then the expression (1-y)*np.log(1-a)
returns nan. The np.nan_to_num ensures that that is converted
to the correct value (0.0).

"""
return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))

@staticmethod
def delta(z, a, y):
"""Return the error delta from the output layer. Note that the
parameter ``z`` is not used by the method. It is included in
the method's parameters in order to make the interface
consistent with the delta method for other cost classes.

"""
return (a-y)


#### Main Network class
class Network(object):

def __init__(self, sizes, cost=CrossEntropyCost):
"""The list ``sizes`` contains the number of neurons in the respective
layers of the network. For example, if the list was [2, 3, 1]
then it would be a three-layer network, with the first layer
containing 2 neurons, the second layer 3 neurons, and the
third layer 1 neuron. The biases and weights for the network
are initialized randomly, using
``self.default_weight_initializer`` (see docstring for that
method).

"""
self.num_layers = len(sizes)
self.sizes = sizes
self.default_weight_initializer()
self.cost=cost

def default_weight_initializer(self):
"""Initialize each weight using a Gaussian distribution with mean 0
and standard deviation 1 over the square root of the number of
weights connecting to the same neuron. Initialize the biases
using a Gaussian distribution with mean 0 and standard
deviation 1.

Note that the first layer is assumed to be an input layer, and
by convention we won't set any biases for those neurons, since
biases are only ever used in computing the outputs from later
layers.

"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)/np.sqrt(x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]

def large_weight_initializer(self):
"""Initialize the weights using a Gaussian distribution with mean 0
and standard deviation 1. Initialize the biases using a
Gaussian distribution with mean 0 and standard deviation 1.

Note that the first layer is assumed to be an input layer, and
by convention we won't set any biases for those neurons, since
biases are only ever used in computing the outputs from later
layers.

This weight and bias initializer uses the same approach as in
Chapter 1, and is included for purposes of comparison. It
will usually be better to use the default weight initializer
instead.

"""
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]

def feedforward(self, a):
"""Return the output of the network if ``a`` is input."""
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a)+b)
return a

def SGD(self, training_data, epochs, mini_batch_size, eta,
lmbda = 0.0,
evaluation_data=None,
monitor_evaluation_cost=False,
monitor_evaluation_accuracy=False,
monitor_training_cost=False,
monitor_training_accuracy=False):
"""Train the neural network using mini-batch stochastic gradient
descent. The ``training_data`` is a list of tuples ``(x, y)``
representing the training inputs and the desired outputs. The
other non-optional parameters are self-explanatory, as is the
regularization parameter ``lmbda``. The method also accepts
``evaluation_data``, usually either the validation or test
data. We can monitor the cost and accuracy on either the
evaluation data or the training data, by setting the
appropriate flags. The method returns a tuple containing four
lists: the (per-epoch) costs on the evaluation data, the
accuracies on the evaluation data, the costs on the training
data, and the accuracies on the training data. All values are
evaluated at the end of each training epoch. So, for example,
if we train for 30 epochs, then the first element of the tuple
will be a 30-element list containing the cost on the
evaluation data at the end of each epoch. Note that the lists
are empty if the corresponding flag is not set.

"""
if evaluation_data: n_data = len(evaluation_data)
n = len(training_data)
evaluation_cost, evaluation_accuracy = [], []
training_cost, training_accuracy = [], []
for j in xrange(epochs):
random.shuffle(training_data)
mini_batches = [
training_data[k:k+mini_batch_size]
for k in xrange(0, n, mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(
mini_batch, eta, lmbda, len(training_data))
print "Epoch %s training complete" % j
if monitor_training_cost:
cost = self.total_cost(training_data, lmbda)
training_cost.append(cost)
print "Cost on training data: {}".format(cost)
if monitor_training_accuracy:
accuracy = self.accuracy(training_data, convert=True)
training_accuracy.append(accuracy)
print "Accuracy on training data: {} / {}".format(
accuracy, n)
if monitor_evaluation_cost:
cost = self.total_cost(evaluation_data, lmbda, convert=True)
evaluation_cost.append(cost)
print "Cost on evaluation data: {}".format(cost)
if monitor_evaluation_accuracy:
accuracy = self.accuracy(evaluation_data)
evaluation_accuracy.append(accuracy)
print "Accuracy on evaluation data: {} / {}".format(
self.accuracy(evaluation_data), n_data)
print
return evaluation_cost, evaluation_accuracy, \
training_cost, training_accuracy

def update_mini_batch(self, mini_batch, eta, lmbda, n):
"""Update the network's weights and biases by applying gradient
descent using backpropagation to a single mini batch. The
``mini_batch`` is a list of tuples ``(x, y)``, ``eta`` is the
learning rate, ``lmbda`` is the regularization parameter, and
``n`` is the total size of the training data set.

"""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw #L2
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]

def backprop(self, x, y):
"""Return a tuple ``(nabla_b, nabla_w)`` representing the
gradient for the cost function C_x. ``nabla_b`` and
``nabla_w`` are layer-by-layer lists of numpy arrays, similar
to ``self.biases`` and ``self.weights``."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = (self.cost).delta(zs[-1], activations[-1], y)
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# Note that the variable l in the loop below is used a little
# differently to the notation in Chapter 2 of the book. Here,
# l = 1 means the last layer of neurons, l = 2 is the
# second-last layer, and so on. It's a renumbering of the
# scheme in the book, used here to take advantage of the fact
# that Python can use negative indices in lists.
for l in xrange(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)

def accuracy(self, data, convert=False):
"""Return the number of inputs in ``data`` for which the neural
network outputs the correct result. The neural network's
output is assumed to be the index of whichever neuron in the
final layer has the highest activation.

The flag ``convert`` should be set to False if the data set is
validation or test data (the usual case), and to True if the
data set is the training data. The need for this flag arises
due to differences in the way the results ``y`` are
represented in the different data sets. In particular, it
flags whether we need to convert between the different
representations. It may seem strange to use different
representations for the different data sets. Why not use the
same representation for all three data sets? It's done for
efficiency reasons -- the program usually evaluates the cost
on the training data and the accuracy on other data sets.
These are different types of computations, and using different
representations speeds things up. More details on the
representations can be found in
mnist_loader.load_data_wrapper.

"""
if convert:
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
for (x, y) in data]
else:
results = [(np.argmax(self.feedforward(x)), y)
for (x, y) in data]
return sum(int(x == y) for (x, y) in results)

def total_cost(self, data, lmbda, convert=False):
"""Return the total cost for the data set ``data``. The flag
``convert`` should be set to False if the data set is the
training data (the usual case), and to True if the data set is
the validation or test data. See comments on the similar (but
reversed) convention for the ``accuracy`` method, above.
"""
cost = 0.0
for x, y in data:
a = self.feedforward(x)
if convert: y = vectorized_result(y)
cost += self.cost.fn(a, y)/len(data)
cost += 0.5*(lmbda/len(data))*sum(
np.linalg.norm(w)**2 for w in self.weights)
return cost

def save(self, filename):
"""Save the neural network to the file ``filename``."""
data = {"sizes": self.sizes,
"weights": [w.tolist() for w in self.weights],
"biases": [b.tolist() for b in self.biases],
"cost": str(self.cost.__name__)}
f = open(filename, "w")
json.dump(data, f)
f.close()

#### Loading a Network
def load(filename):
"""Load a neural network from the file ``filename``. Returns an
instance of Network.

"""
f = open(filename, "r")
data = json.load(f)
f.close()
cost = getattr(sys.modules[__name__], data["cost"])
net = Network(data["sizes"], cost=cost)
net.weights = [np.array(w) for w in data["weights"]]
net.biases = [np.array(b) for b in data["biases"]]
return net

#### Miscellaneous functions
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the j'th position
and zeroes elsewhere. This is used to convert a digit (0...9)
into a corresponding desired output from the neural network.

"""
e = np.zeros((10, 1))
e[j] = 1.0
return e

def sigmoid(z):
"""The sigmoid function."""
return 1.0/(1.0+np.exp(-z))

def sigmoid_prime(z):
"""Derivative of the sigmoid function."""
return sigmoid(z)*(1-sigmoid(z))

Tensorflow 执行代码:参考

Classfication:

https://colab.research.google.com/drive/1M_otG1koa2qwix4-ScwZUzF_NB9rg4vD?usp=sharing

Regression:

https://colab.research.google.com/drive/1k-sj3NP0YNoaIXVNdaXF9WtyY9sMqosR#scrollTo=x5BY2x58MBrt

Aritificial Neural Networks(ANNS)

https://i.loli.net/2020/12/13/KPc9zZ4hNn3CMta.png

Feedforward neural networks

the signal just goes from left to right, we call it a “feedforward” neural network

https://i.loli.net/2020/12/13/JOzT1kxHw9KvEQy.png

Different neural will find different features with inputs

https://i.loli.net/2020/12/13/zKkhGtpC2fgbrvU.png

  • a line : ax+b
  • a neuron: $\sigma(w^T x+b)$
  • Multiple Neurons per Layer:

$z_j = \sigma(w_j^T x+ b_j),for \space j=1…M$

Vectorize the neuron:

$Z=\sigma(W^T x +b)$

这一个单元:将M个特征的信息通过DxM的矩阵W转化到D个特征,然后非常类似于着色器里的sigma函数操作。整个过程一气呵成,GPU的作用可见一斑。

Each neural network layer is a “feature transform”

这种转化在neural network一层一层的迭代:

$z^{(1)} = \sigma(W^{(1)T}x + b^{(1)})\\$$z^{(2)}=\sigma(W^{2}T)z^{(1)}+b^{(2)})$

$z^{(3)}=\sigma(W^{(3)T}z^{(2)}+b^{3})$

$p(y=1|x)=\sigma(W^{(L)T}z^{(L-1)}+b^{(L)})$

https://i.loli.net/2020/12/13/k1sSbpq6CTvflh2.png

The Geometric Picture

  • The neuron is nice and interpretable
    • Large weights = important feature
    • Small weights = not import feature

https://i.loli.net/2020/12/13/ke3L6GMTpzJC5o7.png

$\hat{y} = ax^2 + bx + c, \\ Let: x_1 =x, x_2= x^2\\ Then: \hat{y} = w_1 x_1 + w_2 x_2 +b$

It’s nonlinear because of the sigmoid

$W^{(2)T} \sigma(W^{(1)T}x+b^{(1)})+b^{(2)}$

Convolutional Neural Networks (CNNs)

sHe25ZzqSg4FtI1

更适合图像。

Recurrent Neural Networks(RNNs)- for sequence

a later neuron may connect back to an earlier neuron to created a recurrent connection

Recurrent Neural Networks:

bs2FvGAeqjaTSch

zkrQYxSlXnGqf8m

VK1gY5P6dJcBWHN

A recurrent neural network is a class of artificial neural networks where connections between nodes form a directed graph along a temporal sequence. This allows it to exhibit temporal dynamic behavior. Derived from feedforward neural networks, RNNs can use their internal state to process variable length sequences of inputs. This makes them applicable to tasks such as unsegmented, connected handwriting recognition or speech recognition.

The main use of RNNs are when using google or Facebook these interfaces are able to predict the next word that you are about to type. RNNs have loops to allow information to persist. This reduces the complexity of parameters, unlike other neural networks. These neural nets are considered to be fairly good for modeling sequence data. RNNs keep track of a hidden state, that encodes information about the history of the system dynamics.

RNNs have the potential to overcome these scalability problems and be applied to high-dimensional spatio-temporal dynamics.

Recurrent neural networks are a linear architectural variant of recursive networks. They have a “memory” thus it differs from other neural networks. This memory remembers all the information about, what has been calculated in the previous state. It uses the same parameters for each input as it performs the same task on all the inputs or hidden layers to produce the output.

One of the biggest uniqueness RNNs have is “UAP- Universal Approximation Property” thus they can approximate virtually any dynamical system. This unique property forces us to say recurrent neural networks have something magical about them.

RNNs model has been proven to perform extremely well on temporal data. It has several variants including LSTMs (long short-term memory), GRUs (gated recurrent unit) and Bidirectional RNNs. Building models for natural language, audio files, and other sequence data got a lot easier with sequence algorithms.

Vanishing and Exploding Gradient Problem-limited

This problem sent the major set back for RNNs to get popularity. Values in RNNs can explode or vanish due to a simple reason for remembering previous values. Previous values are good enough to confuse them and cause current values to keep increasing or decreasing & take-over algorithm. Indefinite loops get formed that brings whole network halt.

For example, neurons might get stuck in the loop where it keeps multiplying the previous number to new number which can go to infinity if all numbers are more than one or get stuck at zero if any number is zero.

If the weight is small, multiply it all the time, it will become small. Otherwise it will become very large.这样深度神经网络的训练也会失效。从深度网络的角度来讲,不同的层学习的速度差异很大,表现为网络中靠近输出的层学习的情况很好,靠近输入的层学习的很慢,有时甚至训练了很久,前几层的权值和刚开始随机初始化的值差不多。因此,梯度消失、爆炸,其根本原因在于反向传播训练法则backpropagation (BPTT)**,属于先天不足。

Reasons for the disappearance of the gradient:

  1. Neural network
  2. The initialization weight is too small
  3. Activation function sigmoid (Relu)

Reasons for gradient explosion:

  1. Neural network
  2. The initialization weight is too large

How to solve it?

  1. Pre-training + fine-tuning

其基本思想是每次训练一层隐节点,训练时将上一层隐节点的输出作为输入,而本层隐节点的输出作为下一层隐节点的 输入,此过程就是逐层“预训练”;在训练完成后,再对整个网络进行“微调”

  1. Gradient shear, regular

梯度剪切这种方案主要是针对梯度爆炸问题,其思想是设置一个梯度阀值。

  1. Long Short-Term Memory (LSTM)

LSTMs employ gates that effectively remember and forget information thus alleviating the problem of vanishing gradients,可以缓解Vanishing and Exploding Gradient Problem。复杂的门可以接下来更新的时候“记住”前几次训练的“残留记忆”。

中间的具体对应关系参考下面的建模方法的图c,有点复杂: $$
gt^f=\delta_f (W_f[h{t-1},ot]+b_f) , \space\space g_t^i =\delta_i (W_i[h{t-1},ot]+b_i)\\
\tilde{c}_t = tanh(W_c[h
{t-1},ot]+b_t), \space c_t = g_t^f \odot c{t-1} +gt^i \odot \tilde{c}_t\\
g_t^o = \delta_h(W_h[h
{t-1},o_t]+b_h), \space\space h_t =g_t^o \odot tanh(c_t)

$$ 有点像电路板,gtf, gti, gto分别代表forget, input, output gates. ot is the observable input at time t, ht is the hidden state, ct is the cell state, W is weight matrices, b is biases. The symbol ⊙ denotes the element-wise product. δ is Sigmoid function. The dimension of the hidden state dh (number of hidden units) controls the capacity of the cell to encode history information. The hidden-to-output functional form fho is given by a linear layer: ôt + 1 = Woht, Wo ∈ Rd0 × dh

Reservior Computing

Reservoir computing is a framework for computation derived from recurrent neural network theory that maps input signals into higher dimensional computational spaces through the dynamics of a fixed, non-linear system called a reservoir.[1] After the input signal is fed into the reservoir, which is treated as a “black box,” a simple readout mechanism is trained to read the state of the reservoir and map it to the desired output.[1] The first key benefit of this framework is that training is performed only at the readout stage, as the reservoir dynamics are fixed.[1] The second is that the computational power of naturally available systems, both classical and quantum mechanical, can be utilized to reduce the effective computational cost.[2]

建模方法

机器学习预测时间序列。给定等间隔1/trianglet采样,可观测序列o ∈ Rdo ,{o1, …, oT}。输入前一个时间点的状态ht ∈ Rdh. ht = fhh(ot, ht − 1), ôt + 1 = fho(ht) fhh is hidden-to-hidden mapping, fho is the hidden-to-output mapping. 下面这四种模型都是这种结构,只是对f的实现和参数有所差异。参见:4

https://cdn.jsdelivr.net/gh/Jiaqi-knight/imgBase@main/2020-12-1182528.png

示意图

具体算法请参考