clearml/examples/reporting/configuration_including_hyperparameters.py

121 lines
3.7 KiB
Python
Raw Normal View History

# ClearML - example code for logging into "CONFIGURATION":
# - ArgumentParser parameter logging
# - user properties logging
# - logging of hyperparameters via dictionary
# - logging of hyperparameters via TaskParameters
# - logging of configuration objects via TaskParameters
2019-06-10 17:00:28 +00:00
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
2019-11-27 22:47:51 +00:00
from argparse import ArgumentParser
from enum import Enum
2019-06-10 17:00:28 +00:00
2020-12-22 21:25:37 +00:00
from clearml import Task
from clearml.task_parameters import TaskParameters, param, percent_param
2019-06-10 17:00:28 +00:00
# Connecting ClearML with the current process,
# from here on everything is logged automatically
task = Task.init(project_name='FirstTrial', task_name='first_trial')
# -----------------------------------------------
# Report user properties
# -----------------------------------------------
task.set_user_properties(custom1='great', custom2=True)
task.set_user_properties(custom3=1, custom4=2.0)
# -----------------------------------------------
# Report hyperparameters via dictionary
# -----------------------------------------------
class StringEnumClass(Enum):
A = 'a'
B = 'b'
class IntEnumClass(Enum):
C = 1
D = 2
2019-06-10 17:00:28 +00:00
parameters = {
'list': [1, 2, 3],
'dict': {'a': 1, 'b': 2},
'tuple': (1, 2, 3),
2019-06-10 17:00:28 +00:00
'int': 3,
'float': 2.2,
'string': 'my string',
'IntEnumParam': StringEnumClass.A,
'StringEnumParam': IntEnumClass.C
2019-06-10 17:00:28 +00:00
}
2020-12-23 22:30:32 +00:00
parameters = task.connect(parameters)
2019-06-10 17:00:28 +00:00
# adding new parameter after connect (will be logged as well)
parameters['new_param'] = 'this is new'
# changing the value of a parameter (new value will be stored instead of previous one)
parameters['float'] = '9.9'
print(parameters)
2019-06-10 17:00:28 +00:00
# -----------------------------------------------
# Report hyperparameters via TaskParameters
# -----------------------------------------------
# Define a class that inherits from TaskParameters.
# Note that TaskParameters inherits from _AttrsMeta;
# because of `iterations` and `target_accuracy` do not need to be explicitly populated in an __init__ method.
# Consult the documentation at https://www.attrs.org for more information.
class MyTaskParameters(TaskParameters):
iterations = param(
type=int,
desc="Number of iterations to run",
range=(0, 100000),
)
target_accuracy = percent_param(
desc="The target accuracy of the model",
)
my_task_parameters = MyTaskParameters(iterations=1000, target_accuracy=0.95)
my_task_parameters = task.connect(my_task_parameters, name='from TaskParameters-like object')
# -----------------------------------------------
# Report configuration objects via dictionary
# -----------------------------------------------
2020-12-22 21:25:37 +00:00
complex_nested_dict_configuration = {
'list_of_dicts': [{'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6}],
'nested_dicts': {'nested': {'key': 'value', 'extra': 'value'}, 'number': 42},
'dict': {'simple': 'value', 'number': 2},
'list': [1, 2, 3],
'int': 3,
'float': 2.2,
'string': 'additional string',
}
complex_nested_dict_configuration = task.connect_configuration(
complex_nested_dict_configuration, name='configuration dictionary')
print(complex_nested_dict_configuration)
2019-11-27 22:47:51 +00:00
2019-06-10 17:00:28 +00:00
if __name__ == '__main__':
2019-11-27 22:47:51 +00:00
parser = ArgumentParser()
parser.add_argument('--argparser_int_value', help='integer value', type=int, default=1)
parser.add_argument('--argparser_disabled', action='store_true', default=False, help='disables something')
parser.add_argument('--argparser_str_value', help='string value', default='a string')
args = parser.parse_args()
2021-01-18 18:23:19 +00:00
print('Running under Python {0[0]}.{0[1]}.{0[2]}'.format(sys.version_info), file=sys.stderr)
task_params = task.get_parameters()
print("Task parameters are: {}".format(task_params))