json库保存字典数据到本地

json库保存字典数据到本地

python 复制代码
import json

# 1. 准备配置数据
config = {
    "project": "中文命名实体识别",
    "version": "1.0.0",
    "author": "张三",
    "model_config": {
        "model_type": "bert",
        "pretrained_model": "bert-base-chinese",
        "hidden_size": 768,
        "num_labels": 5
    },
    "training_config": {
        "batch_size": 16,
        "learning_rate": 2e-5,
        "num_epochs": 10,
        "max_seq_length": 128
    },
    "data_config": {
        "train_file": "data/train.txt",
        "dev_file": "data/dev.txt",
        "label_list": ["O", "B-PER", "I-PER", "B-ORG", "I-ORG"]
    },
    "备注": "这是一个示例配置文件"
}

# 2. 保存到文件
config_file = "model_config.json"

try:
    with open(config_file, 'w', encoding='utf-8') as f:
        json.dump(config, f, ensure_ascii=False, indent=4)
    print(f"配置文件已保存到: {config_file}")
except IOError as e:
    print(f"文件写入失败: {e}")
except TypeError as e:
    print(f"JSON序列化失败: {e}")
相关推荐
NAGNIP1 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
moshuying1 天前
别让AI焦虑,偷走你本该有的底气
前端·人工智能
董董灿是个攻城狮1 天前
零基础带你用 AI 搞定命令行
人工智能
喝拿铁写前端1 天前
Dify 构建 FE 工作流:前端团队可复用 AI 工作流实战
前端·人工智能
阿里云大数据AI技术1 天前
阿里云 EMR Serverless Spark + DataWorks 技术实践:引领企业 Data+AI 一体化转型
人工智能
billhan20161 天前
MCP 深入理解:协议原理与自定义开发
人工智能
Jahzo1 天前
openclaw桌面端体验--ClawX
人工智能·github
billhan20161 天前
Agent 开发全流程:从概念到生产
人工智能
用户1474853079741 天前
AI-动手深度学习环境搭建-d2l
深度学习