当前位置:首页>python>一天一个Python知识点——Day 151:深度学习概述

一天一个Python知识点——Day 151:深度学习概述

  • 2026-02-06 21:04:09
一天一个Python知识点——Day 151:深度学习概述

一、什么是深度学习?

1. 深度学习的定义

深度学习是机器学习的一个子领域,它通过模拟人脑的神经网络结构,使用多层非线性变换来学习数据的层次化表示。其核心思想是让机器自动学习特征表示,而不是依赖人工设计的特征。

# 深度学习与传统机器学习的对比import matplotlib.pyplot as plt# 可视化对比fig, axes = plt.subplots(12, figsize=(124))# 传统机器学习axes[0].text(0.50.5, '特征工程 → 模型训练\n\n人工特征设计\n+ 传统算法\n= 有限表现力',              ha='center', va='center', fontsize=12, fontweight='bold')axes[0].set_title('传统机器学习', fontsize=14, fontweight='bold')axes[0].axis('off')# 深度学习axes[1].text(0.50.5, '原始数据 → 深度学习\n\n自动特征学习\n+ 多层神经网络\n= 强大表现力',              ha='center', va='center', fontsize=12, fontweight='bold')axes[1].set_title('深度学习', fontsize=14, fontweight='bold')axes[1].axis('off')plt.suptitle('传统机器学习 vs 深度学习', fontsize=16, fontweight='bold')plt.tight_layout()plt.show()

2. 深度学习的历史里程碑

# 深度学习发展时间线timeline_data = {    '1943''McCulloch & Pitts提出人工神经元模型',    '1958''Frank Rosenblatt发明感知机',    '1969''Minsky & Papert指出感知机的局限性',    '1986''反向传播算法重新被发现',    '1998''Yann LeCun提出LeNet-5(卷积神经网络)',    '2006''Geoffrey Hinton提出深度信念网络',    '2012''AlexNet在ImageNet比赛中大获成功',    '2014''生成对抗网络(GAN)被提出',    '2015''ResNet解决深度网络训练难题',    '2017''Transformer架构革命性突破',    '2020''GPT-3展现强大语言能力',    '2022''扩散模型引领图像生成革命'}print("深度学习发展里程碑:")for year, event in timeline_data.items():    print(f"{year}{event}")

二、神经网络基础

1. 人工神经元模型

import numpy as npclass ArtificialNeuron:    """实现基本的人工神经元"""    def __init__(self, input_size, activation='sigmoid'):        """        初始化神经元        Args:            input_size: 输入特征数量            activation: 激活函数类型        """        # 初始化权重和偏置        self.weights = np.random.randn(input_size) * 0.1        self.bias = np.random.randn() * 0.1        self.activation_type = activation    def activate(self, x):        """前向传播计算"""        # 线性组合        z = np.dot(x, self.weights) + self.bias        # 应用激活函数        if self.activation_type == 'sigmoid':            return self._sigmoid(z)        elif self.activation_type == 'relu':            return self._relu(z)        elif self.activation_type == 'tanh':            return self._tanh(z)        else:            return z  # 线性激活    def _sigmoid(self, x):        """Sigmoid激活函数"""        return 1 / (1 + np.exp(-x))    def _relu(self, x):        """ReLU激活函数"""        return np.maximum(0, x)    def _tanh(self, x):        """Tanh激活函数"""        return np.tanh(x)    def __call__(self, x):        """使神经元可调用"""        return self.activate(x)# 使用示例neuron = ArtificialNeuron(3, activation='sigmoid')inputs = np.array([0.5, -0.20.8])output = neuron(inputs)print(f"神经元输出: {output:.4f}")

2. 常用激活函数及其特性

import numpy as npimport matplotlib.pyplot as pltdef plot_activation_functions():    """绘制常用激活函数"""    x = np.linspace(-55100)    # 定义激活函数    functions = {        'Sigmoid'lambda x: 1 / (1 + np.exp(-x)),        'ReLU'lambda x: np.maximum(0, x),        'Leaky ReLU'lambda x: np.where(x > 0, x, 0.01 * x),        'Tanh'lambda x: np.tanh(x),        'Swish'lambda x: x * (1 / (1 + np.exp(-x))),        'ELU'lambda x: np.where(x > 0, x, np.exp(x) - 1),        'Softplus'lambda x: np.log(1 + np.exp(x))    }    # 创建子图    fig, axes = plt.subplots(24, figsize=(158))    axes = axes.ravel()    for idx, (name, func) in enumerate(functions.items()):        ax = axes[idx]        y = func(x)        ax.plot(x, y, 'b-', linewidth=2)        ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)        ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)        ax.grid(True, alpha=0.3)        ax.set_title(name, fontsize=12, fontweight='bold')        # 添加特性标注        if name == 'Sigmoid':            ax.text(00.5'输出范围: (0,1)\n平滑、可微\n存在梯度消失问题'                    fontsize=9, ha='center')        elif name == 'ReLU':            ax.text(02.5'稀疏激活\n计算高效\n存在"死神经元"问题'                    fontsize=9, ha='center')    # 调整布局    plt.tight_layout()    plt.suptitle('常用激活函数', fontsize=16, fontweight='bold', y=1.02)    plt.show()# 绘制激活函数plot_activation_functions()

三、深度学习架构类型

1. 前馈神经网络(FNN)

