一个带旋钮的函数,以及转动旋钮的方法
神经网络只是一个带有许多可调数字(权重)的函数。一层把输入乘以权重矩阵再加上偏置;然后施加一个像 ReLU(max(x, 0))这样的非线性;结果送入下一层。这样堆叠起来,这类函数能逼近非常复杂的关系。训练就是选取权重,使预测符合已知目标:用损失(预测数值时是均方误差)衡量偏差,计算损失随每个权重如何变化,然后把每个权重朝使损失下降的方向挪动一点。这种挪动重复成千上万次,就是梯度下降。
# A one-hidden-layer network: relu(x @ w1 + b1) @ w2 + b2
import numpy as np
def relu(x):
return np.maximum(x, 0.0)
def mlp_forward(x, w1, b1, w2, b2):
return relu(x @ w1 + b1) @ w2 + b2
x = np.array([[1.0, 2.0], [0.5, -1.0]]) # two samples, two features
w1 = np.array([[1.0, -1.0], [0.5, 1.0]]) # two hidden units
b1 = np.array([0.0, 0.5])
w2 = np.array([1.0, 2.0])
print(relu(np.array([-1.5, 0.0, 2.0]))) # [0. 0. 2.]
print(mlp_forward(x, w1, b1, w2, 0.1)) # [5.1 0.1] a prediction for each sample; the second sample's hidden values are switched off by the relu在最简单的模型上做梯度下降
训练网络使用反向传播来获得梯度,但思路在线性模型 y ~ x @ w + b 上已经完整。均方误差对 w 的梯度是 2/n * x.T @ err,对 b 的梯度是 2 * mean(err),其中 err = 预测 - 目标。从零开始,减去学习率乘梯度,然后重复。学习率过大会冲过头并发散,过小则爬得很慢。先对特征做标准化(均值为零、方差为一),让一个学习率适合所有特征。PyTorch 等库会自动计算梯度,但它们做的正是这个循环。
# Gradient descent on a line: start at zero and walk downhill on the mean squared error
import numpy as np
def mse_loss(pred, target):
return float(np.mean((pred - target) ** 2))
def train_linear(x, y, lr, epochs):
n, d = x.shape
w, b = np.zeros(d), 0.0
for _ in range(epochs):
err = x @ w + b - y
w = w - lr * 2 / n * x.T @ err
b = b - lr * 2 * float(np.mean(err))
return w, float(b)
x = np.array([[0.0], [1.0], [2.0], [3.0]])
y = np.array([1.0, 3.0, 5.0, 7.0]) # exactly y = 2x + 1
for epochs in (0, 10, 2000):
w, b = train_linear(x, y, 0.05, epochs)
print(epochs, np.round(w, 3), round(b, 3), round(mse_loss(x @ w + b, y), 6))
# 0 [0.] 0.0 21.0 the zero start
# 10 [2.009] 0.958 0.00092 already close
# 2000 [2.] 1.0 0.0 the line y = 2x + 1 is found为什么预测收益对神经网络很难
神经网络几乎什么都能拟合,而这正是金融数据的危险所在:收益大部分是噪声,灵活的模型会记住训练期的噪声,到下一期就失效。过拟合表现为训练误差远低于在没见过的数据上的误差。防御手段就是前面章节的那些:与简单基线(线性模型,或预测为零)比较;只用更晚的数据做前向滚动验证,绝不打乱时间;用少量特征和小网络;加入正则化(权重衰减、dropout)和早停;对用不同种子启动的多个网络取平均。如果网络在样本外不能明显胜过线性基线,就用线性模型。
# Overfitting in miniature: a flexible fit to pure noise looks great in-sample and useless afterwards
import numpy as np
rng = np.random.default_rng(5)
x_train, x_test = rng.normal(size=(30, 12)), rng.normal(size=(1000, 12)) # 12 features that mean nothing
y_train, y_test = rng.normal(0, 0.01, size=30), rng.normal(0, 0.01, size=1000) # next-day returns: pure noise
w = np.linalg.lstsq(x_train, y_train, rcond=None)[0] # a model with 12 free weights fitted to 30 points
train_mse = np.mean((x_train @ w - y_train) ** 2)
test_mse = np.mean((x_test @ w - y_test) ** 2)
baseline_mse = np.mean(y_test ** 2) # predict zero
print(train_mse < baseline_mse, test_mse > baseline_mse) # True True better in-sample, worse than zero out of sample# The learning rate matters: a small one settles, one that is too large overshoots and the error blows up
import numpy as np
x = np.array([[1.0], [2.0], [3.0]])
y = np.array([2.0, 4.0, 6.0])
def final_errors(lr, epochs):
w, b, errors = np.zeros(1), 0.0, []
for _ in range(epochs):
err = x @ w + b - y
errors.append(float(np.mean(err ** 2)))
w = w - lr * 2 / len(y) * x.T @ err
b = b - lr * 2 * float(np.mean(err))
return errors
small, large = final_errors(0.05, 30), final_errors(0.5, 30)
print(small[-1] < small[0]) # True the error shrinks
print(large[-1] > 1000 * large[0]) # True the error explodes: the steps are too big先从无聊的基线开始在使用任何网络之前,先用相同的特征拟合一个线性模型,并把它的样本外误差作为要超越的数字。记录每次运行的种子、数据窗口和超参数,因为神经网络训练是随机的,每次结果都会不同。特征缩放只能用训练期的统计量,绝不能用全样本,否则就泄漏了未来。还要记住:如果交易成本比它更大,那么很小的预测优势毫无价值。
- 一层是
relu(x @ w + b);网络是层的堆叠,损失(预测任务用均方误差)衡量预测与目标的距离。 - 训练就是梯度下降:线性模型中
w -= lr * 2/n * x.T @ err、b -= lr * 2 * mean(err);要缩放特征并调学习率。 - 收益大部分是噪声:与线性基线比较,用前向滚动验证,使用带正则化的小网络,并在样本外收益消失时停止。
下一节:用于交易的强化学习,从奖励而不是带标签的答案中学习策略。
实验:评分内容
- 用
relu关掉负值25 分 - 用
mlp_forward做前向计算25 分 - 用
mse_loss衡量误差25 分 - 用
train_linear通过下降来学习25 分