Mace训练
数据集
任何涉及机器学习的模型,第一步就是获取足够优质的数据集用于训练与测试。比如可以使用一个Host(符合条件的结构即可,比如说我的目的是Amor-SiO2-Si,那么我使用一个该结构就可以)结构,对于此结构做一些扰动,然后分别对这些扰动跑一小段小的分子动力学来获取大量结构;或者,直接对该Host结构使用MACE训练好的一些通用势跑大量的分子动力学;亦或者单纯的对Host结构做大量的扰动,这些方法都可以作为获取大量初始结构的办法。但是不建议跑长时间的AIMD,极其消耗资源,而且获得的结构丰富度也有限。
此处使用到的结构主要如下,还包括一些Si的结构,Amor-SiO2的结构这些都不是必要的,只是我顺便算了,本着不浪费的原则,一并使用了。
在获取到这些结构后,需要使用DFT软件(VASP、ABACUS、PWMAT、…)对其进行一次SCF计算,输出原子的受力以及结构的应力信息。
完成上述计算后,需要对这些数据进行处理,我们可以自己写代码处理,也可以使用模型自带的一些脚本,不同MLIP软件对数据集的格式要求可能不一样,需要看情况处理,但一般而言,可以统一转换成.(ext)xyz格式的文件,我用到的MLIP几乎都支持这个格式,即便遇到不支持的,处理起来也方便。
训练
在拿到上述数据后,比如data.xyz文件,就可以开始执行MACE的训练了(确保你安装了MACE),在文章末尾我给出了一个可用的train.py代码,供参考。或者也可以直接命令行参数提交,可以查看MACE官方的教程。
建议查看2026-07-08-MACE训练_2.md使用其中的方法进行训练
结果
这是最主要的输出部分,确保loss在稳定减少
1
2
3
4
5
6
7
8
9
...
2026-07-05 09:54:04.960 INFO: Epoch 187: head: Default, loss=0.00727528, RMSE_E_per_atom= 0.50 meV, RMSE_F= 27.23 meV / A
2026-07-05 09:57:50.697 INFO: Epoch 188: head: Default, loss=0.00728713, RMSE_E_per_atom= 0.51 meV, RMSE_F= 27.25 meV / A
2026-07-05 10:01:37.270 INFO: Epoch 189: head: Default, loss=0.00725387, RMSE_E_per_atom= 0.51 meV, RMSE_F= 27.18 meV / A
2026-07-05 10:05:24.289 INFO: Epoch 190: head: Default, loss=0.00726462, RMSE_E_per_atom= 0.50 meV, RMSE_F= 27.21 meV / A
2026-07-05 10:09:10.491 INFO: Epoch 191: head: Default, loss=0.00727438, RMSE_E_per_atom= 0.51 meV, RMSE_F= 27.23 meV / A
2026-07-05 10:12:57.668 INFO: Epoch 192: head: Default, loss=0.00725913, RMSE_E_per_atom= 0.60 meV, RMSE_F= 27.18 meV / A
2026-07-05 10:16:43.737 INFO: Epoch 193: head: Default, loss=0.00732247, RMSE_E_per_atom= 0.97 meV, RMSE_F= 27.20 meV / A
...
模型在训练集和验证集上的最终表现,同时也会有测试集的信息,但是太长了此处不给出
1
2
3
4
5
6
7
2026-07-05 10:41:05.005 INFO: Error-table on TRAIN and VALID:
+---------------+---------------------+------------------+-------------------+
| config_type | RMSE E / meV / atom | RMSE F / meV / A | relative F RMSE % |
+---------------+---------------------+------------------+-------------------+
| train_Default | 3.7 | 23.5 | 2.06 |
| valid_Default | 3.7 | 28.2 | 2.47 |
+---------------+---------------------+------------------+-------------------+
流程
总之,整个流程比较困难的地方只在第一步,我们需要使用合适的方法获取到足够丰富的数据集,然后对这些结构批量进行SCF计算,最后获取受力信息。这些工作一旦完成,剩下的就是交给MLIP模型自己训练即可。当然为了获取更好的模型,还可以对模型的参数进行调整,但一般而言,默认参数基本是足够的。
附 train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# train.py
"""
MACE 模型训练脚本
==================
基于 T01_MACE_Practice_I.ipynb 教程实现。
读取 .xyz 数据文件,训练 MACE 势函数模型。
用法: 直接修改下方 CONFIG,然后运行
python train.py
"""
import os
import sys
import logging
import warnings
import re
import yaml
import numpy as np
from ase.io import read
from mace.cli.run_train import main as mace_run_train_main
warnings.filterwarnings("ignore")
# ===========================================================================
# 配置区 (修改这里即可)
# ===========================================================================
CONFIG = {
# ----- 数据 -----
# 方式一: 直接指定已拆分好的文件
"train_file": None, # 训练集路径 (split_source 不为空时自动生成)
"test_file": None, # 测试集路径 (split_source 不为空时自动生成)
"valid_fraction": 0.0, # 从训练集切分验证集的比例 (有 valid_file 时设为 0)
# 方式二: 从单个文件自动拆分 (推荐)
"split_source": "mace.xyz", # 原始 .xyz 文件, 设为 None 则使用上面的 train_file
"split_ratios": [0.8, 0.1, 0.1], # [训练, 验证, 测试] 比例
"split_seed": 42, # 拆分随机种子
"split_output_dir": "splitted_data", # 拆分后文件的输出目录
"energy_key": "energy", # 能量字段名
"forces_key": "forces", # 力字段名
"E0s": "average", # 原子能量: "average" 或 "isolated"
# ----- 模型架构 -----
"num_channels": 128, # 通道数 (64快速, 128默认, 256高精度)
"max_L": 1, # 消息对称性 (0最快, 1推荐, 2最精确)
"r_max": 5.0, # 截断半径 Å (推荐 4.0~7.0)
"max_ell": 3, # 球谐函数最大阶数
"correlation": 3, # 多体展开阶数
"num_interactions": 2, # 消息传递层数 (保持2)
"num_radial_basis": 8, # 径向基函数数量
# ----- 训练控制 -----
"name": "mace_model", # 模型名称
"output_dir": "MACE_models", # 输出目录
"device": "cuda", # 设备: "auto" / "cuda" / "cpu" / "mps"
"batch_size": 2, # 批大小
"max_num_epochs": 200, # 最大训练轮数
"seed": 42, # 随机种子
"start_swa": 100, # SWA 阶段起始 epoch
"patience": 2048, # 早停耐心
"distributed":True,
# ----- 损失权重 -----
"lr": 0.01, # 初始学习率
"swa_lr": 0.001, # SWA 阶段学习率
"energy_weight": 1.0, # 能量损失权重
"forces_weight": 100.0, # 力损失权重
"swa_energy_weight": 1000.0, # SWA 阶段能量权重
}
# ===========================================================================
# 以下无需修改
# ===========================================================================
def determine_device(device_arg):
if device_arg != "auto":
return device_arg
try:
import torch
if torch.cuda.is_available():
return "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
else:
return "cpu"
except ImportError:
return "cpu"
def create_yaml_config(cfg):
"""生成 MACE 训练 YAML 配置"""
config = {
"model": "MACE",
"num_channels": cfg["num_channels"],
"max_L": cfg["max_L"],
"r_max": cfg["r_max"],
"max_ell": cfg["max_ell"],
"correlation": cfg["correlation"],
"num_interactions": cfg["num_interactions"],
"num_radial_basis": cfg["num_radial_basis"],
"name": cfg["name"],
"model_dir": cfg["output_dir"],
"log_dir": cfg["output_dir"],
"checkpoints_dir": cfg["output_dir"],
"results_dir": cfg["output_dir"],
"train_file": os.path.abspath(cfg["train_file"]),
"valid_fraction": cfg["valid_fraction"],
"energy_key": cfg["energy_key"],
"forces_key": cfg["forces_key"],
"E0s": cfg["E0s"],
"device": determine_device(cfg["device"]),
"batch_size": cfg["batch_size"],
"max_num_epochs": cfg["max_num_epochs"],
"seed": cfg["seed"],
"lr": cfg["lr"],
"swa_lr": cfg["swa_lr"],
"start_swa": cfg["start_swa"],
"patience": cfg["patience"],
"forces_weight": cfg["forces_weight"],
"energy_weight": cfg["energy_weight"],
"swa_energy_weight": cfg["swa_energy_weight"],
"swa": True,
"ema": True,
"ema_decay": 0.99,
"optimizer": "adam",
"weight_decay": 5e-7,
"amsgrad": True,
"scheduler": "ReduceLROnPlateau",
"lr_factor": 0.8,
"scheduler_patience": 50,
"clip_grad": 10.0,
}
if cfg.get("test_file"):
config["test_file"] = os.path.abspath(cfg["test_file"])
if cfg.get("valid_file"):
config["valid_file"] = os.path.abspath(cfg["valid_file"])
config["valid_fraction"] = 0.0
return config
def analyze_xyz(file_path):
"""分析 .xyz 文件的基本信息"""
data = read(file_path, ":")
with open(file_path, "r") as f:
lines = f.readlines()
print(f"\n{'='*50}")
print(f" 数据文件分析: {file_path}")
print(f"{'='*50}")
# 原始注释头
n1 = int(lines[0].strip())
print(f"结构1 注释头: {lines[1].strip()}")
if len(lines) > n1 + 3:
n2 = int(lines[n1 + 2].strip())
if len(lines) > n1 + n2 + 3:
print(f"结构2 注释头: {lines[n1 + 3].strip()}")
print(f"总结构数: {len(data)}")
all_species = set()
for atoms in data:
all_species.update(atoms.get_chemical_symbols())
print(f"原子种类: {sorted(all_species)}")
n_atoms_list = [len(atoms) for atoms in data]
print(f"原子数范围: {min(n_atoms_list)} ~ {max(n_atoms_list)}")
# ASE 解析结果
sample = data[0]
print(f"\nASE info.keys(): {list(sample.info.keys())}")
print(f"ASE arrays.keys(): {list(sample.arrays.keys())}")
# 检查 calc
all_calc_keys = set()
for atoms in data[:5]:
if hasattr(atoms, "calc") and atoms.calc is not None:
try:
if atoms.get_potential_energy() is not None:
all_calc_keys.add("energy (from calc)")
except Exception:
pass
try:
if atoms.get_forces() is not None:
all_calc_keys.add("forces (from calc)")
except Exception:
pass
if all_calc_keys:
print(f"ASE calc: {sorted(all_calc_keys)}")
# 从原始文件检测字段
print(f"\n--- 从原始文件检测 ---")
column_keys = set()
header_keys = set()
line_idx = 0
struct_count = 0
while line_idx < len(lines) and struct_count < 5:
try:
n_atoms = int(lines[line_idx].strip())
except ValueError:
line_idx += 1
continue
comment = lines[line_idx + 1]
props_match = re.search(r'Properties=([^\s]+)', comment)
if props_match:
for part in props_match.group(1).split(":"):
if ":" in part:
name = part.split(":")[0]
if name not in ("species", "pos"):
column_keys.add(name)
for match in re.finditer(r'(\w+)=(?:"[^"]*"|[^\s"]+)', comment):
k = match.group(1)
if k not in ("Properties", "Lattice"):
header_keys.add(k)
line_idx += n_atoms + 2
struct_count += 1
print(f"数据列: {sorted(column_keys)}")
print(f"key=value: {sorted(header_keys)}")
print(f"{'='*50}\n")
return data
def split_dataset(cfg):
"""
从单个 .xyz 文件拆分为训练/验证/测试集。
- 自动识别 config_type=IsolatedAtom 的结构,始终放入训练集。
- 其余结构按 cfg["split_ratios"] 随机打乱拆分。
- 输出 train.xyz, valid.xyz, test.xyz 到 split_output_dir。
- 返回更新后的 train_file, test_file,以及 valid_file 路径。
"""
source = cfg["split_source"]
ratios = cfg["split_ratios"]
output_dir = cfg["split_output_dir"]
if not os.path.exists(source):
print(f"[ERROR] split_source 文件不存在: {source}")
sys.exit(1)
print(f"\n{'='*50}")
print(f" 拆分数据集: {source}")
print(f"{'='*50}")
data = read(source, ":")
total = len(data)
# 分离 IsolatedAtom 和普通结构
isolated = []
normal = []
for atoms in data:
ct = atoms.info.get("config_type", "")
if ct == "IsolatedAtom":
isolated.append(atoms)
else:
normal.append(atoms)
print(f"总结构数: {total}")
print(f" IsolatedAtom: {len(isolated)}")
print(f" 普通结构: {len(normal)}")
if len(normal) < 3:
print("[ERROR] 普通结构数量不足,无法拆分 (至少需要 3 个)")
sys.exit(1)
# 随机打乱普通结构
import random
rng = random.Random(cfg["split_seed"])
rng.shuffle(normal)
# 按比例拆分
r_train, r_val, r_test = ratios
n_total = len(normal)
n_train = int(n_total * r_train)
n_val = int(n_total * r_val)
# 剩余归测试集,避免四舍五入丢失
n_test = n_total - n_train - n_val
train_data = normal[:n_train] + isolated # IsolatedAtom 始终加入训练集
valid_data = normal[n_train:n_train + n_val]
test_data = normal[n_train + n_val:]
print(f"拆分比例: {ratios}")
print(f" 训练集 (含IsolatedAtom): {len(train_data)}")
print(f" 验证集: {len(valid_data)}")
print(f" 测试集: {len(test_data)}")
# 写入文件
os.makedirs(output_dir, exist_ok=True)
train_path = os.path.join(output_dir, "train.xyz")
valid_path = os.path.join(output_dir, "valid.xyz")
test_path = os.path.join(output_dir, "test.xyz")
from ase.io import write
write(train_path, train_data)
write(valid_path, valid_data)
write(test_path, test_data)
print(f"\n已写入:")
print(f" {os.path.abspath(train_path)}")
print(f" {os.path.abspath(valid_path)}")
print(f" {os.path.abspath(test_path)}")
print(f"{'='*50}\n")
return train_path, valid_path, test_path
def check_keys(cfg, sample):
"""检查 energy/forces key 是否可用"""
energy_ok = (cfg["energy_key"] in sample.info or
cfg["energy_key"] in sample.arrays)
if not energy_ok:
try:
if sample.get_potential_energy() is not None:
energy_ok = True
except Exception:
pass
forces_ok = (cfg["forces_key"] in sample.info or
cfg["forces_key"] in sample.arrays)
if not forces_ok:
try:
if sample.get_forces() is not None:
forces_ok = True
except Exception:
pass
if not energy_ok:
print(f"[ERROR] energy_key='{cfg['energy_key']}' 未找到!")
print(f" 请在 CONFIG 中修改 energy_key")
return False
if not forces_ok:
print(f"[ERROR] forces_key='{cfg['forces_key']}' 未找到!")
print(f" 请在 CONFIG 中修改 forces_key")
return False
return True
def main():
cfg = CONFIG
# 0. 如果配置了 split_source,先拆分数据
if cfg.get("split_source"):
train_file, valid_file, test_file = split_dataset(cfg)
cfg = dict(cfg) # 复制一份,避免修改全局 CONFIG
cfg["train_file"] = train_file
cfg["test_file"] = test_file
cfg["valid_file"] = valid_file
cfg["valid_fraction"] = 0.0 # 已有独立验证集
else:
cfg["valid_file"] = None
# 1. 检查文件
if not os.path.exists(cfg["train_file"]):
print(f"[ERROR] 训练文件不存在: {cfg['train_file']}")
sys.exit(1)
# 2. 分析数据
data = analyze_xyz(cfg["train_file"])
# 3. 检查 key
if not check_keys(cfg, data[0]):
sys.exit(1)
# 4. 创建输出目录
os.makedirs(cfg["output_dir"], exist_ok=True)
# 5. 生成 YAML 并保存
yaml_config = create_yaml_config(cfg)
config_path = os.path.join(cfg["output_dir"], f"{cfg['name']}_config.yml")
with open(config_path, "w") as f:
yaml.dump(yaml_config, f, default_flow_style=False)
print(f"[INFO] 配置文件: {config_path}")
# 6. 打印摘要
print(f"\n{'='*50}")
print(f" 训练配置")
print(f"{'='*50}")
print(f"模型: {cfg['name']}")
print(f"设备: {yaml_config['device']}")
print(f"训练文件: {cfg['train_file']}")
val_info = cfg.get("valid_file")
if not val_info:
val_info = f"{cfg['valid_fraction'] * 100:.0f}% from train"
print(f"验证文件: {val_info}")
print(f"测试文件: {cfg['test_file'] or '无'}")
print(f"通道数: {cfg['num_channels']} | max_L: {cfg['max_L']} | r_max: {cfg['r_max']}")
print(f"epochs: {cfg['max_num_epochs']} | batch: {cfg['batch_size']}")
print(f"energy_key: {cfg['energy_key']} | forces_key: {cfg['forces_key']}")
print(f"输出目录: {os.path.abspath(cfg['output_dir'])}")
print(f"{'='*50}\n")
# 7. 训练
print("[INFO] 开始训练...\n")
logging.getLogger().handlers.clear()
sys.argv = ["program", "--config", config_path]
mace_run_train_main()
print(f"\n[INFO] 完成! 模型保存在: {os.path.abspath(cfg['output_dir'])}/")
if __name__ == "__main__":
main()