class FeedForwardNeuralNetwork:    """实现简单的前馈神经网络"""    def __init__(self, layer_sizes, activations=None):        """        初始化神经网络        Args:            layer_sizes: 每层神经元数量列表,如[10, 20, 5, 1]            activations: 每层的激活函数列表        """        self.layer_sizes = layer_sizes        self.num_layers = len(layer_sizes) - 1        # 初始化权重和偏置        self.weights = []        self.biases = []        for i in range(self.num_layers):            # Xavier/Glorot初始化            scale = np.sqrt(2.0 / (layer_sizes[i] + layer_sizes[i+1]))            w = np.random.randn(layer_sizes[i], layer_sizes[i+1]) * scale            b = np.zeros(layer_sizes[i+1])            self.weights.append(w)            self.biases.append(b)        # 设置激活函数        if activations is None:            activations = ['relu'] * (self.num_layers - 1) + ['sigmoid']        self.activations = activations    def forward(self, X):        """前向传播"""        self.activations_history = [X]        self.z_history = []        a = X        for i in range(self.num_layers):            z = np.dot(a, self.weights[i]) + self.biases[i]            self.z_history.append(z)            if self.activations[i] == 'sigmoid':                a = 1 / (1 + np.exp(-z))            elif self.activations[i] == 'relu':                a = np.maximum(0, z)            elif self.activations[i] == 'tanh':                a = np.tanh(z)            else:                a = z  # 线性激活            self.activations_history.append(a)        return a    def predict(self, X):        """预测"""        return self.forward(X)    def summary(self):        """打印网络结构信息"""        print("=" * 50)        print("神经网络结构摘要")        print("=" * 50)        print(f"层数: {self.num_layers}")        print(f"神经元配置: {self.layer_sizes}")        print(f"激活函数: {self.activations}")        total_params = 0        for i, (w, b) in enumerate(zip(self.weights, self.biases)):            params = w.size + b.size            total_params += params            print(f"层 {i+1}{w.shape[0]} → {w.shape[1]} "                  f"(权重: {w.shape}, 偏置: {b.shape}) "                  f"参数数: {params:,}")        print(f"总参数数: {total_params:,}")        print("=" * 50)# 创建并测试神经网络nn = FeedForwardNeuralNetwork(    layer_sizes=[1020151],    activations=['relu''relu''sigmoid'])nn.summary()# 测试前向传播X_test = np.random.randn(510)  # 5个样本,10个特征output = nn.predict(X_test)print(f"\n输入形状: {X_test.shape}")print(f"输出形状: {output.shape}")

2. 卷积神经网络(CNN)

class SimpleCNN:    """实现简化的卷积神经网络"""    def __init__(self, input_shape=(28281)):        """初始化CNN"""        self.input_shape = input_shape        self.layers = []    def add_conv_layer(self, filters=32, kernel_size=3, activation='relu'):        """添加卷积层"""        layer_info = {            'type''conv',            'filters': filters,            'kernel_size': kernel_size,            'activation': activation        }        self.layers.append(layer_info)        return self    def add_pooling_layer(self, pool_size=2, stride=2):        """添加池化层"""        layer_info = {            'type''pool',            'pool_size': pool_size,            'stride': stride        }        self.layers.append(layer_info)        return self    def add_dense_layer(self, units, activation='relu'):        """添加全连接层"""        layer_info = {            'type''dense',            'units': units,            'activation': activation        }        self.layers.append(layer_info)        return self    def add_flatten_layer(self):        """添加展平层"""        self.layers.append({'type''flatten'})        return self    def forward(self, X):        """前向传播(简化版)"""        # 模拟卷积操作        output = X        for layer in self.layers:            if layer['type'] == 'conv':                # 简化的卷积操作                output = self._conv_forward(output, layer)            elif layer['type'] == 'pool':                # 简化的池化操作                output = self._pool_forward(output, layer)            elif layer['type'] == 'flatten':                # 展平操作                output = output.reshape(output.shape[0], -1)            elif layer['type'] == 'dense':                # 全连接层                output = self._dense_forward(output, layer)        return output    def _conv_forward(self, X, layer_params):        """简化的卷积前向传播"""        # 在实际实现中,这里会有实际的卷积计算        # 这里我们返回一个简化的结果        batch_size, height, width, channels = X.shape        filters = layer_params['filters']        # 简化的输出形状计算        kernel_size = layer_params['kernel_size']        output_height = height - kernel_size + 1        output_width = width - kernel_size + 1        return np.random.randn(batch_size, output_height, output_width, filters)    def _pool_forward(self, X, layer_params):        """简化的池化前向传播"""        batch_size, height, width, channels = X.shape        pool_size = layer_params['pool_size']        # 简化的输出形状计算        output_height = height // pool_size        output_width = width // pool_size        return np.random.randn(batch_size, output_height, output_width, channels)    def _dense_forward(self, X, layer_params):        """简化的全连接前向传播"""        units = layer_params['units']        activation = layer_params['activation']        # 线性变换        output = np.random.randn(X.shape[0], units)        # 激活函数        if activation == 'relu':            output = np.maximum(0, output)        elif activation == 'sigmoid':            output = 1 / (1 + np.exp(-output))        return output    def summary(self):        """打印网络结构信息"""        print("=" * 60)        print("卷积神经网络结构摘要")        print("=" * 60)        print(f"输入形状: {self.input_shape}")        current_shape = self.input_shape        for i, layer in enumerate(self.layers):            layer_type = layer['type'].upper()            if layer_type == 'CONV':                filters = layer['filters']                kernel_size = layer['kernel_size']                print(f"层 {i+1}: 卷积层 "                      f"({current_shape[0]}x{current_shape[1]}x{current_shape[2]}) → "                      f"过滤器: {filters}, 核大小: {kernel_size}x{kernel_size}")                # 更新形状(简化计算)                current_shape = (                    current_shape[0] - kernel_size + 1,                    current_shape[1] - kernel_size + 1,                    filters                )            elif layer_type == 'POOL':                pool_size = layer['pool_size']                print(f"层 {i+1}: 池化层 "                      f"({current_shape[0]}x{current_shape[1]}x{current_shape[2]}) → "                      f"池化大小: {pool_size}x{pool_size}")                # 更新形状                current_shape = (                    current_shape[0] // pool_size,                    current_shape[1] // pool_size,                    current_shape[2]                )            elif layer_type == 'FLATTEN':                flattened_size = np.prod(current_shape)                print(f"层 {i+1}: 展平层 "                      f"({current_shape[0]}x{current_shape[1]}x{current_shape[2]}) → "                      f"({flattened_size},)")                current_shape = (flattened_size,)            elif layer_type == 'DENSE':                units = layer['units']                print(f"层 {i+1}: 全连接层 "                      f"({current_shape[0ifisinstance(current_shape, tupleelse current_shape}) → "                      f"({units},)")                current_shape = (units,)        print("=" * 60)# 创建并测试CNNcnn = SimpleCNN(input_shape=(28281))cnn.add_conv_layer(filters=32, kernel_size=3, activation='relu')cnn.add_pooling_layer(pool_size=2)cnn.add_conv_layer(filters=64, kernel_size=3, activation='relu')cnn.add_pooling_layer(pool_size=2)cnn.add_flatten_layer()cnn.add_dense_layer(units=128, activation='relu')cnn.add_dense_layer(units=10, activation='softmax')cnn.summary()

