全国大学生数据建模比赛——深度学习

全国大学生数学建模比赛中,深度学习可以成为解决复杂问题的有力手段。

一、深度学习的优势在比赛中的体现

  1. 强大的模式识别能力:深度学习模型,如卷积神经网络(CNN)和循环神经网络(RNN),在处理图像、文本、时间序列等数据方面表现出卓越的性能。在数学建模比赛中,可能会遇到需要对图像进行分类、对文本进行情感分析或对时间序列数据进行预测等问题,深度学习可以有效地提取数据中的特征,提高模型的准确性。
  2. 自动特征提取:与传统的机器学习方法相比,深度学习模型能够自动从原始数据中学习特征,无需人工设计特征。这在处理大规模、高维度数据时尤为重要,可以节省大量的时间和精力。
  3. 良好的泛化能力:经过充分训练的深度学习模型通常具有较好的泛化能力,能够在新的数据上表现出良好的性能。这对于数学建模比赛中未知的测试数据非常关键,能够提高模型的可靠性和实用性。

二、在比赛中应用深度学习的步骤

  1. 问题理解与数据收集:

    • 首先,深入理解比赛问题的背景和要求,确定需要解决的具体问题。
    • 然后,收集与问题相关的各种数据,包括训练数据和测试数据。数据的质量和数量对深度学习模型的性能至关重要。
  2. 数据预处理:

    • 对收集到的数据进行预处理,包括数据清洗、归一化、标准化等操作。对于图像数据,可能还需要进行裁剪、缩放、旋转等增强操作,以增加数据的多样性。
    • 将数据划分为训练集、验证集和测试集,用于模型的训练、调参和评估。
  3. 模型选择与搭建:

    • 根据问题的特点和数据的类型,选择合适的深度学习模型。例如,对于图像分类问题,可以选择 CNN;对于文本处理问题,可以选择 RNN 或 Transformer 架构。
    • 使用深度学习框架,如 TensorFlow、PyTorch 等,搭建所选的模型。可以从现有的开源模型开始,根据需要进行修改和调整。
  4. 模型训练与调参:

    • 使用训练集对模型进行训练,调整模型的参数,以最小化损失函数。可以采用随机梯度下降等优化算法,设置适当的学习率、批次大小等参数。
    • 在训练过程中,使用验证集对模型进行评估,及时调整模型的结构和参数,防止过拟合。可以采用正则化、Dropout 等技术来提高模型的泛化能力。
  5. 模型评估与改进:

    • 使用测试集对训练好的模型进行最终评估,计算模型的准确率、召回率、F1 值等指标,评估模型的性能。
    • 根据评估结果,分析模型存在的问题,如过拟合、欠拟合等,采取相应的改进措施,如增加数据量、调整模型结构、改进训练方法等。

三、注意事项与挑战

  1. 计算资源需求:深度学习模型通常需要大量的计算资源,包括 GPU 等硬件设备。在比赛中,可能需要合理安排计算资源,提高计算效率。
  2. 数据量要求:深度学习模型需要大量的训练数据才能发挥出良好的性能。在数据量有限的情况下,可以考虑采用数据增强、迁移学习等技术来提高模型的性能。
  3. 模型解释性:深度学习模型通常具有较高的复杂性,难以解释其决策过程。在比赛中,可能需要对模型的结果进行解释和说明,以增强模型的可信度。
  4. 时间限制:数学建模比赛通常有时间限制,需要在有限的时间内完成模型的训练和评估。因此,需要合理安排时间,选择合适的模型和算法,提高建模效率。

总之,在全国大学生数学建模比赛中,深度学习可以为解决复杂问题提供强大的工具。但在应用深度学习时,需要充分考虑问题的特点、数据的类型和计算资源等因素,选择合适的模型和算法,并进行充分的实验和调参,以提高模型的性能和可靠性。

例题案例:

1. TensorFlow框架的基本使用(5-1)

  1. 获取训练数据

构建一个简单的线性模型:W,b为参数,W=2,b=1,运用tf.random.normal() 产生1000个随机数,产生x,y数据。

用matplotlib库,用蓝色绘制训练数据。

  1. 定义模型

通过对样本数据的离散图可以判断,呈线性规律变化,因此可以建立一个线性模型,即 ,把该线性模型定义为一个简单的类,里面封装了变量和计算,变量设置用tf.Variable()。

  1. 定义损失函数

损失函数是衡量给定输入的模型输出与期望输出的匹配程度,采用均方误差(L2范数损失函数)。

  1. 模型训练

