python第31天打卡

python 复制代码
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers, optimizers, utils, datasets

# 数据加载和预处理函数
def load_and_preprocess_data():
    (x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
    # 重塑并归一化图像数据
    x_train = x_train.reshape(-1, 28, 28, 1).astype("float32") / 255.0
    x_test = x_test.reshape(-1, 28, 28, 1).astype("float32") / 255.0
    # 转换标签为one-hot编码
    y_train = utils.to_categorical(y_train, 10)
    y_test = utils.to_categorical(y_test, 10)
    return (x_train, y_train), (x_test, y_test)

# 模型定义
def create_simple_cnn():
    return keras.Sequential([
        layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(128, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])

def create_complex_cnn():
    return keras.Sequential([
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(256, activation='relu'),
        layers.Dense(128, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])

# 训练和评估函数
def train_and_evaluate(model, optimizer, x_train, y_train, x_test, y_test):
    model.compile(
        optimizer=optimizer,
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )
    history = model.fit(
        x_train, y_train,
        epochs=5,
        batch_size=64,
        validation_data=(x_test, y_test)
    )
    return history.history

# 主程序
if __name__ == "__main__":
    # 加载数据
    (x_train, y_train), (x_test, y_test) = load_and_preprocess_data()
    
    # 模型和优化器配置
    model_configs = [
        ('Simple CNN', create_simple_cnn),
        ('Complex CNN', create_complex_cnn)
    ]
    optimizers_config = {
        'SGD': optimizers.SGD(learning_rate=0.01),
        'Adam': optimizers.Adam(learning_rate=0.001)
    }
    
    # 训练和评估所有组合
    results = {}
    for model_name, model_fn in model_configs:
        for opt_name, optimizer in optimizers_config.items():
            print(f"\n{'='*50}")
            print(f"Training {model_name} with {opt_name} optimizer:")
            
            model = model_fn()
            history = train_and_evaluate(
                model, optimizer,
                x_train, y_train,
                x_test, y_test
            )
            
            # 记录结果
            results[f"{model_name}_{opt_name}"] = history
            print(f"\nTraining results for {model_name}/{opt_name}:")
            print(f"Final Training Accuracy: {history['accuracy'][-1]:.4f}")
            print(f"Final Validation Accuracy: {history['val_accuracy'][-1]:.4f}")
            print(f"Final Training Loss: {history['loss'][-1]:.4f}")
            print(f"Final Validation Loss: {history['val_loss'][-1]:.4f}")

@浙大疏锦行

相关推荐
予枫的编程笔记几秒前
【论文解读】DLF:以语言为核心的多模态情感分析新范式 (AAAI 2025)
人工智能·python·算法·机器学习
IOT-Power6 分钟前
QT 对话框(QDialog)中 accept、reject、exec、open的使用
开发语言·qt
froginwe118 分钟前
ASP Session
开发语言
lbb 小魔仙16 分钟前
【Python】零基础学 Python 爬虫:从原理到反爬,构建企业级爬虫系统
开发语言·爬虫·python
Swift社区18 分钟前
ArkTS Web 组件里,如何通过 javaScriptProxy 让 JS 同步调用原生方法
开发语言·前端·javascript
Q741_14718 分钟前
海致星图招聘 数据库内核研发实习生 一轮笔试 总结复盘(1) 作答语言:C/C++ 链表 二叉树
开发语言·c++·经验分享·面试·笔试
黄河里的小鲤鱼21 分钟前
拯救草台班子-战略
人工智能·python·信息可视化
秃了也弱了。23 分钟前
FASTJSON库:阿里出品java界json解析库,使用与踩坑记录
java·开发语言·json
_OP_CHEN23 分钟前
【从零开始的Qt开发指南】(十九)Qt 文件操作:从 I/O 设备到文件信息,一站式掌握跨平台文件处理
开发语言·c++·qt·前端开发·文件操作·gui开发·qt文件
Dr.Alex Wang25 分钟前
Google Firebase 实战教学 - Streamlit、Bucket、Firebase
数据库·python·安全·googlecloud