import numpy as np
def gradient_test():
h_in = 64
h1 = 1000
h2 = 100
h_out = 10
x = np.random.randn(h_in, h1)
w1 = np.random.randn(h1, h2)
w2 = np.random.randn(h2, h_out)
y = np.random.randn(h_in, h_out)
# y = x.dot(w1).dot(w2)
lr = 1e-6
batch_size = 100
for i in range(batch_size):
# propagation size (h_in, h2)
x1 = x.dot(w1)
# get relu x,
x_relu = np.maximum(x1, 0)
# get y_pred, size (h_in, h_out)
y_pred = x_relu.dot(w2)
# calculate loss
loss = np.sum((y_pred - y)**2)
print('batch no:', i, ', loss:', loss)
# get the gradient of y for loss, size (h_in, h_out)
grad_y = 2 * (y_pred - y)
# get gradient of x_relu, size (h_in, h2) = (h_in, h_out).(h_out, h2)
grad_x_relu = grad_y.dot(w2.T)
# get the gradient of w2, size (h2, h_out) = (h2, h_in).(h_in, h_out)
grad_w2 = x_relu.T.dot(grad_y)
# backpropagation for w2
w2 -= lr * grad_w2
# get the gradient of x according to gradient of x_relu, size (h_in, h1) = (h_in, h2).(h2, h1)
grad_x = grad_x_relu.dot(w1.T)
# get the gradient of w1 according to gradient of x_relu, size (h1, h2) = (h1, h_in).(h_in, h2)
grad_w1 = x.T.dot(grad_x_relu)
# backpropagation for x and w1
x -= lr * grad_x
w1 -= lr * grad_w1
if __name__ == '__main__':
gradient_test()
Numpy实现简单的线性回归
最新推荐文章于 2024-04-28 05:00:00 发布