Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cache5MolFormer Finetuning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Max Rausch-Dupont
Cache5MolFormer Finetuning
Commits
e88e02cf
Commit
e88e02cf
authored
10 months ago
by
Max Rausch Dupont
Browse files
Options
Downloads
Patches
Plain Diff
Add training script
parent
75e467e5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
run.sh
+1
-1
1 addition, 1 deletion
run.sh
train.py
+153
-0
153 additions, 0 deletions
train.py
with
154 additions
and
1 deletion
run.sh
+
1
−
1
View file @
e88e02cf
source
$PROJECT_ROOT
/setup.sh
${
CONDA
}
run
-n
${
ENV_NAME
}
python
$PROJECT_ROOT
/
run_molformer
.py
${
CONDA
}
run
-n
${
ENV_NAME
}
python
$PROJECT_ROOT
/
train
.py
This diff is collapsed.
Click to expand it.
train.py
0 → 100644
+
153
−
0
View file @
e88e02cf
import
os
import
pathlib
from
dataclasses
import
dataclass
,
field
from
typing
import
Literal
import
evaluate
import
numpy
as
np
import
wandb
from
datasets
import
load_dataset
from
transformers
import
(
AutoTokenizer
,
AutoModelForSequenceClassification
,
HfArgumentParser
,
TrainingArguments
,
Trainer
,
)
from
wandb.util
import
generate_id
@dataclass
class
ModelArguments
:
train_method
:
Literal
[
"
classification
"
,
"
regression
"
]
=
field
(
default
=
"
regression
"
,
metadata
=
{
"
help
"
:
"
Method of training. Either
'
classification
'
or
'
regression
'
.
"
},
)
@dataclass
class
DataTrainingArguments
:
fold_column
:
Literal
[
"
DataSAIL
"
,
"
random
"
]
=
field
(
default
=
"
DataSAIL
"
,
metadata
=
{
"
help
"
:
"
Which splitting method to use to pick the cross-validation folds.
"
"
Either
'
DataSAIL
'
or
'
random
'
.
"
},
)
file
:
str
=
field
(
default
=
"
20240430_MCHR1_splitted_RJ.csv
"
,
metadata
=
{
"
help
"
:
"
Name of the data file.
"
},
)
def
main
():
parser
=
HfArgumentParser
((
ModelArguments
,
DataTrainingArguments
))
model_args
,
data_args
=
parser
.
parse_args_into_dataclasses
()
root_dir
=
pathlib
.
Path
(
__file__
).
parent
model_name
=
"
ibm/MoLFormer-XL-both-10pct
"
model
=
AutoModelForSequenceClassification
.
from_pretrained
(
model_name
,
trust_remote_code
=
True
,
num_labels
=
2
if
model_args
.
train_method
==
"
classification
"
else
1
,
device_map
=
"
auto
"
,
)
tokenizer
=
AutoTokenizer
.
from_pretrained
(
model_name
,
trust_remote_code
=
True
)
tokenizer_config
=
{
"
padding
"
:
"
max_length
"
,
"
truncation
"
:
True
,
"
max_length
"
:
tokenizer
.
model_max_length
,
"
return_tensors
"
:
"
pt
"
,
}
def
tokenize_input
(
examples
):
return
tokenizer
(
examples
[
"
text
"
],
**
tokenizer_config
)
# Data Preparation
ds
=
load_dataset
(
"
csv
"
,
data_files
=
str
(
root_dir
/
data_args
.
file
),
split
=
"
train
"
,
# Creates this automatically
)
ds
=
ds
.
rename_column
(
"
smiles
"
,
"
text
"
)
ds
=
ds
.
rename_column
(
"
class
"
if
model_args
.
train_method
==
"
classification
"
else
"
acvalue_uM
"
,
"
label
"
,
)
train
=
ds
.
filter
(
lambda
x
:
x
[
f
"
{
data_args
.
fold_column
}
_10f
"
]
not
in
(
"
Fold_8
"
,
"
Fold_9
"
)
)
# val = ds.filter(lambda x: x[f"{data_args.fold_column}_10f"] == "Fold_8")
# test = ds.filter(lambda x: x[f"{data_args.fold_column}_10f"] == "Fold_9")
train_tokenized
=
train
.
map
(
tokenize_input
,
batched
=
True
)
# val_tokenized = val.map(tokenize_input, batched=True)
# test_tokenized = test.map(tokenize_input, batched=True)
# Training
classification_metrics
=
[
"
roc_auc
"
,
"
precision
"
,
"
recall
"
,
"
f1
"
]
regression_metrics
=
[
"
mse
"
,
"
r_squared
"
,
"
pearsonr
"
]
def
compute_metrics
(
model_output
):
logits
,
labels
=
model_output
if
model_args
.
train_method
==
"
classification
"
:
predictions
=
np
.
argmax
(
logits
,
axis
=-
1
)
metrics
=
classification_metrics
else
:
predictions
=
logits
# Regression values
metrics
=
regression_metrics
output
=
{}
for
metric
in
metrics
:
metric_func
=
evaluate
.
load
(
metric
)
result
=
metric_func
.
compute
(
predictions
=
predictions
,
references
=
labels
)
if
isinstance
(
result
,
dict
):
output
[
metric
]
=
list
(
result
.
values
()).
pop
()
else
:
output
[
metric
]
=
result
return
output
os
.
environ
[
"
WANDB_PROJECT
"
]
=
"
cache5_molformer_finetune
"
os
.
environ
[
"
WANDB_LOG_MODEL
"
]
=
"
checkpoint
"
os
.
environ
[
"
WANDB_TAGS
"
]
=
f
"
{
model_args
.
train_method
}
,
{
data_args
.
fold_column
}
"
wandb_prefix
=
generate_id
()
K
=
8
for
i
in
range
(
K
):
cv_test_fold
=
train_tokenized
.
filter
(
lambda
x
:
x
[
f
"
{
data_args
.
fold_column
}
_10f
"
]
==
f
"
Fold_
{
i
}
"
)
cv_train_fold
=
train_tokenized
.
filter
(
lambda
x
:
x
[
f
"
{
data_args
.
fold_column
}
_10f
"
]
!=
f
"
Fold_
{
i
}
"
)
train_args
=
TrainingArguments
(
output_dir
=
os
.
getenv
(
"
CHECKPOINT_DIR
"
,
root_dir
),
evaluation_strategy
=
"
epoch
"
,
logging_strategy
=
"
epoch
"
,
do_train
=
True
,
do_eval
=
True
,
num_train_epochs
=
10
,
report_to
=
"
wandb
"
,
save_total_limit
=
2
,
run_name
=
f
"
{
wandb_prefix
}
_
{
i
}
"
,
)
trainer
=
Trainer
(
model
=
model
,
args
=
train_args
,
train_dataset
=
cv_test_fold
,
eval_dataset
=
cv_train_fold
,
compute_metrics
=
compute_metrics
,
)
trainer
.
train
()
wandb
.
finish
()
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment