判断空间中点的卦限
import numpy as np
前向传播函数
def trans_forward(x, w, b):
out = None
N = x.shape[0]
x_row = x.reshape(N, -1)
out = np.dot(x_row, w) + b
cache = (x, w, b)
return out, cache
反向传播函数
def trans_backward(dout, cache):
x, w, b = cache
dx, dw, db = None, None, None
dx = np.dot(dout, w.T)
dx = np.reshape(dx, x.shape)
x_row = x.reshape(x.shape[0], -1)
dw = np.dot(x_row.T, dout)
db = np.sum(dout, axis = 0, keepdims = True)
return dx, dw, db
参数初始化
x = np.array([[1, 1, 1],
[-1, 1, 1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, -1],
[1, -1, -1]])
t = np.array([1, 2, 3, 4, 5, 6, 7, 8])
np.random.seed(1)
input_dim = x.shape[1]
num_classes = t.shape[0]
hidden_dim = 100
reg = .001
epsilon = .001
w1 = np.random.randn(input_dim, hidden_dim)
w2 = np.random.randn(hidden_dim, num_classes)
b1 = np.zeros((1, hidden_dim))
b2 = np.zeros((1, num_classes))
迭代训练
for i in range (10000):
H, fc_cache = trans_forward(x, w1, b1)
H = np.maximum(0, H)
relu_cache = H
y, cache = trans_forward(H, w2, b2)
probs = np.exp(y - np.max(y, axis=1, keepdims=True))
probs /= np.sum(probs, axis=1, keepdims=True)
N = y.shape[0]
loss = -np.sum(np.log(probs[np.arange(N), t-1])) / N
print(probs[np.arange(N), t - 1], loss)
dx = probs.copy()
dx[np.arange(N), t-1] -= 1
dx /= N
dh1, dw2, db2 = trans_backward(dx, cache)
dh1[relu_cache <= 0] = 0
dX, dw1, db1 = trans_backward(dh1, fc_cache)
dw2 += reg * w2
dw1 += reg * w1
w2 += -epsilon * dw2
b2 += -epsilon * db2
w1 += -epsilon * dw1
b1 += -epsilon * db1
test = np.array([[2, 2, 2],
[-2, 2, 2],
[-2, -2, 2],
[2, -2, 2],
[2, 2, -2],
[-2, 2, -2],
[-2, -2, -2],
[2, -2, -2]])
H,fc_cache = trans_forward(test,w1,b1)
H = np.maximum(0, H)
relu_cache = H
Y,cachey = trans_forward(H,w2,b2)
probs = np.exp(Y - np.max(Y, axis=1, keepdims=True))
probs /= np.sum(probs, axis=1, keepdims=True)
print(probs)
for k in range(8):
print(test[k,:],"所在的象限为",np.argmax(probs[k,:])+1)