3. 循环神经网络(RNN)

class SimpleRNN:    """实现简化的循环神经网络"""    def __init__(self, input_size, hidden_size, output_size):        """初始化RNN"""        self.input_size = input_size        self.hidden_size = hidden_size        self.output_size = output_size        # 初始化参数        # 输入到隐藏层的权重        self.W_xh = np.random.randn(input_size, hidden_size) * 0.01        # 隐藏层到隐藏层的权重        self.W_hh = np.random.randn(hidden_size, hidden_size) * 0.01        # 隐藏层到输出层的权重        self.W_hy = np.random.randn(hidden_size, output_size) * 0.01        # 偏置项        self.b_h = np.zeros(hidden_size)        self.b_y = np.zeros(output_size)        # 缓存        self.history = {}    def forward(self, X):        """        前向传播        Args:            X: 输入序列,形状为(seq_length, batch_size, input_size)        Returns:            输出序列        """        seq_length, batch_size, _ = X.shape        # 初始化隐藏状态        h = np.zeros((batch_size, self.hidden_size))        # 存储历史值        self.history['h_states'] = [h]        self.history['inputs'] = X        # 存储输出        outputs = []        for t in range(seq_length):            # 当前时间步的输入            x_t = X[t]            # 更新隐藏状态            h = np.tanh(np.dot(x_t, self.W_xh) + np.dot(h, self.W_hh) + self.b_h)            self.history['h_states'].append(h)            # 计算输出            y_t = np.dot(h, self.W_hy) + self.b_y            outputs.append(y_t)        # 堆叠所有时间步的输出        return np.stack(outputs)    def backward(self, d_outputs):        """反向传播(简化版)"""        # 在实际实现中,这里会有详细的反向传播计算        # 这里我们返回梯度的简化版本        gradients = {            'dW_xh': np.random.randn(*self.W_xh.shape) * 0.01,            'dW_hh': np.random.randn(*self.W_hh.shape) * 0.01,            'dW_hy': np.random.randn(*self.W_hy.shape) * 0.01,            'db_h': np.random.randn(*self.b_h.shape) * 0.01,            'db_y': np.random.randn(*self.b_y.shape) * 0.01        }        return gradients    def summary(self):        """打印网络结构信息"""        print("=" * 50)        print("循环神经网络结构摘要")        print("=" * 50)        print(f"输入大小: {self.input_size}")        print(f"隐藏层大小: {self.hidden_size}")        print(f"输出大小: {self.output_size}")        print(f"总参数数: {self.W_xh.size + self.W_hh.size + self.W_hy.size + self.b_h.size + self.b_y.size:,}")        print("=" * 50)    def generate_sequence(self, seed, length=20):        """生成序列(简化版)"""        # 在实际实现中,这里会有序列生成逻辑        generated = [seed]        h = np.zeros((1self.hidden_size))        for i in range(length):            # 简化的序列生成            x = generated[-1]            h = np.tanh(np.dot(x, self.W_xh) + np.dot(h, self.W_hh) + self.b_h)            y = np.dot(h, self.W_hy) + self.b_y            # 添加一些随机性            next_item = y + np.random.randn(*y.shape) * 0.1            generated.append(next_item)        return np.stack(generated)# 创建并测试RNNrnn = SimpleRNN(input_size=10, hidden_size=20, output_size=5)rnn.summary()# 测试前向传播seq_length = 15batch_size = 8X_test = np.random.randn(seq_length, batch_size, 10)output = rnn.forward(X_test)print(f"\n输入形状: {X_test.shape}")print(f"输出形状: {output.shape}")

四、深度学习框架比较

1. 主要框架对比

import pandas as pd# 创建框架对比表格frameworks_data = {    '框架': ['TensorFlow''PyTorch''Keras''MXNet''JAX''PaddlePaddle'],    '发布年份': [201520162015201520182016],    '开发者': ['Google''Facebook''François Chollet''Amazon''Google''百度'],    '主要语言': ['Python/C++''Python/C++''Python''Python/C++''Python''Python'],    '易用性': [355344],    '灵活性': [553554],    '部署能力': [543545],    '社区规模': [554333],    '主要优势': [        '生产部署、生态完善',        '动态图、研究友好',        'API简洁、快速原型',        '分布式训练、多语言',        '函数式、自动微分',        '中文文档、国产框架'    ]}frameworks_df = pd.DataFrame(frameworks_data)# 格式化输出print("深度学习框架对比")print("=" * 100)print(frameworks_df.to_string(index=False))print("\n" + "=" * 100)# 创建选择指南print("\n选择指南:")print("1. TensorFlow: 适合生产部署,企业级应用,需要强大生态系统")print("2. PyTorch: 适合学术研究,快速原型开发,动态计算图")print("3. Keras: 适合初学者,快速上手,高级API")print("4. MXNet: 适合分布式训练,多语言支持")print("5. JAX: 适合函数式编程,数值计算,自动微分")print("6. PaddlePaddle: 国产框架,中文文档丰富,工业级应用")

2. 框架安装与简单示例

def get_framework_setup_guide():    """获取框架安装和使用指南"""    guides = {        'TensorFlow': {            '安装''pip install tensorflow',            '导入''import tensorflow as tf',            '简单示例''''# 创建简单的神经网络model = tf.keras.Sequential([    tf.keras.layers.Dense(64, activation='relu'),    tf.keras.layers.Dense(10, activation='softmax')])model.compile(optimizer='adam',              loss='sparse_categorical_crossentropy',              metrics=['accuracy'])            ''',            '最新版本''2.x (2023年)'        },        'PyTorch': {            '安装''pip install torch torchvision',            '导入''import torch',            '简单示例''''# 创建简单的神经网络class Net(torch.nn.Module):    def __init__(self):        super().__init__()        self.fc1 = torch.nn.Linear(784, 64)        self.fc2 = torch.nn.Linear(64, 10)    def forward(self, x):        x = torch.relu(self.fc1(x))        x = self.fc2(x)        return x            ''',            '最新版本''2.0+ (2023年)'        },        'Keras': {            '安装''pip install keras',            '导入''from keras import layers, models',            '简单示例''''# 创建简单的神经网络model = models.Sequential()model.add(layers.Dense(64, activation='relu', input_shape=(784,)))model.add(layers.Dense(10, activation='softmax'))model.compile(optimizer='rmsprop',              loss='categorical_crossentropy',              metrics=['accuracy'])            ''',            '最新版本''3.0 (2023年)'        }    }    return guides# 打印框架指南guides = get_framework_setup_guide()for framework, info in guides.items():    print(f"\n{'='*60}")    print(f"{framework} 快速指南")    print(f"{'='*60}")    for key, value in info.items():        print(f"{key}{value}")