运用数据和模型来训练得到模型的变量(W和b),观察W和b的变化(使用matplotlib绘制W和b的变化情况曲线)。

python 复制代码
import tensorflow as tf
import matplotlib.pyplot as plt

# 步骤1:生成训练数据
num_samples = 1000
true_W = 2
true_b = 1
inputs = tf.random.normal(shape=(num_samples,))
noise = tf.random.normal(shape=(num_samples,))
outputs = inputs * true_W + true_b + noise

# 绘制训练数据
plt.scatter(inputs, outputs, c='b', label='Training data')
plt.xlabel('Input')
plt.ylabel('Output')
plt.legend()
plt.show()

# 步骤2:定义模型
class LinearModel(tf.Module):
    def __init__(self):
        self.W = tf.Variable(tf.random.normal(shape=(), stddev=0.1))
        self.b = tf.Variable(tf.random.normal(shape=(), stddev=0.1))

    def __call__(self, x):
        return self.W * x + self.b

# 步骤3:定义损失函数
def loss(y_true, y_pred):
    return tf.reduce_mean(tf.square(y_true - y_pred))

# 步骤4:模型训练
model = LinearModel()
learning_rate = 0.1
epochs = 50
history_W, history_b = [], []

for epoch in range(epochs):
    with tf.GradientTape() as tape:
        current_loss = loss(outputs, model(inputs))
    dW, db = tape.gradient(current_loss, [model.W, model.b])
    model.W.assign_sub(learning_rate * dW)
    model.b.assign_sub(learning_rate * db)
    history_W.append(model.W.numpy())
    history_b.append(model.b.numpy())

# 可视化W和b的变化
plt.plot(history_W, label='W')
plt.plot(history_b, label='b')
plt.xlabel('Epochs')
plt.ylabel('Values')
plt.legend()
plt.show()

2. 多层神经网络分类(5-2)

  1. 数据获取与预处理

MNIST 数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST). 训练集 (training set) 由来自 250 个不同人手写的数字构成, 其中 50% 是高中学生, 50% 来自人口普查局 (the Census Bureau) 的工作人员. 测试集(test set) 也是同样比例的手写数字数据。

每张图像的大小都是28x28像素。MNIST数据集有60000张图像用于训练和10000张图像用于测试,其中每张图像都被标记了对应的数字(0-9)。

  1. 加载数据集
  2. 查看数据集
  1. 归一化处理
  1. 模型构建
  1. 模型定义
  1. 编译模型
  1. 输出模型参数
  1. 模型训练
  1. 训练
  1. 获取训练历史数据中的各指标值
  1. 绘制指标在训练过程中的变化图
  1. 模型评估

使用测试集对模型进行评估

代码:

python 复制代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 加载MNIST数据集
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 输出第一张图片和对应的标签
# 查看训练集中的一张图像和对应的标签
plt.imshow(x_train[0], cmap='gray')
plt.title(f"Label: {x_train[0]}")
plt.axis('off')
plt.show()
# 查看测试集中的一张图像和对应的标签
plt.imshow(x_test[0], cmap='gray')
plt.title(f"Label: {x_test[0]}")
plt.axis('off')
plt.show()
# 对输入数据进行归一化处理
x_train = x_train / 255.0
x_test = x_test / 255.0
# 定义显示图片的函数
def plot_images(images):
    plt.imshow(images, cmap='binary')
    plt.show()
# 构建神经网络模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),  # 将输入展平为一维数组
    tf.keras.layers.Dense(256, activation='relu'),  # 全连接层,使用ReLU激活函数
    tf.keras.layers.Dropout(0.2),  # Dropout层,可以防止过拟合
    tf.keras.layers.Dense(128, activation='relu'), # 全连接层,使用ReLU激活函数
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')  # 输出层,使用softmax激活函数输出分类概率
])
# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',  # 使用交叉熵损失函数
              metrics=['sparse_categorical_accuracy'])

# 输出模型结构
model.summary()
# 训练模型
history = model.fit(x_train, y_train, epochs=50, validation_split=0.2, verbose=1)
train_loss = history.history['loss']
val_loss = history.history['val_loss']
train_accuracy = history.history['sparse_categorical_accuracy']
val_accuracy = history.history['val_sparse_categorical_accuracy']
# 生成图形
plt.figure(figsize=(12, 4))
# Loss 图
plt.subplot(1, 2, 1)
plt.plot(train_loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
# Accuracy 图
plt.subplot(1, 2, 2)
plt.plot(train_accuracy, label='Training Accuracy')
plt.plot(val_accuracy, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()


test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f"Test Loss: {test_loss}")
print(f"Test Accuracy: {test_accuracy}")

3. 多层神经网络回归(5-3)

  1. 数据获取与预处理

Auto MPG 数据集,它记录了各种汽车效能指标MPG(Mile Per Gallon)与气缸数、重量、马力等因素的真实数据。除了产地的数字字段表示类别外,其他字段都是数值类型。对于产地地段,1 表示美国,2 表示欧洲,3 表示日本。

  1. 加载数据集

column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',

'Acceleration', 'Model Year', 'Origin'] #选定需要的数据特征

raw_dataset = pd.read_csv('./data/auto-mpg.data', names=column_names,

na_values = "?", comment='\t',

sep=" ", skipinitialspace=True) #读取刚下载的数据

  1. 数据清洗

统计数据集中各列中空值的个数,并删除包含空值的行。

  1. 将Origin列转换为one-hot(独热)编码。
  2. 数据探索
  • 使用describe方法查看数据的统计指标
  • 使用seaborn库中pairplot方法绘制"MPG", "Cylinders", "Displacement", "Weight"四列的联合分布图
  1. 数据标准化

labels = dataset.pop('MPG') #从数据集中取出目标值MPG

#数据标准化

from sklearn.preprocessing import StandardScaler

def norm(x):

return (x - train_stats['mean']) / train_stats['std'] #标准化公式

scaler = StandardScaler()

normed_dataset = scaler.fit_transform(dataset)

  1. 划分训练集与测试集

#拆分训练数据集和测试数据集,将数据集拆分为一个训练数据集和一个测试数据集。

X_train, X_test, Y_train, Y_test = train_test_split(normed_dataset,labels,test_size=0.2,random_state=0)

  1. 模型构建
  1. 模型定义

model = tf.keras.Sequential([

tf.keras.layers.Dense(64, activation='relu', input_shape=[X_train.shape[1]]),

tf.keras.layers.Dense(64, activation='relu'),

tf.keras.layers.Dense(1)

])

  1. 编译模型

loss='mse' #损失用mse

optimizer='adam'

metrics=['mae', 'mse'])

  1. 输出模型参数

print(model.summary())

  1. 模型训练
  1. 训练

epochs=100,

validation_split = 0.2

verbose=1

  1. 获取训练历史数据中的各指标值

mae = history.history['mae']

val_mae = history.history['val_mae']

mse = history.history['mse']

val_mse = history.history['val_mse']

  1. 绘制指标在训练过程中的变化图

plt.figure(1)

plt.plot(mae, label='Training MAE')

plt.plot(val_mae, label='Validation MAE')

plt.title('Training and Validation MAE')

plt.legend()

plt.figure(2)

plt.plot(mse, label='Training MSE')

plt.plot(val_mse, label='Validation MSE')

plt.title('Training and Validation MSE')

plt.legend()

plt.show()

  1. 模型评估

使用测试集对模型进行评估

测试模型

model.evaluate(X_test, Y_test, verbose=1)

代码:

python 复制代码
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 加载数据集
column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
                'Acceleration', 'Model Year', 'Origin']
raw_dataset = pd.read_csv('auto-mpg.data', names=column_names,
                      na_values = "?", comment='\t',
                      sep=" ", skipinitialspace=True)
print(raw_dataset)
# 数据清洗
dataset = raw_dataset.dropna()
# 将Origin列转换为one-hot编码
dataset['Origin'] = dataset['Origin'].map({1: 'USA', 2: 'Europe', 3: 'Japan'})
dataset = pd.get_dummies(dataset, columns=['Origin'], prefix='', prefix_sep='')
# 数据探索
print(dataset.describe())
sns.pairplot(dataset[['MPG', 'Cylinders', 'Displacement', 'Weight']], diag_kind='kde')
# 数据标准化
labels = dataset.pop('MPG')
train_stats = dataset.describe().transpose()
def norm(x):
    return (x - train_stats['mean']) / train_stats['std']
normed_dataset = norm(dataset)
# 划分训练集与测试集
X_train, X_test, Y_train, Y_test = train_test_split(normed_dataset, labels, test_size=0.2, random_state=0)
# 模型构建
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=[X_train.shape[1]]),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])
# 编译模型
model.compile(loss='mse', optimizer='adam', metrics=['mae', 'mse'])

