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}")