# 机器学习 101：一文带你读懂梯度下降

https://github.com/sthalles/blog-resources

## 基本概念

f(x) = x²在不同点的斜率。

## 房价预测

# read the Houses dataset CSV file

# only get the Size and the Price features

Xs = housing_data[[‘Size’]]

Ys = housing_data[[‘Price’]]

# get some statistics

max_size = np.max(Xs)

min_size = np.min(Xs)

max_price = np.max(Ys)

min_price = np.min(Ys)

# Normalize the input features

Xs = (Xs – min_size) / (max_size – min_size)

Ys = (Ys – min_price) / (max_price – min_price)

W0的偏导数

W1的偏导数

for epoch in range(total_epochs):

for X_batch, y_batch in next_batch(Xs_train, Ys_train, batch_size=batch_size):

# linearly combine input and weights

train_pred = W0 + np.dot(X_batch, W1)

# calculate the SSE between predicted and true values

train_err = mean_squared_error(y_batch, train_pred)

# calculate the gradients with respect to W0 and W1

DW0 = -(2/batch_size) * sum(y_batch.squeeze() – train_pred.squeeze())

DW1 = -(2/batch_size) * sum(X_batch.squeeze() * (y_batch.squeeze() – train_pred.squeeze()))

# update W0 and W1 in the opposite direction to the gradient

W0 = W0 – lr * DW0

W1 = W1 – lr * DW1