直观描述
神经网络
人工神经网络
人工神经网络 矩阵形式
公式描述
损失微分推导
损失依赖图
[外链图片转存失败(img-pLfoCupO-1565850993249)(http://chuantu.xyz/t6/702/1565850931x1709417261.jpg)]
dLdivDz2
dLdivDw2
损失微分推导 mathmetica固定维度 symbol
1隐层人工神经网络 代码实现
%load_ext ipython_autoimport
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
#make fetch_openml load from local file:
# cp -rv ./openml ~/scikit_learn_data/mldata/
import numpy as np
X, y = mnist["data"], mnist["target"].astype(np.float32)
X = X / 255
digits = 10
examples = y.shape[0]
y = y.reshape(1, examples)
Y_new = np.eye(digits)[y.astype('int32')]
Y_new = Y_new.T.reshape(digits, examples)
m = 60000
m_test = X.shape[0] - m
X_train, X_test = X[:m].T, X[m:].T
Y_train, Y_test = Y_new[:,:m], Y_new[:,m:]
np.random.seed(138)
shuffle_index = np.random.permutation(m)
X_train, Y_train = X_train[:, shuffle_index], Y_train[:, shuffle_index]
# shuffle_index = np.random.permutation(m)
# X_train, y_train = X_train[:,shuffle_index], y_train[:,shuffle_index]
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
def compute_multiclass_loss(Y, Y_hat):
L_sum = np.sum(np.multiply(Y, np.log(Y_hat)))
m = Y.shape[1]
L = -(1/m) * L_sum
return L
n_x = X_train.shape[0]
n_h = 64
learning_rate = 1
W1 = np.random.randn(n_h, n_x)
b1 = np.zeros((n_h, 1))
W2 = np.random.randn(digits, n_h)
b2 = np.zeros((digits, 1))
X = X_train
Y = Y_train
for i in range(4000):
Z1 = np.matmul(W1,X) + b1
A1 = sigmoid(Z1)
Z2 = np.matmul(W2,A1) + b2
A2 = np.exp(Z2) / np.sum(np.exp(Z2), axis=0)
cost = compute_multiclass_loss(Y, A2)
dZ2 = A2-Y
dW2 = (1./m) * np.matmul(dZ2, A1.T)
db2 = (1./m) * np.sum(dZ2, axis=1, keepdims=True)
dA1 = np.matmul(W2.T, dZ2)
dZ1 = dA1 * sigmoid(Z1) * (1 - sigmoid(Z1))
dW1 = (1./m) * np.matmul(dZ1, X.T)
db1 = (1./m) * np.sum(dZ1, axis=1, keepdims=True)
W2 = W2 - learning_rate * dW2
b2 = b2 - learning_rate * db2
W1 = W1 - learning_rate * dW1
b1 = b1 - learning_rate * db1
if (i % 100 == 0):
print("Epoch", i, "cost: ", cost)
print("Final cost:", cost)
Epoch 0 cost: 449165.0122274696
Epoch 100 cost: 45986.79247327906
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-12-8fe04cbfa5b8> in <module>
13 for i in range(4000):
14
---> 15 Z1 = np.matmul(W1,X) + b1
16 A1 = sigmoid(Z1)
17 Z2 = np.matmul(W2,A1) + b2
KeyboardInterrupt:
from sklearn.metrics import classification_report, confusion_matrix
Z1 = np.matmul(W1, X_test) + b1
A1 = sigmoid(Z1)
Z2 = np.matmul(W2, A1) + b2
A2 = np.exp(Z2) / np.sum(np.exp(Z2), axis=0)
predictions = np.argmax(A2, axis=0)
labels = np.argmax(Y_test, axis=0)
print(confusion_matrix(predictions, labels))
print(classification_report(predictions, labels))
参考资料
- Using deep learning to investigate the neuroimaging correlates of psychiatric and neurological disorders: Methods and applications
- https://jonathanweisberg.org/post/A%20Neural%20Network%20from%20Scratch%20-%20Part%201/