-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchemprop_foundation.py
More file actions
210 lines (184 loc) · 7.75 KB
/
chemprop_foundation.py
File metadata and controls
210 lines (184 loc) · 7.75 KB
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
"""ChemProp Foundation Model Examples
This script demonstrates how to use pretrained foundation models (like CheMeleon)
with ChemProp in Workbench. Foundation models provide pretrained MPNN weights that
can significantly improve performance, especially on smaller datasets.
CheMeleon is a descriptor-based foundation model pretrained on 1M PubChem molecules
to predict Mordred molecular descriptors. This gives the MPNN a strong prior for
molecular representation learning.
References:
- CheMeleon: https://github.com/JacksonBurns/chemeleon
- Paper: https://arxiv.org/abs/2506.15792
"""
from workbench.api import FeatureSet, Model, ModelType, ModelFramework, Endpoint
# Recreate Flag in case you want to recreate the artifacts
recreate = False
# =============================================================================
# Single-Target CheMeleon Foundation Model
# =============================================================================
# Basic usage: Load CheMeleon pretrained weights and fine-tune on your dataset.
# The MPNN weights are initialized from CheMeleon, and a new FFN head is trained
# for your specific task.
if recreate or not Model("chemeleon-logd").exists():
feature_set = FeatureSet("open_admet_all")
m = feature_set.to_model(
name="chemeleon-logd",
model_type=ModelType.UQ_REGRESSOR,
model_framework=ModelFramework.CHEMPROP,
target_column="logd",
feature_list=["smiles"],
description="CheMeleon foundation model fine-tuned for LogD prediction",
tags=["chemprop", "chemeleon", "foundation", "open_admet"],
hyperparameters={
"from_foundation": "CheMeleon", # Load pretrained MPNN weights
"n_folds": 5,
"max_epochs": 100, # Fewer epochs needed with pretrained weights
},
)
m.set_owner("BW")
# Create an Endpoint
if recreate or not Endpoint("chemeleon-logd").exists():
m = Model("chemeleon-logd")
end = m.to_endpoint(tags=["chemprop", "chemeleon", "foundation", "open_admet"])
end.set_owner("BW")
end.auto_inference()
end.cross_fold_inference()
# =============================================================================
# Foundation Model with MPNN Freezing (Two-Phase Training)
# =============================================================================
# Advanced usage: Freeze the MPNN for initial epochs to stabilize the FFN,
# then unfreeze and fine-tune the entire model. This approach is recommended
# when you have limited data or want more stable training.
if recreate or not Model("chemeleon-logd-frozen").exists():
feature_set = FeatureSet("open_admet_all")
m = feature_set.to_model(
name="chemeleon-logd-frozen",
model_type=ModelType.UQ_REGRESSOR,
model_framework=ModelFramework.CHEMPROP,
target_column="logd",
feature_list=["smiles"],
description="CheMeleon with frozen MPNN warmup for stable fine-tuning",
tags=["chemprop", "chemeleon", "foundation", "frozen", "open_admet"],
hyperparameters={
"from_foundation": "CheMeleon",
"freeze_mpnn_epochs": 10, # Phase 1: Train FFN only for 10 epochs
"max_epochs": 100, # Total epochs (10 frozen + 90 fine-tuning)
"n_folds": 5,
},
)
m.set_owner("BW")
# Create an Endpoint
if recreate or not Endpoint("chemeleon-logd-frozen").exists():
m = Model("chemeleon-logd-frozen")
end = m.to_endpoint(tags=["chemprop", "chemeleon", "foundation", "frozen", "open_admet"])
end.set_owner("BW")
end.auto_inference()
end.cross_fold_inference()
# =============================================================================
# Multi-Task Foundation Model
# =============================================================================
# Foundation models work with multi-task learning too. This combines CheMeleon's
# pretrained representations with multi-task regression across 9 ADMET endpoints.
ADMET_TARGETS = [
"logd",
"ksol",
"hlm_clint",
"mlm_clint",
"caco_2_papp_a_b",
"caco_2_efflux",
"mppb",
"mbpb",
"mgmb",
]
if recreate or not Model("chemeleon-mt").exists():
feature_set = FeatureSet("open_admet_all")
m = feature_set.to_model(
name="chemeleon-mt",
model_type=ModelType.REGRESSOR,
model_framework=ModelFramework.CHEMPROP,
target_column=ADMET_TARGETS, # Multi-task: 9 ADMET endpoints
feature_list=["smiles"],
description="CheMeleon foundation model for multi-task ADMET prediction",
tags=["chemprop", "chemeleon", "foundation", "multitask", "open_admet"],
hyperparameters={
"from_foundation": "CheMeleon",
"freeze_mpnn_epochs": 10,
"max_epochs": 100,
"n_folds": 5,
},
)
m.set_owner("BW")
# Create an Endpoint
if recreate or not Endpoint("chemeleon-mt").exists():
m = Model("chemeleon-mt")
end = m.to_endpoint(tags=["chemprop", "chemeleon", "foundation", "multitask", "open_admet"])
end.set_owner("BW")
end.auto_inference()
end.cross_fold_inference()
# =============================================================================
# Hybrid Foundation Model (CheMeleon + Molecular Descriptors)
# =============================================================================
# Combine CheMeleon's pretrained MPNN with additional molecular descriptors.
# The extra features are concatenated with the MPNN output before the FFN,
# providing complementary information to the learned representations.
TOP_LOGD_SHAP_FEATURES = [
"mollogp",
"fr_halogen",
"peoe_vsa6",
"nbase",
"peoe_vsa7",
"peoe_vsa9",
"peoe_vsa1",
"mi",
"bcut2d_mrlow",
"slogp_vsa1",
]
if recreate or not Model("chemeleon-logd-hybrid").exists():
feature_set = FeatureSet("open_admet_logd")
# Hybrid mode: SMILES (for CheMeleon MPNN) + molecular descriptors
hybrid_features = ["smiles"] + TOP_LOGD_SHAP_FEATURES
m = feature_set.to_model(
name="chemeleon-logd-hybrid",
model_type=ModelType.UQ_REGRESSOR,
model_framework=ModelFramework.CHEMPROP,
target_column="logd",
feature_list=hybrid_features,
description="CheMeleon hybrid model with MPNN + top SHAP molecular descriptors",
tags=["chemprop", "chemeleon", "foundation", "hybrid", "open_admet"],
hyperparameters={
"from_foundation": "CheMeleon",
"freeze_mpnn_epochs": 10,
"max_epochs": 100,
"n_folds": 5,
},
)
m.set_owner("BW")
# Create an Endpoint
if recreate or not Endpoint("chemeleon-logd-hybrid").exists():
m = Model("chemeleon-logd-hybrid")
end = m.to_endpoint(tags=["chemprop", "chemeleon", "foundation", "hybrid", "open_admet"])
end.set_owner("BW")
end.auto_inference()
end.cross_fold_inference()
# =============================================================================
# Custom Foundation Model (Your Own Pretrained Weights)
# =============================================================================
# You can also use your own pretrained Chemprop model as a foundation.
# This is useful if you have domain-specific pretrained weights.
#
# Example (uncomment to use):
#
# if recreate or not Model("my-custom-foundation-model").exists():
# feature_set = FeatureSet("my_dataset")
# m = feature_set.to_model(
# name="my-custom-foundation-model",
# model_type=ModelType.UQ_REGRESSOR,
# model_framework=ModelFramework.CHEMPROP,
# target_column="my_target",
# feature_list=["smiles"],
# description="Fine-tuned from custom pretrained model",
# hyperparameters={
# "from_foundation": "/path/to/my_pretrained_model.pt", # Path to .pt file
# "freeze_mpnn_epochs": 5,
# "max_epochs": 50,
# },
# )