查看: 89|回复: 3

Python AI模型训练实战:线性回归与TensorFlow神经网络代码解析

[复制链接]
发表于 2 小时前 | 显示全部楼层 |阅读模式
本文基于加州房价数据集,演示如何使用Python从零构建并训练AI模型。内容涵盖数据预处理、线性回归、多层神经网络、模型优化以及Flask API部署,所有代码均可直接运行。

一、环境准备与数据加载
首先安装必需库:
  1. pip install numpy pandas matplotlib seaborn scikit-learn tensorflow
复制代码
加载sklearn内置的加州房价数据集:
  1. from sklearn.datasets import fetch_california_housing
  2. import pandas as pd
  3. housing = fetch_california_housing()
  4. data = pd.DataFrame(housing.data, columns=housing.feature_names)
  5. data['PRICE'] = housing.target
  6. print(data.head())
复制代码
数据标准化:使用StandardScaler将特征减去均值除以标准差,使模型收敛更快:
  1. from sklearn.preprocessing import StandardScaler
  2. scaler = StandardScaler()
  3. features = data.drop('PRICE', axis=1)
  4. target = data['PRICE']
  5. features_scaled = scaler.fit_transform(features)
复制代码

二、训练线性回归模型
划分训练集和测试集(80/20):
  1. from sklearn.model_selection import train_test_split
  2. X_train, X_test, y_train, y_test = train_test_split(
  3.     features_scaled, target, test_size=0.2, random_state=42)
复制代码
训练并评估:
  1. from sklearn.linear_model import LinearRegression
  2. from sklearn.metrics import mean_squared_error, r2_score
  3. model = LinearRegression()
  4. model.fit(X_train, y_train)
  5. y_pred = model.predict(X_test)
  6. mse = mean_squared_error(y_test, y_pred)
  7. r2 = r2_score(y_test, y_pred)
  8. print(f'MSE: {mse}, R²: {r2}')
复制代码
MSE越小越好,R²越接近1说明模型拟合度越高。该线性回归模型作为基准。

三、构建深度学习神经网络
使用TensorFlow搭建全连接神经网络:
  1. import tensorflow as tf
  2. from tensorflow.keras.models import Sequential
  3. from tensorflow.keras.layers import Dense, Input
  4. nn_model = Sequential([
  5.     Input(shape=(X_train.shape[1],)),
  6.     Dense(64, activation='relu'),
  7.     Dense(32, activation='relu'),
  8.     Dense(1)
  9. ])
  10. nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
  11. nn_model.summary()
复制代码
训练100轮,批大小32,20%验证集:
  1. history = nn_model.fit(
  2.     X_train, y_train,
  3.     epochs=100,
  4.     batch_size=32,
  5.     validation_split=0.2,
  6.     verbose=1)
复制代码
评估测试集:
  1. test_loss, test_mae = nn_model.evaluate(X_test, y_test)
  2. print(f'Test MSE: {test_loss}, Test MAE: {test_mae}')
复制代码

四、模型优化策略
1. 防止过拟合——添加Dropout层:
  1. from tensorflow.keras.layers import Dropout
  2. nn_model = Sequential([
  3.     Input(shape=(X_train.shape[1],)),
  4.     Dense(64, activation='relu'),
  5.     Dropout(0.5),
  6.     Dense(32, activation='relu'),
  7.     Dense(1)
  8. ])
  9. nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
复制代码
2. 早停法——在验证损失不再下降时停止训练:
  1. from tensorflow.keras.callbacks import EarlyStopping
  2. early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
  3. history = nn_model.fit(
  4.     X_train, y_train,
  5.     epochs=200,
  6.     batch_size=64,
  7.     validation_split=0.2,
  8.     callbacks=[early_stop])
复制代码
3. 调整学习率:
  1. from tensorflow.keras.optimizers import Adam
  2. nn_model.compile(optimizer=Adam(learning_rate=0.0001), loss='mse', metrics=['mae'])
复制代码

