周志洋

个人站

持续学习 才能不被淘汰


Python机器学习-深度学习与TensorFlow详解

深度学习的重要性

深度学习是机器学习的一个重要分支,通过多层神经网络模拟人脑的学习过程,在图像识别、自然语言处理、语音识别等领域取得了突破性进展。TensorFlow作为Google开发的开源深度学习框架,提供了完整的深度学习工具链。掌握TensorFlow的使用对于理解和应用深度学习技术至关重要。

TensorFlow基础

1. 环境配置和基础概念

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

def tensorflow_basics_demo():
    """TensorFlow基础演示"""
    print("=== TensorFlow基础 ===")
    
    # 检查TensorFlow版本
    print(f"TensorFlow版本: {tf.__version__}")
    print(f"GPU可用: {tf.config.list_physical_devices('GPU')}")
    
    # 1. 张量基础操作
    print(f"\n1. 张量基础操作:")
    
    # 创建张量
    scalar = tf.constant(42)
    vector = tf.constant([1, 2, 3, 4, 5])
    matrix = tf.constant([[1, 2], [3, 4]])
    tensor = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
    
    print(f"   标量: {scalar}, 形状: {scalar.shape}")
    print(f"   向量: {vector}, 形状: {vector.shape}")
    print(f"   矩阵: {matrix}, 形状: {matrix.shape}")
    print(f"   张量: {tensor}, 形状: {tensor.shape}")
    
    # 张量运算
    a = tf.constant([[1, 2], [3, 4]])
    b = tf.constant([[5, 6], [7, 8]])
    
    print(f"\n   矩阵加法: {a + b}")
    print(f"   矩阵乘法: {tf.matmul(a, b)}")
    print(f"   元素乘法: {a * b}")
    
    # 2. 变量和张量操作
    print(f"\n2. 变量和张量操作:")
    
    # 创建变量
    var = tf.Variable([1.0, 2.0, 3.0])
    print(f"   变量: {var}")
    print(f"   变量值: {var.numpy()}")
    
    # 修改变量
    var.assign([4.0, 5.0, 6.0])
    print(f"   修改后: {var.numpy()}")
    
    # 3. 自动微分
    print(f"\n3. 自动微分:")
    
    x = tf.Variable(3.0)
    with tf.GradientTape() as tape:
        y = x * x + 2 * x + 1
    
    dy_dx = tape.gradient(y, x)
    print(f"   函数: y = x² + 2x + 1")
    print(f"   当x=3时, y={y.numpy()}, dy/dx={dy_dx.numpy()}")
    
    return scalar, vector, matrix, tensor

tensors = tensorflow_basics_demo()

2. 数据预处理

def data_preprocessing_demo():
    """数据预处理演示"""
    print("\n=== 数据预处理 ===")
    
    # 1. 加载和预处理数据
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    
    print(f"训练集形状: {x_train.shape}")
    print(f"测试集形状: {x_test.shape}")
    print(f"标签范围: {np.unique(y_train)}")
    
    # 数据归一化
    x_train = x_train.astype('float32') / 255.0
    x_test = x_test.astype('float32') / 255.0
    
    # 添加通道维度(用于卷积网络)
    x_train = x_train[..., tf.newaxis]
    x_test = x_test[..., tf.newaxis]
    
    print(f"预处理后训练集形状: {x_train.shape}")
    
    # 2. 数据增强
    print(f"\n2. 数据增强:")
    
    data_augmentation = tf.keras.Sequential([
        tf.keras.layers.RandomRotation(0.1),
        tf.keras.layers.RandomZoom(0.1),
        tf.keras.layers.RandomTranslation(0.1, 0.1)
    ])
    
    # 应用数据增强
    augmented_image = data_augmentation(x_train[0:1])
    print(f"   原始图像形状: {x_train[0:1].shape}")
    print(f"   增强后图像形状: {augmented_image.shape}")
    
    return x_train, y_train, x_test, y_test

data = data_preprocessing_demo()
x_train, y_train, x_test, y_test = data

神经网络构建

1. 基础神经网络

def basic_neural_network_demo():
    """基础神经网络演示"""
    print("\n=== 基础神经网络 ===")
    
    # 1. 使用Sequential API构建模型
    print(f"1. Sequential API模型:")
    
    model_sequential = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    print(f"   模型结构:")
    model_sequential.summary()
    
    # 编译模型
    model_sequential.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    # 2. 训练模型
    print(f"\n2. 训练模型:")
    
    history = model_sequential.fit(
        x_train, y_train,
        epochs=5,
        batch_size=32,
        validation_split=0.2,
        verbose=1
    )
    
    # 3. 评估模型
    print(f"\n3. 评估模型:")
    
    test_loss, test_accuracy = model_sequential.evaluate(x_test, y_test, verbose=0)
    print(f"   测试损失: {test_loss:.4f}")
    print(f"   测试准确率: {test_accuracy:.4f}")
    
    # 预测
    predictions = model_sequential.predict(x_test[:5])
    predicted_classes = np.argmax(predictions, axis=1)
    print(f"   前5个预测结果: {predicted_classes}")
    print(f"   实际标签: {y_test[:5]}")
    
    return model_sequential, history

model, history = basic_neural_network_demo()

卷积神经网络

1. CNN基础

def cnn_basic_demo():
    """卷积神经网络基础演示"""
    print("\n=== 卷积神经网络基础 ===")
    
    # 1. 基础CNN模型
    print(f"1. 基础CNN模型:")
    
    cnn_model = tf.keras.Sequential([
        # 第一个卷积块
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        tf.keras.layers.MaxPooling2D((2, 2)),
        
        # 第二个卷积块
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D((2, 2)),
        
        # 第三个卷积块
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        
        # 全连接层
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dropout(0.5),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    cnn_model.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy']
    )
    
    print(f"   CNN模型结构:")
    cnn_model.summary()
    
    # 2. 训练CNN模型
    print(f"\n2. 训练CNN模型:")
    
    cnn_history = cnn_model.fit(
        x_train, y_train,
        epochs=3,
        batch_size=32,
        validation_split=0.2,
        verbose=1
    )
    
    # 3. 评估CNN模型
    print(f"\n3. 评估CNN模型:")
    
    cnn_test_loss, cnn_test_accuracy = cnn_model.evaluate(x_test, y_test, verbose=0)
    print(f"   CNN测试准确率: {cnn_test_accuracy:.4f}")
    
    return cnn_model, cnn_history

cnn_model, cnn_history = cnn_basic_demo()

总结

深度学习与TensorFlow的关键要点:

  1. TensorFlow基础:张量操作、变量、自动微分、数据预处理
  2. 神经网络构建:Sequential API、模型编译、训练和评估
  3. 卷积神经网络:CNN基础、卷积层、池化层、全连接层
  4. 模型优化:数据增强、正则化、回调函数
  5. 实际应用:图像分类、模型保存和加载
  6. 最佳实践:模型设计、训练策略、性能评估

掌握这些深度学习技能,可以构建复杂的神经网络模型,解决各种机器学习和人工智能问题。


转载请注明:周志洋的博客 » Python机器学习-深度学习与TensorFlow详解

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