五、深度学习的关键概念

1. 损失函数

import numpy as npimport matplotlib.pyplot as pltclass LossFunctions:    """实现常见的损失函数"""    @staticmethod    def mse(y_true, y_pred):        """均方误差 (Mean Squared Error)"""        return np.mean((y_true - y_pred) ** 2)    @staticmethod    def mae(y_true, y_pred):        """平均绝对误差 (Mean Absolute Error)"""        return np.mean(np.abs(y_true - y_pred))    @staticmethod    def binary_crossentropy(y_true, y_pred, epsilon=1e-7):        """二分类交叉熵"""        y_pred = np.clip(y_pred, epsilon, 1 - epsilon)        return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))    @staticmethod    def categorical_crossentropy(y_true, y_pred, epsilon=1e-7):        """多分类交叉熵"""        y_pred = np.clip(y_pred, epsilon, 1 - epsilon)        return -np.mean(np.sum(y_true * np.log(y_pred), axis=-1))    @staticmethod    def huber_loss(y_true, y_pred, delta=1.0):        """Huber损失 (结合MSE和MAE的优点)"""        error = y_true - y_pred        abs_error = np.abs(error)        quadratic = np.minimum(abs_error, delta)        linear = abs_error - quadratic        return np.mean(0.5 * quadratic ** 2 + delta * linear)    @staticmethod    def contrastive_loss(y_true, y_pred, margin=1.0):        """对比损失 (用于度量学习)"""        positive_distance = y_true * y_pred ** 2        negative_distance = (1 - y_true) * np.maximum(margin - y_pred, 0) ** 2        return np.mean(positive_distance + negative_distance)    @staticmethod    def visualize_loss_functions():        """可视化损失函数"""        fig, axes = plt.subplots(23, figsize=(1510))        axes = axes.ravel()        x = np.linspace(-33100)        y_true = 0  # 假设真实值为0        # MSE        y_pred = x        loss = (y_true - y_pred) ** 2        axes[0].plot(x, loss, 'b-', linewidth=2)        axes[0].set_title('MSE (均方误差)', fontweight='bold')        axes[0].set_xlabel('预测值')        axes[0].set_ylabel('损失')        axes[0].grid(True, alpha=0.3)        # MAE        loss = np.abs(y_true - y_pred)        axes[1].plot(x, loss, 'r-', linewidth=2)        axes[1].set_title('MAE (平均绝对误差)', fontweight='bold')        axes[1].set_xlabel('预测值')        axes[1].grid(True, alpha=0.3)        # Huber Loss        delta = 1.0        loss = np.where(np.abs(x) <= delta, 0.5 * x ** 2, delta * (np.abs(x) - 0.5 * delta))        axes[2].plot(x, loss, 'g-', linewidth=2)        axes[2].set_title('Huber损失', fontweight='bold')        axes[2].set_xlabel('预测值')        axes[2].grid(True, alpha=0.3)        # Binary Crossentropy (假设真实值为1)        y_true_binary = 1        y_pred_binary = 1 / (1 + np.exp(-x))  # Sigmoid变换        loss = - (y_true_binary * np.log(y_pred_binary) +                  (1 - y_true_binary) * np.log(1 - y_pred_binary))        axes[3].plot(x, loss, 'm-', linewidth=2)        axes[3].set_title('二分类交叉熵 (y_true=1)', fontweight='bold')        axes[3].set_xlabel('预测值 (z)')        axes[3].grid(True, alpha=0.3)        # 对比损失        distance = np.abs(x)        margin = 1.0        y_true_contrastive = np.ones_like(x)  # 假设是正样本对        loss = y_true_contrastive * distance ** 2        axes[4].plot(x, loss, 'c-', linewidth=2, label='正样本')        y_true_contrastive = np.zeros_like(x)  # 假设是负样本对        loss = (1 - y_true_contrastive) * np.maximum(margin - distance, 0) ** 2        axes[4].plot(x, loss, 'y-', linewidth=2, label='负样本')        axes[4].set_title('对比损失', fontweight='bold')        axes[4].set_xlabel('距离')        axes[4].legend()        axes[4].grid(True, alpha=0.3)        # 损失函数应用场景        axes[5].axis('off')        axes[5].text(0.50.5                    '损失函数选择指南:\n\n'                    '• MSE: 回归问题,对异常值敏感\n'                    '• MAE: 回归问题,对异常值鲁棒\n'                    '• Huber: 结合MSE和MAE的优点\n'                    '• Binary CE: 二分类问题\n'                    '• Categorical CE: 多分类问题\n'                    '• Contrastive: 度量学习,相似度计算',                    ha='center', va='center', fontsize=11,                    bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))        plt.suptitle('深度学习常用损失函数', fontsize=16, fontweight='bold', y=1.02)        plt.tight_layout()        plt.show()# 测试损失函数loss_funcs = LossFunctions()# 测试数据y_true = np.array([1010])y_pred = np.array([0.90.20.80.3])print("损失函数计算结果:")print(f"MSE: {loss_funcs.mse(y_true, y_pred):.4f}")print(f"MAE: {loss_funcs.mae(y_true, y_pred):.4f}")print(f"Binary Crossentropy: {loss_funcs.binary_crossentropy(y_true, y_pred):.4f}")# 可视化损失函数LossFunctions.visualize_loss_functions()

2. 优化器