五、模型保存与Flask部署
保存模型为文件夹:
  1. nn_model.save('my_ai_model')
复制代码
创建Flask API服务(app.py):
  1. from flask import Flask, request, jsonify
  2. import numpy as np
  3. from tensorflow.keras.models import load_model
  4. model = load_model('my_ai_model')
  5. app = Flask(__name__)
  6. @app.route('/predict', methods=['POST'])
  7. def predict():
  8.     data = request.json
  9.     features = np.array(data['features']).reshape(1, -1)
  10.     prediction = model.predict(features)
  11.     return jsonify({'prediction': float(prediction[0][0])})
  12. if __name__ == '__main__':
  13.     app.run(debug=True)
复制代码
启动后使用curl测试:
  1. curl -X POST -H "Content-Type: application/json" -d '{"features":[0.1,-0.2,0.5,0.3,0.7,-1.1,0.4,0.9]}' http://127.0.0.1:5000/predict
复制代码
返回JSON格式预测值。

通过上述步骤,你已掌握从数据准备、模型训练、优化到部署的完整AI开发流程。实际项目中可根据需求调整网络深度、正则化强度或使用GPU加速(检查GPU可用性:tf.config.list_physical_devices('GPU'))。
回复

使用道具 举报

发表于 2 小时前 | 显示全部楼层

Re: Python AI模型训练实战:线性回归与TensorFlow神经网络代码解析

楼主写得非常详细,从数据加载、预处理到模型训练、优化再到部署,整个流程很完整,代码也能直接跑,对我这种刚接触深度学习的新手帮助很大。 想问一下:在Flask部署的部分,输入的数据需要先做标准化才能传给模型,但代码里直接用了`request.json`。是不是需要在`predict`函数里再调用一次训练时保存的Scaler(比如用joblib存下来)对输入做同样的标准化处理?还是说模型已经能直接处理未标准化的数据了? 另外,线性回归和神经网络在测试集上的MSE/MAE对比结果楼主能分享一下吗?想看看引入深度网络后提升幅度有多大。
回复 支持 反对

使用道具 举报

发表于 2 小时前 | 显示全部楼层

Re: Python AI模型训练实战:线性回归与TensorFlow神经网络代码解析

感谢楼主的详细教程!从数据预处理到模型部署一气呵成,代码可以直接跑起来,非常实用。 想请教一个小问题:我在跑神经网络那部分时,发现训练集和测试集都是用标准化后的特征,但目标变量 `PRICE` 没有做标准化。这种做法在回归任务中很常见吗?会不会影响 Adam 优化器的收敛?另外,部署时加载模型后,输入数据也需要用之前拟合的 `scaler` 转换吧,这部分楼主是不是省略了示例? 再次感谢分享,期待更多实战帖子!
回复 支持 反对

使用道具 举报

发表于 2 小时前 | 显示全部楼层

Re: Python AI模型训练实战:线性回归与TensorFlow神经网络代码解析

很实用的教程,代码可以直接跑通这点好评。我有个小问题:把Dropout设成0.5会不会太高?以前试过类似大小的网络,0.5感觉丢得有点多,容易欠拟合。楼主实际对比过不同dropout率的效果吗?另外,保存模型的时候需要把StandardScaler也一起存下来吧?不然部署时新数据没法标准化。有没有考虑用Pipeline把scaler和模型打包?期待后续能补上这部分。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

指导单位

江苏省公安厅

江苏省通信管理局

浙江省台州刑侦支队

DEFCON GROUP 86025

Hacking Group 021A

旗下站点

态势感知中心

应急响应中心

红盟安全

联系我们

官方QQ群:112851260

官方邮箱:security#ihonker.org(#改成@)

官方核心成员

关注微信公众号

Archiver|手机版|小黑屋| ( 沪ICP备2021026908号 )

GMT+8, 2026-6-27 20:20 , Processed in 0.035179 second(s), 18 queries , Gzip On, Redis On.

Powered by ihonker.com

Copyright © 2015-现在.

  • 返回顶部