共计 2582 个字符,预计需要花费 7 分钟才能阅读完成。
全文链接:https://tecdat.cn/?p=33792
原文出处:拓端数据部落公众号
在这篇文章中,我将尝试介绍从简略的线性回归到应用神经网络构建非线性概率模型的步骤。
这在模型噪声随着模型变量之一变动或为非线性的状况下特地有用,比方在存在异方差性的状况下。
当客户的数据是非线性时,这样会对线性回归解决方案提出一些问题:
# 增加的噪声量是 x 的函数 | |
n = 20000 | |
...... | |
x_train = x[: n // 2] | |
x_test = x[n // 2 :] | |
y_train = y[: n // 2] | |
...... | |
plt.show() |
线性回归办法
咱们用均方差作为优化指标,这是线性回归的规范损失函数。
model_lin_reg = tf.keras.Sequential( | |
...... | |
history = model_lin_reg.fit(x_train, y_train, epochs=10, verbose=0) | |
# 模型曾经收敛:plt.plot(history.history["loss"]) | |
...... |
Final loss: 5.25
咱们定义一些辅助函数来绘制后果:
def plot_results(x, y, y_est_mu, y_est_std=None): | |
...... | |
plt.show() | |
def plot_model_results(model, x, y, tfp_model: bool = True): | |
model.weights | |
...... | |
plot_results(x, y, y_est_mu, y_est_std) |
模型残差的标准差不影响收敛的回归系数,因而没有绘制。
plot_modesults(mod_linreg......, tfp_model=False)
TensorFlow 概率
咱们能够通过最大化正态分布的似然性来拟合上述雷同的模型,其中平均值是线性回归模型的估计值。
def negloglik(y, distr): | |
...... | |
model_lin_reg_tfp = tf.keras.Sequential( | |
...... | |
lambda t: tfp.distributions.Normal(loc=t, scale=5,) | |
), | |
] | |
) | |
model_lin_reg_tfp.compile(......) | |
history = model_lin_reg_tf...... | |
plot_model_results(model_lin_r......rue) |
拟合带有标准差的线性回归
为了拟合线性回归模型的最佳标准差,咱们须要进行一些操作。咱们须要网络输入两个节点,一个用于示意平均值,另一个用于示意标准差。
model_lin_reg_std_tfp = tf.keras.Sequential( | |
[......), | |
] | |
) | |
model_lin_reg_std_tfp.compile(......) | |
history = model_lin_reg_std_tfp.fit(x_train, y_train, epochs=50, ......train, tfp_model=True) |
下面的图表显示,标准差和均值都与之前不同。它们都随着 x 变量的减少而减少。然而,它们对数据依然不是很好的拟合,无奈捕捉到非线性关系。
神经网络办法
为了帮忙拟合 x 和 y 之间非线性关系,咱们能够利用神经网络。这能够简略地应用咱们设计的雷同 TensorFlow 模型,但增加一个具备非线性激活函数的暗藏层。
model_lin_reg_std_nn_tfp = tf.keras.Sequential( | |
[......) | |
), | |
] | |
) | |
model_lin_reg_std_nn_tfp.compile( | |
...... | |
plot_model_results(mode ......rain, tfp_model=True) |
神经网络模型拟合的均值比线性回归模型更好地合乎数据的非线性关系。
后果
咱们对训练集和测试集运行了各个模型。在任何模型中,两者之间的性能变动不大。咱们能够看到,神经网络模型在训练集和测试集上的体现最好。
results = pd.DataFrame(index=["Train", "Test"]) | |
models = {......).numpy(),] | |
results.transpose() |
激活函数
上面应用 relu 或 softplus 激活函数创立雷同的网络。首先是 relu 网络的后果:
model_relu = tf.keras.Sequential( | |
[ | |
...... | |
loc=t[:, 0:1], scale=tf.math.softplus(t[:, 1:2]) | |
) | |
), | |
] | |
) | |
m ...... | |
plot_model_results(model_relu, x_train, y_train) |
而后是 softplus 的后果:
model_softplus = tf.keras.Sequential( | |
[ | |
...... | |
loc=t[:, 0:1], scale=tf.math.softplus(t[:, 1:2]) | |
) | |
), | |
] | |
) | |
model_softplus.compile( | |
...... | |
plot_model_results(model_softplus, x_train, y_train) |
咱们能够看到,基于 sigmoid 的神经网络具备最佳性能。
results = pd.DataFrame(index=["Train", "Test"]) | |
models = {......(x_test)) | |
).numpy(),] | |
results.transpose() |
最受欢迎的见解
1.R 语言实现 CNN(卷积神经网络)模型进行回归
2.r 语言实现拟合神经网络预测和后果可视化
3.python 用遗传算法 - 神经网络 - 含糊逻辑控制算法对乐透剖析
4.R 语言联合新冠疫情 COVID-19 股票价格预测:ARIMA,KNN 和神经网络工夫序列剖析
5.Python TensorFlow 循环神经网络 RNN-LSTM 神经网络预测股票市场价格工夫序列和 MSE 评估准确性
6.Matlab 用深度学习长短期记忆(LSTM)神经网络对文本数据进行分类
7. 用于 NLP 的 seq2seq 模型实例用 Keras 实现神经机器翻译
8. R 语言用 FNN-LSTM 假近邻长短期记忆人工神经网络模型进行工夫序列深度学习预测
9.Python 用 RNN 循环神经网络:LSTM 长期记忆、GRU 门循环单元、回归和 ARIMA 对 COVID-19 新冠疫情新增人数工夫序列预测