脚本专家 发表于 2026-6-27 18:00:07

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

本文基于加州房价数据集,演示如何使用Python从零构建并训练AI模型。内容涵盖数据预处理、线性回归、多层神经网络、模型优化以及Flask API部署,所有代码均可直接运行。

一、环境准备与数据加载
首先安装必需库:

pip install numpy pandas matplotlib seaborn scikit-learn tensorflow

加载sklearn内置的加州房价数据集:

from sklearn.datasets import fetch_california_housing
import pandas as pd

housing = fetch_california_housing()
data = pd.DataFrame(housing.data, columns=housing.feature_names)
data['PRICE'] = housing.target
print(data.head())

数据标准化:使用StandardScaler将特征减去均值除以标准差,使模型收敛更快:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
features = data.drop('PRICE', axis=1)
target = data['PRICE']
features_scaled = scaler.fit_transform(features)


二、训练线性回归模型
划分训练集和测试集(80/20):

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    features_scaled, target, test_size=0.2, random_state=42)

训练并评估:

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'MSE: {mse}, R²: {r2}')

MSE越小越好,R²越接近1说明模型拟合度越高。该线性回归模型作为基准。

三、构建深度学习神经网络
使用TensorFlow搭建全连接神经网络:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input

nn_model = Sequential([
    Input(shape=(X_train.shape,)),
    Dense(64, activation='relu'),
    Dense(32, activation='relu'),
    Dense(1)
])
nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])
nn_model.summary()

训练100轮,批大小32,20%验证集:

history = nn_model.fit(
    X_train, y_train,
    epochs=100,
    batch_size=32,
    validation_split=0.2,
    verbose=1)

评估测试集:

test_loss, test_mae = nn_model.evaluate(X_test, y_test)
print(f'Test MSE: {test_loss}, Test MAE: {test_mae}')


四、模型优化策略
1. 防止过拟合——添加Dropout层:

from tensorflow.keras.layers import Dropout

nn_model = Sequential([
    Input(shape=(X_train.shape,)),
    Dense(64, activation='relu'),
    Dropout(0.5),
    Dense(32, activation='relu'),
    Dense(1)
])
nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])

2. 早停法——在验证损失不再下降时停止训练:

from tensorflow.keras.callbacks import EarlyStopping

early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
history = nn_model.fit(
    X_train, y_train,
    epochs=200,
    batch_size=64,
    validation_split=0.2,
    callbacks=)

3. 调整学习率:

from tensorflow.keras.optimizers import Adam

nn_model.compile(optimizer=Adam(learning_rate=0.0001), loss='mse', metrics=['mae'])


五、模型保存与Flask部署
保存模型为文件夹:

nn_model.save('my_ai_model')

创建Flask API服务(app.py):

from flask import Flask, request, jsonify
import numpy as np
from tensorflow.keras.models import load_model

model = load_model('my_ai_model')
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.json
    features = np.array(data['features']).reshape(1, -1)
    prediction = model.predict(features)
    return jsonify({'prediction': float(prediction)})

if __name__ == '__main__':
    app.run(debug=True)

启动后使用curl测试:

curl -X POST -H "Content-Type: application/json" -d '{"features":}' http://127.0.0.1:5000/predict

返回JSON格式预测值。

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

热心网友6 发表于 2026-6-27 18:10:00

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

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

热心网友6 发表于 2026-6-27 18:10:00

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

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

热心网友6 发表于 2026-6-27 18:10:00

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

很实用的教程,代码可以直接跑通这点好评。我有个小问题:把Dropout设成0.5会不会太高?以前试过类似大小的网络,0.5感觉丢得有点多,容易欠拟合。楼主实际对比过不同dropout率的效果吗?另外,保存模型的时候需要把StandardScaler也一起存下来吧?不然部署时新数据没法标准化。有没有考虑用Pipeline把scaler和模型打包?期待后续能补上这部分。
页: [1]
查看完整版本: Python AI模型训练实战:线性回归与TensorFlow神经网络代码解析