class OptimizerComparison:    """优化器比较和可视化"""    @staticmethod    def visualize_optimization_path():        """可视化不同优化器的优化路径"""        # 定义测试函数 (Rosenbrock函数,有全局最小值)        def rosenbrock(x, y):            return (1 - x) ** 2 + 100 * (y - x ** 2) ** 2        # 生成网格        x = np.linspace(-22100)        y = np.linspace(-13100)        X, Y = np.meshgrid(x, y)        Z = rosenbrock(X, Y)        # 优化器模拟        optimizers = {            'SGD': {                'lr'0.01,                'momentum'0.0            },            'SGD with Momentum': {                'lr'0.01,                'momentum'0.9            },            'Adam': {                'lr'0.01,                'beta1'0.9,                'beta2'0.999            },            'RMSprop': {                'lr'0.01,                'rho'0.9            },            'Adagrad': {                'lr'0.1            }        }        # 创建图形        fig, axes = plt.subplots(23, figsize=(1510))        axes = axes.ravel()        for idx, (name, params) in enumerate(optimizers.items()):            ax = axes[idx]            # 绘制等高线            ax.contour(X, Y, Z, levels=np.logspace(-1320), alpha=0.5)            # 初始化参数            x_pos, y_pos = -1.52.5            # 存储轨迹            trajectory = [(x_pos, y_pos)]            # 模拟优化过程            for step in range(100):                # 计算梯度                grad_x = -2 * (1 - x_pos) - 400 * x_pos * (y_pos - x_pos ** 2)                grad_y = 200 * (y_pos - x_pos ** 2)                # 应用不同优化器更新规则                if name == 'SGD':                    x_pos -= params['lr'] * grad_x                    y_pos -= params['lr'] * grad_y                elif name == 'SGD with Momentum':                    # 简化的动量实现                    if step == 0:                        vx, vy = 00                    vx = params['momentum'] * vx + params['lr'] * grad_x                    vy = params['momentum'] * vy + params['lr'] * grad_y                    x_pos -= vx                    y_pos -= vy                elif name == 'Adam':                    # 简化的Adam实现                    if step == 0:                        m1x, m1y = 00                        m2x, m2y = 00                    m1x = params['beta1'] * m1x + (1 - params['beta1']) * grad_x                    m1y = params['beta1'] * m1y + (1 - params['beta1']) * grad_y                    m2x = params['beta2'] * m2x + (1 - params['beta2']) * grad_x ** 2                    m2y = params['beta2'] * m2y + (1 - params['beta2']) * grad_y ** 2                    # 偏置校正                    m1x_hat = m1x / (1 - params['beta1'] ** (step + 1))                    m1y_hat = m1y / (1 - params['beta1'] ** (step + 1))                    m2x_hat = m2x / (1 - params['beta2'] ** (step + 1))                    m2y_hat = m2y / (1 - params['beta2'] ** (step + 1))                    x_pos -= params['lr'] * m1x_hat / (np.sqrt(m2x_hat) + 1e-8)                    y_pos -= params['lr'] * m1y_hat / (np.sqrt(m2y_hat) + 1e-8)                trajectory.append((x_pos, y_pos))            # 绘制轨迹            trajectory = np.array(trajectory)            ax.plot(trajectory[:, 0], trajectory[:, 1], 'ro-', linewidth=2, markersize=3)            ax.plot(trajectory[00], trajectory[01], 'go', markersize=8, label='起点')            ax.plot(trajectory[-10], trajectory[-11], 'bo', markersize=8, label='终点')            ax.set_title(name, fontweight='bold')            ax.set_xlabel('x')            ax.set_ylabel('y')            ax.legend()            ax.grid(True, alpha=0.3)        # 优化器选择指南        axes[5].axis('off')        axes[5].text(0.50.5                    '优化器选择指南:\n\n'                    '• SGD: 简单,收敛慢,可能震荡\n'                    '• SGD+Momentum: 减少震荡,加速收敛\n'                    '• Adam: 自适应学习率,通常表现好\n'                    '• RMSprop: 适合非平稳目标\n'                    '• Adagrad: 适合稀疏数据\n\n'                    '一般推荐: Adam (默认选择)',                    ha='center', va='center', fontsize=11,                    bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.5))        plt.suptitle('优化器比较:在Rosenbrock函数上的优化路径'                    fontsize=16, fontweight='bold', y=1.02)        plt.tight_layout()        plt.show()    @staticmethod    def optimizer_summary():        """优化器特性总结"""        optimizers_info = {            'SGD': {                '公式''θ = θ - η·∇J(θ)',                '优点''简单,理论基础强',                '缺点''收敛慢,易震荡',                '适用场景''凸优化问题'            },            'Momentum': {                '公式''v = βv + η·∇J(θ)\nθ = θ - v',                '优点''加速收敛,减少震荡',                '缺点''需要调节β参数',                '适用场景''深度学习训练'            },            'Adam': {                '公式''复杂,结合动量和自适应学习率',                '优点''自适应学习率,通常表现优异',                '缺点''内存占用稍大',                '适用场景''深度学习(默认推荐)'            },            'RMSprop': {                '公式''E[g²] = ρE[g²] + (1-ρ)g²\nθ = θ - η·g/√(E[g²]+ε)',                '优点''自适应学习率,适合非平稳目标',                '缺点''需要调节ρ参数',                '适用场景''RNN训练'            },            'Adagrad': {                '公式''G = G + g⊙g\nθ = θ - η·g/√(G+ε)',                '优点''自适应学习率,适合稀疏数据',                '缺点''学习率单调递减',                '适用场景''稀疏特征学习'            }        }        print("=" * 80)        print("深度学习优化器总结")        print("=" * 80)        for name, info in optimizers_info.items():            print(f"\n{name}:")            print(f"  公式: {info['公式']}")            print(f"  优点: {info['优点']}")            print(f"  缺点: {info['缺点']}")            print(f"  适用场景: {info['适用场景']}")        print("\n" + "=" * 80)# 显示优化器信息OptimizerComparison.optimizer_summary()OptimizerComparison.visualize_optimization_path()

六、深度学习训练技巧

1. 正则化技术

