Instructions to use tdc/Geneformer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tdc/Geneformer with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="tdc/Geneformer")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("tdc/Geneformer") model = AutoModelForMaskedLM.from_pretrained("tdc/Geneformer", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download geneformer/mtl/optuna_utils.py from tdc/Geneformer: direct link, hf CLI and curl.
- Browser
- Download file 850 Bytes
-
https://huggingface.co/tdc/Geneformer/resolve/1b531a57070efd45ef2d73f0e399f5ab40e5a3ea/geneformer/mtl/optuna_utils.py
- Command line
-
hf download hf://tdc/Geneformer@1b531a57070efd45ef2d73f0e399f5ab40e5a3ea/geneformer/mtl/optuna_utils.py
-
curl -L -o optuna_utils.py https://huggingface.co/tdc/Geneformer/resolve/1b531a57070efd45ef2d73f0e399f5ab40e5a3ea/geneformer/mtl/optuna_utils.py
850 Bytes
| import optuna | |
| from optuna.integration import TensorBoardCallback | |
| def save_trial_callback(study, trial, trials_result_path): | |
| with open(trials_result_path, "a") as f: | |
| f.write( | |
| f"Trial {trial.number}: Value (F1 Macro): {trial.value}, Params: {trial.params}\n" | |
| ) | |
| def create_optuna_study(objective, n_trials, trials_result_path, tensorboard_log_dir): | |
| study = optuna.create_study(direction="maximize") | |
| # init TensorBoard callback | |
| tensorboard_callback = TensorBoardCallback( | |
| dirname=tensorboard_log_dir, metric_name="F1 Macro" | |
| ) | |
| # callback and TensorBoard callback | |
| callbacks = [ | |
| lambda study, trial: save_trial_callback(study, trial, trials_result_path), | |
| tensorboard_callback, | |
| ] | |
| study.optimize(objective, n_trials=n_trials, callbacks=callbacks) | |
| return study | |