从零开始掌握BP神经网络:基于TensorFlow的回归与分类实战
一、前言为什么要学BP神经网络BPBack Propagation神经网络是深度学习的基石之一。无论你是刚入门机器学习还是希望系统掌握神经网络的基本原理BP神经网络都是一个绕不开的起点。它通过前向传播计算输出再通过反向传播调整权重从而让网络不断“学习”到数据的规律。本文将带你使用TensorFlow框架完成两个经典任务波士顿房价预测回归任务鸢尾花分类分类任务通过这两个项目你将掌握以下技能数据预处理标准化、独热编码BP神经网络的结构设计输入层、隐藏层、输出层模型编译与训练损失函数、优化器、评估指标结果可视化损失曲线、准确率曲线超参数调优思路层数、节点数、激活函数等二、环境准备与数据加载2.1 安装与导入库确保已安装TensorFlow、Scikit-learn、Matplotlib等库pip install tensorflow scikit-learn matplotlib导入所需模块import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_boston, load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, OneHotEncoder import tensorflow as tf from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.losses import MeanSquaredError, CategoricalCrossentropy from tensorflow.keras.optimizers import Adam注意新版Scikit-learn中波士顿数据集已移除可用fetch_openml替代或使用模拟数据本文使用经典方式说明。三、任务一波士顿房价预测回归3.1 数据加载与预处理# 加载数据示例使用fetch_openml from sklearn.datasets import fetch_openml boston fetch_openml(nameboston, version1, as_frameTrue) X boston.data.values.astype(np.float32) y boston.target.values.astype(np.float32) # 标准化 scaler StandardScaler() X_scaled scaler.fit_transform(X) # 划分训练集与测试集 X_train, X_test, y_train, y_test train_test_split(X_scaled, y, test_size0.2, random_state42)输出示例训练集样本数404 测试集样本数102 特征数133.2 构建BP神经网络model Sequential([ Dense(64, activationrelu, input_shape(X_train.shape[1],)), Dense(32, activationrelu), Dense(1) # 线性激活默认 ]) model.summary()网络结构_________________________________________________________________ Layer (type) Output Shape Param # dense (Dense) (None, 64) 896 _________________________________________________________________ dense_1 (Dense) (None, 32) 2080 _________________________________________________________________ dense_2 (Dense) (None, 1) 33 Total params: 3,009 Trainable params: 3,0093.3 编译与训练model.compile(optimizerAdam(learning_rate0.001), lossMeanSquaredError()) history model.fit(X_train, y_train, validation_split0.2, epochs100, batch_size32, verbose0)3.4 评估与可视化# 测试集评估 test_loss model.evaluate(X_test, y_test, verbose0) print(f测试集MSE: {test_loss:.4f}) # 绘制损失曲线 plt.plot(history.history[loss], labeltrain_loss) plt.plot(history.history[val_loss], labelval_loss) plt.xlabel(Epochs) plt.ylabel(MSE) plt.legend() plt.title(波士顿房价预测 - 损失曲线) plt.show()结果示例训练集MSE: 10.1993 测试集MSE: 13.2085四、任务二鸢尾花分类分类4.1 数据加载与编码iris load_iris() X iris.data y iris.target.reshape(-1, 1) # 独热编码 encoder OneHotEncoder(sparse_outputFalse) y_onehot encoder.fit_transform(y) # 标准化 scaler StandardScaler() X_scaled scaler.fit_transform(X) # 划分数据集 X_train, X_test, y_train, y_test train_test_split(X_scaled, y_onehot, test_size0.2, random_state42)4.2 构建分类网络model_cls Sequential([ Dense(64, activationrelu, input_shape(X_train.shape[1],)), Dense(32, activationrelu), Dense(3, activationsoftmax) ]) model_cls.compile(optimizerAdam(learning_rate0.001), lossCategoricalCrossentropy(), metrics[accuracy])4.3 训练与评估history_cls model_cls.fit(X_train, y_train, validation_split0.2, epochs100, batch_size32, verbose0) # 测试集准确率 test_loss, test_acc model_cls.evaluate(X_test, y_test, verbose0) print(f测试集准确率: {test_acc:.4f})结果示例训练集准确率: 1.0000 测试集准确率: 0.9750可视化训练过程代码解析绘制准确率曲线plt.plot(history_cls.history[accuracy], labeltrain_acc) plt.plot(history_cls.history[val_accuracy], labelval_acc) plt.xlabel(Epochs) plt.ylabel(Accuracy) plt.legend() plt.title(鸢尾花分类 - 准确率曲线) plt.show()绘制损失曲线plt.plot(history_cls.history[loss], labeltrain_loss) plt.plot(history_cls.history[val_loss], labelval_loss) plt.xlabel(Epochs) plt.ylabel(Loss) plt.legend() plt.title(鸢尾花分类 - 损失曲线) plt.show()参数调优关键发现网络层数影响1层训练MSE 15.2测试MSE 16.8欠拟合2层训练MSE 10.2测试MSE 13.2最佳3层训练MSE 8.5测试MSE 18.9过拟合节点数量选择8节点欠拟合MSE偏高64→32结构表现最佳256→128结构训练慢且易过拟合激活函数对比sigmoid训练MSE 14.5测试MSE 15.9收敛慢tanh训练MSE 12.1测试MSE 14.0中等ReLU训练MSE 10.2测试MSE 13.2收敛快优化技术实现正则化与Dropout示例from tensorflow.keras.layers import Dropout from tensorflow.keras.regularizers import l2 model_reg Sequential([ Dense(64, activationrelu, kernel_regularizerl2(0.001), input_shape(13,)), Dropout(0.5), Dense(32, activationrelu, kernel_regularizerl2(0.001)), Dropout(0.5), Dense(1) ])早停法实现callback tf.keras.callbacks.EarlyStopping( monitorval_loss, patience10 ) history model.fit(..., callbacks[callback])动态学习率配置lr_schedule tf.keras.optimizers.schedules.ExponentialDecay( initial_learning_rate0.01, decay_steps1000, decay_rate0.9 ) optimizer Adam(learning_ratelr_schedule)性能指标总结房价预测测试MSE13.2鸢尾花分类准确率97.5%推荐隐藏层激活函数ReLU输出层选择回归用线性分类用softmax扩展方向建议图像处理卷积神经网络CNN时序数据循环神经网络RNN/LSTM模型优化超参数自动调优Keras Tuner进阶技术迁移学习与预训练模型