class RegularizationTechniques:    """深度学习正则化技术"""    @staticmethod    def l1_regularization(weights, lambda_l1):        """L1正则化 (Lasso)"""        return lambda_l1 * np.sum(np.abs(weights))    @staticmethod    def l2_regularization(weights, lambda_l2):        """L2正则化 (Ridge)"""        return lambda_l2 * np.sum(weights ** 2)    @staticmethod    def elastic_net(weights, lambda_l1, lambda_l2):        """弹性网络 (结合L1和L2)"""        return (lambda_l1 * np.sum(np.abs(weights)) +                 lambda_l2 * np.sum(weights ** 2))    @staticmethod    def dropout(activations, dropout_rate, training=True):        """Dropout正则化"""        if not training:            return activations        # 生成Dropout掩码        mask = np.random.binomial(11 - dropout_rate, size=activations.shape)        # 应用Dropout并缩放        activations = activations * mask / (1 - dropout_rate)        return activations    @staticmethod    def batch_normalization(x, gamma=1, beta=0, epsilon=1e-5):        """批量归一化 (简化版)"""        # 计算批次的均值和方差        mean = np.mean(x, axis=0)        variance = np.var(x, axis=0)        # 归一化        x_norm = (x - mean) / np.sqrt(variance + epsilon)        # 缩放和偏移        return gamma * x_norm + beta    @staticmethod    def data_augmentation_examples():        """数据增强示例"""        techniques = {            '图像数据增强': [                '随机旋转 (±30度)',                '随机缩放 (0.8-1.2倍)',                '随机裁剪',                '随机水平翻转',                '颜色抖动 (亮度、对比度、饱和度)',                '随机噪声添加'            ],            '文本数据增强': [                '同义词替换',                '随机插入',                '随机交换',                '随机删除',                '回译 (翻译成其他语言再译回)',                'EDA (Easy Data Augmentation)'            ],            '时间序列增强': [                '时间扭曲',                '窗口滑动',                '随机缩放',                '添加噪声',                '通道混洗 (多变量时)'            ]        }        print("数据增强技术:")        print("=" * 60)        for category, methods in techniques.items():            print(f"\n{category}:")            for method in methods:                print(f"  • {method}")        print("\n" + "=" * 60)    @staticmethod    def early_stopping_callback(patience=10, min_delta=0.001):        """早停回调函数"""        class EarlyStopping:            def __init__(self, patience=patience, min_delta=min_delta):                self.patience = patience                self.min_delta = min_delta                self.best_loss = float('inf')                self.counter = 0                self.should_stop = False            def __call__(self, current_loss):                if current_loss < self.best_loss - self.min_delta:                    self.best_loss = current_loss                    self.counter = 0                    print(f"损失改善: {current_loss:.4f}")                    return False                else:                    self.counter += 1                    print(f"早停计数: {self.counter}/{self.patience}")                    if self.counter >= self.patience:                        self.should_stop = True                        print("达到早停条件,停止训练")                    return self.should_stop        return EarlyStopping()# 测试正则化技术reg = RegularizationTechniques()# 测试Dropoutactivations = np.array([[1.02.03.0],                        [4.05.06.0]])dropout_rate = 0.5print("Dropout示例:")print(f"原始激活值:\n{activations}")print(f"Dropout后 (训练模式):\n{reg.dropout(activations, dropout_rate, training=True)}")print(f"Dropout后 (推理模式):\n{reg.dropout(activations, dropout_rate, training=False)}")# 显示数据增强技术reg.data_augmentation_examples()

2. 超参数调优

class HyperparameterTuning:    """深度学习超参数调优"""    @staticmethod    def learning_rate_scheduler():        """学习率调度器"""        schedulers = {            '固定学习率': {                '描述''整个训练过程使用固定学习率',                '适用场景''简单任务,小数据集',                '代码示例''lr = 0.001'            },            '阶梯下降': {                '描述''在指定轮次降低学习率',                '适用场景''大多数深度学习任务',                '代码示例''''if epoch % 30 == 0:    lr *= 0.1                '''            },            '余弦退火': {                '描述''学习率按余弦函数从高到低变化',                '适用场景''需要跳出局部最优的任务',                '公式''lr = lr_min + 0.5*(lr_max-lr_min)*(1+cos(epoch/T_max*π))'            },            '循环学习率': {                '描述''学习率在最小和最大值之间循环变化',                '适用场景''提高模型泛化能力',                '代码示例''''cycle = epoch % cycle_lengthlr = lr_min + 0.5*(lr_max-lr_min)*(1+cos(cycle/cycle_length*π))                '''            },            '热重启': {                '描述''周期性重启学习率,每次重启后逐渐降低峰值',                '适用场景''复杂任务,需要精细调优',                '优势''结合大范围探索和精细调优'            }        }        print("学习率调度策略:")        print("=" * 80)        for name, info in schedulers.items():            print(f"\n{name}:")            print(f"  描述: {info['描述']}")            print(f"  适用场景: {info['适用场景']}")            if '公式' in info:                print(f"  公式: {info['公式']}")            if '代码示例' in info:                print(f"  代码示例: {info['代码示例']}")        print("\n" + "=" * 80)    @staticmethod    def visualize_learning_rates():        """可视化不同学习率调度策略"""        epochs = 100        # 不同调度策略        strategies = {            '固定学习率': [0.001] * epochs,            '阶梯下降': [],            '指数衰减': [],            '余弦退火': [],            '循环学习率': []        }        # 生成学习率序列        for epoch in range(epochs):            # 阶梯下降 (每30轮降低10倍)            lr = 0.001            if epoch >= 30:                lr *= 0.1            if epoch >= 60:                lr *= 0.1            strategies['阶梯下降'].append(lr)            # 指数衰减            strategies['指数衰减'].append(0.001 * np.exp(-0.05 * epoch))            # 余弦退火            T_max = 50            lr_min = 0.0001            lr_max = 0.01            strategies['余弦退火'].append(                lr_min + 0.5 * (lr_max - lr_min) *                 (1 + np.cos(epoch / T_max * np.pi))            )            # 循环学习率            cycle_length = 20            cycle = epoch % cycle_length            strategies['循环学习率'].append(                0.0001 + 0.5 * (0.01 - 0.0001) *                 (1 + np.cos(cycle / cycle_length * np.pi))            )        # 绘制图形        plt.figure(figsize=(128))        for idx, (name, lr_sequence) in enumerate(strategies.items()):            plt.plot(lr_sequence, linewidth=2, label=name)        plt.xlabel('训练轮次', fontsize=12)        plt.ylabel('学习率', fontsize=12)        plt.title('不同学习率调度策略比较', fontsize=16, fontweight='bold')        plt.legend(fontsize=10)        plt.grid(True, alpha=0.3)        plt.yscale('log')  # 对数尺度        plt.tight_layout()        plt.show()    @staticmethod    def hyperparameter_search_space():        """深度学习超参数搜索空间"""        search_space = {            '学习率': {                '范围': [1e-51e-1],                '推荐值': [1e-33e-41e-4],                '搜索策略''对数均匀采样',                '备注''最重要的超参数'            },            '批量大小': {                '范围': [16256],                '推荐值': [3264128],                '搜索策略''均匀采样 (2的幂次)',                '备注''GPU内存允许的情况下尽量大'            },            '网络深度': {                '范围': [220],                '推荐值': [35812],                '搜索策略''均匀采样',                '备注''根据任务复杂度选择'            },            'Dropout率': {                '范围': [0.00.5],                '推荐值': [0.20.30.5],                '搜索策略''均匀采样',                '备注''正则化强度,防止过拟合'            },            '权重衰减': {                '范围': [0.00.1],                '推荐值': [1e-41e-50.0],                '搜索策略''对数均匀采样',                '备注''L2正则化强度'            },            '优化器': {                '选项': ['Adam''SGD''RMSprop''Adagrad'],                '推荐值''Adam',                '搜索策略''类别采样',                '备注''Adam通常是默认选择'            }        }        print("深度学习超参数搜索空间:")        print("=" * 100)        for param, info in search_space.items():            print(f"\n{param}:")            for key, value in info.items():                print(f"  {key}{value}")        print("\n" + "=" * 100)        print("\n调优策略建议:")        print("1. 先调学习率,固定其他参数")        print("2. 然后调批量大小和网络结构")        print("3. 最后调正则化相关参数")        print("4. 使用贝叶斯优化或随机搜索")        print("5. 早停防止过拟合")# 显示超参数调优信息tuning = HyperparameterTuning()tuning.hyperparameter_search_space()tuning.learning_rate_scheduler()tuning.visualize_learning_rates()

