Skip to content

Log code¶

Comet's code logging allows you to log the code behind your Experiment.

Unless explicitly disabled, after an Experiment is created, Comet automatically logs:

  1. The file the the Experiment was created in.
  2. Relevant git information, including the git commit and the git path (i.e., uncommitted files) if they exist.

You can also log additional code from file, folder, or file-like string.

Example of Code tab
View logged code for Comet Tutorial 2

Comet allows you to search through and access code files from your Single Experiment page as well as easily compare the code used for different training runs using the experiment comparison functionality.

The git information is displayed when you want to Reproduce an Experiment.

The following method can be used to code:

  • log_code(): Can be used to log both files and folders.

The code logged to an Experiment can be viewed in the following Single Experiment Tabs:

For example, you could...

Review the exact logic used for data preprocessing to ensure consistency in feature engineering across different model iterations.

Log a code file¶

The example below logs a single .py file to Comet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import comet_ml

# Initialize the Comet Experiment
comet_ml.login()
exp = comet_ml.start()

# Create an example .py file
with open("/tmp/test-file.py", "w+") as f:
    f.write('print("hello world")')

# Log the file to Comet
exp.log_code(file_name="/tmp/test-file.py")

To log a single file, you use the file_name argument. To log a folder or a file-like object, you'd use respectively the folder argument and the code argument with associated code_name.

Additionally, you could use the overwrite argument to overwrite any existing asset with the same name.

Dec. 17, 2024