# 输出模型参数
print(model.summary())
# 模型训练
history = model.fit(X_train, Y_train, epochs=1000, validation_split=0.3, verbose=1)
# 获取训练历史数据中的各指标值
mae = history.history['mae']
val_mae = history.history['val_mae']
mse = history.history['mse']
val_mse = history.history['val_mse']
plt.figure()
plt.plot(mae, label='Training MAE')
plt.plot(val_mae, label='Validation MAE')
plt.title('Training and Validation MAE')
plt.legend()
plt.show()
plt.figure()
plt.plot(mse, label='Training MSE')
plt.plot(val_mse, label='Validation MSE')
plt.title('Training and Validation MSE')
plt.legend()
plt.show()
# 模型评估
h1=model.evaluate(X_test, Y_test, verbose=1)
print(h1)

数据集例样:

18.0 8 307.0 130.0 3504. 12.0 70 1 "chevrolet chevelle malibu"

15.0 8 350.0 165.0 3693. 11.5 70 1 "buick skylark 320"

18.0 8 318.0 150.0 3436. 11.0 70 1 "plymouth satellite"

16.0 8 304.0 150.0 3433. 12.0 70 1 "amc rebel sst"

17.0 8 302.0 140.0 3449. 10.5 70 1 "ford torino"

15.0 8 429.0 198.0 4341. 10.0 70 1 "ford galaxie 500"

14.0 8 454.0 220.0 4354. 9.0 70 1 "chevrolet impala"

14.0 8 440.0 215.0 4312. 8.5 70 1 "plymouth fury iii"

14.0 8 455.0 225.0 4425. 10.0 70 1 "pontiac catalina"

15.0 8 390.0 190.0 3850. 8.5 70 1 "amc ambassador dpl"

15.0 8 383.0 170.0 3563. 10.0 70 1 "dodge challenger se"

14.0 8 340.0 160.0 3609. 8.0 70 1 "plymouth 'cuda 340"

15.0 8 400.0 150.0 3761. 9.5 70 1 "chevrolet monte carlo"

14.0 8 455.0 225.0 3086. 10.0 70 1 "buick estate wagon (sw)"

24.0 4 113.0 95.00 2372. 15.0 70 3 "toyota corona mark ii"

22.0 6 198.0 95.00 2833. 15.5 70 1 "plymouth duster"

18.0 6 199.0 97.00 2774. 15.5 70 1 "amc hornet"

21.0 6 200.0 85.00 2587. 16.0 70 1 "ford maverick"

27.0 4 97.00 88.00 2130. 14.5 70 3 "datsun pl510"

26.0 4 97.00 46.00 1835. 20.5 70 2 "volkswagen 1131 deluxe sedan"

25.0 4 110.0 87.00 2672. 17.5 70 2 "peugeot 504"

24.0 4 107.0 90.00 2430. 14.5 70 2 "audi 100 ls"

25.0 4 104.0 95.00 2375. 17.5 70 2 "saab 99e"

26.0 4 121.0 113.0 2234. 12.5 70 2 "bmw 2002"

21.0 6 199.0 90.00 2648. 15.0 70 1 "amc gremlin"

10.0 8 360.0 215.0 4615. 14.0 70 1 "ford f250"

10.0 8 307.0 200.0 4376. 15.0 70 1 "chevy c20"

11.0 8 318.0 210.0 4382. 13.5 70 1 "dodge d200"

9.0 8 304.0 193.0 4732. 18.5 70 1 "hi 1200d"

27.0 4 97.00 88.00 2130. 14.5 71 3 "datsun pl510"

28.0 4 140.0 90.00 2264. 15.5 71 1 "chevrolet vega 2300"

25.0 4 113.0 95.00 2228. 14.0 71 3 "toyota corona"

25.0 4 98.00 ? 2046. 19.0 71 1 "ford pinto"

19.0 6 232.0 100.0 2634. 13.0 71 1 "amc gremlin"

16.0 6 225.0 105.0 3439. 15.5 71 1 "plymouth satellite custom"

17.0 6 250.0 100.0 3329. 15.5 71 1 "chevrolet chevelle malibu"

19.0 6 250.0 88.00 3302. 15.5 71 1 "ford torino 500"

18.0 6 232.0 100.0 3288. 15.5 71 1 "amc matador"

14.0 8 350.0 165.0 4209. 12.0 71 1 "chevrolet impala"

14.0 8 400.0 175.0 4464. 11.5 71 1 "pontiac catalina brougham"

14.0 8 351.0 153.0 4154. 13.5 71 1 "ford galaxie 500"

14.0 8 318.0 150.0 4096. 13.0 71 1 "plymouth fury iii"

12.0 8 383.0 180.0 4955. 11.5 71 1 "dodge monaco (sw)"

13.0 8 400.0 170.0 4746. 12.0 71 1 "ford country squire (sw)"

13.0 8 400.0 175.0 5140. 12.0 71 1 "pontiac safari (sw)"

18.0 6 258.0 110.0 2962. 13.5 71 1 "amc hornet sportabout (sw)"

22.0 4 140.0 72.00 2408. 19.0 71 1 "chevrolet vega (sw)"

19.0 6 250.0 100.0 3282. 15.0 71 1 "pontiac firebird"

18.0 6 250.0 88.00 3139. 14.5 71 1 "ford mustang"

23.0 4 122.0 86.00 2220. 14.0 71 1 "mercury capri 2000"

28.0 4 116.0 90.00 2123. 14.0 71 2 "opel 1900"

30.0 4 79.00 70.00 2074. 19.5 71 2 "peugeot 304"

30.0 4 88.00 76.00 2065. 14.5 71 2 "fiat 124b"

31.0 4 71.00 65.00 1773. 19.0 71 3 "toyota corolla 1200"

35.0 4 72.00 69.00 1613. 18.0 71 3 "datsun 1200"

27.0 4 97.00 60.00 1834. 19.0 71 2 "volkswagen model 111"

26.0 4 91.00 70.00 1955. 20.5 71 1 "plymouth cricket"

24.0 4 113.0 95.00 2278. 15.5 72 3 "toyota corona hardtop"

25.0 4 97.50 80.00 2126. 17.0 72 1 "dodge colt hardtop"

23.0 4 97.00 54.00 2254. 23.5 72 2 "volkswagen type 3"

20.0 4 140.0 90.00 2408. 19.5 72 1 "chevrolet vega"

21.0 4 122.0 86.00 2226. 16.5 72 1 "ford pinto runabout"

13.0 8 350.0 165.0 4274. 12.0 72 1 "chevrolet impala"

14.0 8 400.0 175.0 4385. 12.0 72 1 "pontiac catalina"

15.0 8 318.0 150.0 4135. 13.5 72 1 "plymouth fury iii"

14.0 8 351.0 153.0 4129. 13.0 72 1 "ford galaxie 500"

17.0 8 304.0 150.0 3672. 11.5 72 1 "amc ambassador sst"

11.0 8 429.0 208.0 4633. 11.0 72 1 "mercury marquis"

13.0 8 350.0 155.0 4502. 13.5 72 1 "buick lesabre custom"

12.0 8 350.0 160.0 4456. 13.5 72 1 "oldsmobile delta 88 royale"

13.0 8 400.0 190.0 4422. 12.5 72 1 "chrysler newport royal"

19.0 3 70.00 97.00 2330. 13.5 72 3 "mazda rx2 coupe"

相关推荐
吃什么芹菜卷3 分钟前
机器学习:opencv--图像金字塔
人工智能·笔记·opencv·机器学习·计算机视觉
IFTICing5 分钟前
【文献阅读】Unsupervised Machine Learning for Bot Detection on Twitter
人工智能·python·机器学习·数据挖掘
우 유9 分钟前
【菜菜的sklearn机器学习】(1)决策树
人工智能·python·sklearn
AwesomeCPA23 分钟前
果蔬识别系统性能优化之路(四)
前端·人工智能·python·性能优化·tensorflow
nice-wyh23 分钟前
ORB-SLAM2关键点总结
人工智能·计算机视觉
Wils0nEdwards24 分钟前
camouflaged object detection中的decoder最核心的作用
深度学习·目标检测·计算机视觉
CaiYongji26 分钟前
深度!程序员生涯的垃圾时间(上)
人工智能·gpt·chatgpt·openai
h1771134720528 分钟前
交友系统“陌陌”全方位解析
大数据·人工智能·微信小程序·小程序·回归
h1771134720536 分钟前
定制相亲交友系统如何提升用户体验
大数据·开发语言·人工智能·小程序·系统开发
逐梦苍穹43 分钟前
速通GPT:Improving Language Understanding by Generative Pre-Training全文解读
论文阅读·人工智能·gpt·语言模型·论文笔记