1. 只能分两类
层数固定不变 | 层数可以变化 |
---|---|
''' 11行神经网络① 固定三层,两类 ''' # 只适合 0, 1 两类。若不是,要先转化 import numpy as np X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]) y = np.array([0,1,1,0]).reshape(-1,1) # 此处reshape是为了便于算法简洁实现 wi = 2*np.random.randn(3,5) - 1 wh = 2*np.random.randn(5,1) - 1 for j in range(10000): li = X lh = 1/(1+np.exp(-(np.dot(li,wi)))) lo = 1/(1+np.exp(-(np.dot(lh,wh)))) lo_delta = (y - lo)*(lo*(1-lo)) lh_delta = np.dot(lo_delta, wh.T) * (lh * (1-lh)) wh += np.dot(lh.T, lo_delta) wi += np.dot(li.T, lh_delta) print('训练结果:', lo) |
''' 11行神经网络① 层数可变,两类 ''' # 只适合 0, 1 两类。若不是,要先转化 import numpy as np X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]) y = np.array([0,1,1,0]).reshape(-1,1) # 此处reshape是为了便于算法简洁实现 neurals = [3,15,1]
|
2.可以分多类
层数固定不变 | 层数可以变化 |
---|---|
''' 11行神经网络① 固定三层,多类 ''' import numpy as np X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]) #y = np.array([0,1,1,0]) # 可以两类 y = np.array([0,1,2,3]) # 可以多类 wi = np.random.randn(3,5) wh = np.random.randn(5,4) # 改 bh = np.random.randn(1,5) bo = np.random.randn(1,4) # 改 epsilon = 0.01 # 学习速率 lamda = 0.01 # 正则化强度 for j in range(1000): li = X lh = np.tanh(np.dot(li, wi) + bh) # tanh 函数 lo = np.exp(np.dot(lh, wh) + bo) probs = lo / np.sum(lo, axis=1, keepdims=True) # 后向传播 lo_delta = np.copy(probs) lo_delta[range(X.shape[0]), y] -= 1 lh_delta = np.dot(lo_delta, wh.T) * (1 - np.power(lh, 2)) # 更新权值、偏置 wh -= epsilon * (np.dot(lh.T, lo_delta) + lamda * wh) wi -= epsilon * (np.dot(li.T, lh_delta) + lamda * wi) bo -= epsilon * np.sum(lo_delta, axis=0, keepdims=True) bh -= epsilon * np.sum(lh_delta, axis=0) print('训练结果:', np.argmax(probs, axis=1))
|
''' 11行神经网络① 层数可变,多类 ''' import numpy as np X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]) #y = np.array([0,1,1,0]) # 可以两类 y = np.array([0,1,2,3]) # 可以多类 neurals = [3, 10, 8, 4]
|