七、实践项目:手写数字识别

import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import fetch_openmlfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import OneHotEncoderclass MNISTDigitRecognition:    """手写数字识别实践项目"""    def __init__(self):        """初始化"""        self.X_train = None        self.y_train = None        self.X_test = None        self.y_test = None        self.model = None    def load_data(self):        """加载MNIST数据集"""        print("加载MNIST数据集...")        # 使用fetch_openml加载MNIST数据集        mnist = fetch_openml('mnist_784', version=1, parser='auto')        X = mnist.data.astype('float32') / 255.0  # 归一化        y = mnist.target.astype('int')        # 划分训练集和测试集        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(            X, y, test_size=0.2, random_state=42, stratify=y        )        print(f"训练集大小: {self.X_train.shape}")        print(f"测试集大小: {self.X_test.shape}")        print(f"类别分布: {np.bincount(self.y_train)}")        return self    def visualize_samples(self, n_samples=10):        """可视化样本"""        plt.figure(figsize=(156))        for i in range(n_samples):            plt.subplot(2, n_samples//2, i+1)            image = self.X_train[i].reshape(2828)            plt.imshow(image, cmap='gray')            plt.title(f"标签: {self.y_train[i]}")            plt.axis('off')        plt.suptitle('MNIST手写数字样本', fontsize=16, fontweight='bold')        plt.tight_layout()        plt.show()    def create_simple_model(self):        """创建简单的神经网络模型"""        class SimpleNN:            """简单的全连接神经网络"""            def __init__(self, input_size=784, hidden_size=128, output_size=10):                self.input_size = input_size                self.hidden_size = hidden_size                self.output_size = output_size                # 初始化参数                self.W1 = np.random.randn(input_size, hidden_size) * np.sqrt(2.0 / input_size)                self.b1 = np.zeros(hidden_size)                self.W2 = np.random.randn(hidden_size, output_size) * np.sqrt(2.0 / hidden_size)                self.b2 = np.zeros(output_size)                # 缓存                self.cache = {}            def relu(self, x):                """ReLU激活函数"""                return np.maximum(0, x)            def softmax(self, x):                """Softmax激活函数"""                exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))                return exp_x / np.sum(exp_x, axis=1, keepdims=True)            def forward(self, X):                """前向传播"""                # 第一层                z1 = np.dot(X, self.W1) + self.b1                a1 = self.relu(z1)                # 第二层                z2 = np.dot(a1, self.W2) + self.b2                a2 = self.softmax(z2)                # 缓存中间结果                self.cache = {'X': X, 'z1': z1, 'a1': a1, 'z2': z2, 'a2': a2}                return a2            def backward(self, X, y, learning_rate=0.01):                """反向传播"""                m = X.shape[0]                # 从缓存中获取前向传播的结果                z1 = self.cache['z1']                a1 = self.cache['a1']                a2 = self.cache['a2']                # 将y转换为one-hot编码                y_onehot = np.zeros((m, self.output_size))                y_onehot[np.arange(m), y] = 1                # 计算输出层的梯度                dz2 = a2 - y_onehot                dW2 = np.dot(a1.T, dz2) / m                db2 = np.sum(dz2, axis=0) / m                # 计算隐藏层的梯度                da1 = np.dot(dz2, self.W2.T)                dz1 = da1 * (z1 > 0)  # ReLU的梯度                dW1 = np.dot(X.T, dz1) / m                db1 = np.sum(dz1, axis=0) / m                # 更新参数                self.W2 -= learning_rate * dW2                self.b2 -= learning_rate * db2                self.W1 -= learning_rate * dW1                self.b1 -= learning_rate * db1            def predict(self, X):                """预测"""                probas = self.forward(X)                return np.argmax(probas, axis=1)            def evaluate(self, X, y):                """评估模型"""                y_pred = self.predict(X)                accuracy = np.mean(y_pred == y)                return accuracy            def summary(self):                """打印模型信息"""                print("=" * 50)                print("简单神经网络模型")                print("=" * 50)                print(f"输入大小: {self.input_size}")                print(f"隐藏层大小: {self.hidden_size}")                print(f"输出大小: {self.output_size}")                total_params = (self.W1.size + self.b1.size +                               self.W2.size + self.b2.size)                print(f"总参数数: {total_params:,}")                print("=" * 50)        self.model = SimpleNN()        return self.model    def train_model(self, epochs=10, batch_size=64, learning_rate=0.01):        """训练模型"""        n_samples = self.X_train.shape[0]        n_batches = n_samples // batch_size        print(f"开始训练...")        print(f"训练样本数: {n_samples}")        print(f"批次大小: {batch_size}")        print(f"批次数: {n_batches}")        print(f"训练轮次: {epochs}")        train_losses = []        train_accuracies = []        test_accuracies = []        for epoch in range(epochs):            epoch_loss = 0            epoch_accuracy = 0            # 打乱数据            indices = np.random.permutation(n_samples)            X_shuffled = self.X_train[indices]            y_shuffled = self.y_train[indices]            for batch in range(n_batches):                # 获取当前批次数据                start = batch * batch_size                end = start + batch_size                X_batch = X_shuffled[start:end]                y_batch = y_shuffled[start:end]                # 前向传播                y_pred = self.model.forward(X_batch)                # 计算损失(交叉熵)                m = X_batch.shape[0]                y_onehot = np.zeros((m, 10))                y_onehot[np.arange(m), y_batch] = 1                loss = -np.mean(np.sum(y_onehot * np.log(y_pred + 1e-8), axis=1))                epoch_loss += loss                # 计算准确率                batch_pred = np.argmax(y_pred, axis=1)                batch_acc = np.mean(batch_pred == y_batch)                epoch_accuracy += batch_acc                # 反向传播和参数更新                self.model.backward(X_batch, y_batch, learning_rate)            # 计算平均损失和准确率            avg_loss = epoch_loss / n_batches            avg_accuracy = epoch_accuracy / n_batches            # 测试集准确率            test_acc = self.model.evaluate(self.X_test, self.y_test)            train_losses.append(avg_loss)            train_accuracies.append(avg_accuracy)            test_accuracies.append(test_acc)            print(f"轮次 {epoch+1}/{epochs}: "                  f"训练损失={avg_loss:.4f}, "                  f"训练准确率={avg_accuracy:.4f}, "                  f"测试准确率={test_acc:.4f}")        # 可视化训练过程        self.plot_training_history(train_losses, train_accuracies, test_accuracies)        return train_losses, train_accuracies, test_accuracies    def plot_training_history(self, train_losses, train_accuracies, test_accuracies):        """绘制训练历史"""        fig, axes = plt.subplots(12, figsize=(124))        # 损失曲线        axes[0].plot(train_losses, 'b-', linewidth=2, label='训练损失')        axes[0].set_xlabel('训练轮次')        axes[0].set_ylabel('损失')        axes[0].set_title('训练损失曲线')        axes[0].legend()        axes[0].grid(True, alpha=0.3)        # 准确率曲线        axes[1].plot(train_accuracies, 'g-', linewidth=2, label='训练准确率')        axes[1].plot(test_accuracies, 'r-', linewidth=2, label='测试准确率')        axes[1].set_xlabel('训练轮次')        axes[1].set_ylabel('准确率')        axes[1].set_title('准确率曲线')        axes[1].legend()        axes[1].grid(True, alpha=0.3)        plt.suptitle('MNIST手写数字识别训练历史', fontsize=14, fontweight='bold')        plt.tight_layout()        plt.show()    def show_predictions(self, n_samples=15):        """展示预测结果"""        # 随机选择测试样本        indices = np.random.choice(len(self.X_test), n_samples, replace=False)        X_sample = self.X_test[indices]        y_sample = self.y_test[indices]        # 预测        y_pred = self.model.predict(X_sample)        # 可视化        plt.figure(figsize=(1510))        n_cols = 5        n_rows = int(np.ceil(n_samples / n_cols))        for i, idx in enumerate(indices):            plt.subplot(n_rows, n_cols, i+1)            image = X_sample[i].reshape(2828)            plt.imshow(image, cmap='gray')            # 标记正确/错误            is_correct = y_pred[i] == y_sample[i]            color = 'green' if is_correct else 'red'            plt.title(f"真实: {y_sample[i]}\n预测: {y_pred[i]}", color=color)            plt.axis('off')        accuracy = np.mean(y_pred == y_sample)        plt.suptitle(f'预测结果 (准确率: {accuracy:.2%})', fontsize=16, fontweight='bold')        plt.tight_layout()        plt.show()        # 打印混淆矩阵(简化)        print("\n预测结果统计:")        print(f"样本数: {n_samples}")        print(f"正确数: {np.sum(y_pred == y_sample)}")        print(f"错误数: {np.sum(y_pred != y_sample)}")        print(f"准确率: {accuracy:.2%}")# 运行MNIST手写数字识别项目mnist_project = MNISTDigitRecognition()# 加载数据mnist_project.load_data()# 可视化样本mnist_project.visualize_samples(10)# 创建模型model = mnist_project.create_simple_model()model.summary()# 训练模型train_losses, train_accuracies, test_accuracies = mnist_project.train_model(    epochs=20,    batch_size=128,    learning_rate=0.01)# 展示预测结果mnist_project.show_predictions(15)
深度学习正在改变世界。 从图像识别到自然语言处理,从自动驾驶到医疗诊断,深度学习的应用无处不在。虽然深度学习技术看起来很复杂,但通过系统的学习和实践,你也可以掌握这项强大的技术。

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-02-07 12:15:08 HTTP/2.0 GET : https://f.mffb.com.cn/a/473971.html
  2. 运行时间 : 0.327037s [ 吞吐率:3.06req/s ] 内存消耗:4,597.97kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=2c406e23baa797122f3cde2ff1999e97
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000940s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.001596s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.004055s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.014345s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.001503s ]
  6. SELECT * FROM `set` [ RunTime:0.015677s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.001585s ]
  8. SELECT * FROM `article` WHERE `id` = 473971 LIMIT 1 [ RunTime:0.081947s ]
  9. UPDATE `article` SET `lasttime` = 1770437708 WHERE `id` = 473971 [ RunTime:0.026643s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.003942s ]
  11. SELECT * FROM `article` WHERE `id` < 473971 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.003587s ]
  12. SELECT * FROM `article` WHERE `id` > 473971 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.022886s ]
  13. SELECT * FROM `article` WHERE `id` < 473971 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.027898s ]
  14. SELECT * FROM `article` WHERE `id` < 473971 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.003522s ]
  15. SELECT * FROM `article` WHERE `id` < 473971 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.004935s ]
0.332015s