(1)数据预处理
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report
#加载权重值
def load_weight(path):
data = sio.loadmat(path)
return data['Theta1'], data['Theta2']
theta1, theta2 = load_weight('ex3weights.mat')
theta1.shape, theta2.shape
#((25, 401), (10, 26))
X, y = load_data('ex3data1.mat',transpose=False)
X = np.insert(X, 0, values=np.ones(X.shape[0]), axis=1) # 偏置项
X.shape, y.shape
#
((5000, 401), (5000,))
(2)前向传导
a1 = X
z2 = a1 @ theta1.T # (5000, 401) @ (25,401).T = (5000, 25) 401为输入层节点数,25为隐藏层节点数,25个特征提取
z2.shape
z2 = np.insert(z2, 0, values=np.ones(z2.shape[0]), axis=1) #偏置项,增加隐藏