一个轻量级的python包,用于记录机器学习实验
hypernotes的Python项目详细描述
超音符
hypernotes是一个轻量级的python包,用于记录机器学习实验。它提供了一种简单的方法来存储超参数及其相应的评估指标,以及附加信息,并在以后再次检索它们进行分析。它是用纯python编写的,不需要额外的依赖项。
目录
安装
pip install hypernotes
python 3.6+是必需的
基本用法
hypernotes实现了一个note和一个store类。note是python字典的一个小包装。这意味着你可以用它做任何事,也可以用普通字典做任何事,但除此之外,它还存储:
- python可执行文件的路径,
- 有关git存储库(如果有)当前状态的信息,如最后一次提交、当前分支等。
- 开始(初始化时)和结束日期时间(调用note.end()或add to store)
它提供:
- 一个有用的默认字典结构
- 访问所有初始字典键作为属性,以获得更好的自动完成支持和可读性(例如
note.parameters
,note.features
)
如果你打印一张纸条,你就能看到里面是什么。初始化后的注释如下:
Note(content={'text':'','model':None,'parameters':{},'features':{'identifier':[],'binary':[],'categorical':[],'numerical':[]},'target':None,'metrics':{},'info':{},'start_datetime':datetime.datetime(2019,5,21,11,3,20),'end_datetime':None,'identifier':'3228fe02-d1c8-4251-8b35-bb8ae3d5f227','python_path':'C:/example_path/python.exe','git':{'repo_name':'C:/path_to_your_repo','branch':'master','commit':'6bbdf31'}}
然后,这些注释与使用json文件的store实例一起保存。因此,只应将json-serializable objects+datetime.datetime实例添加到note中。
注释通过其identifier
属性唯一标识。
创建便笺并添加到存储区
fromhypernotesimportNote,Storenote=Note("Some descriptive text about your experiment")# Add name of used algorithmnote.model="randomforest"# Add hyperparameters about model training, preprocessing, etc.note.parameters["num_estimators"]=100note.parameters["impute_missings"]=True# Add the names of the features and of the target variablenote.features["identifier"]=["id"]note.features["binary"]=["bool1"]note.features["categorical"]=["cat1","cat2"]note.features["numerical"]=["num1"]note.target="target"# Some additional informationnote.info["important_stuff"]="something noteworthy"# ... Rest of your code ...# train_recall, train_precision test_recall, test_precision = train_and_evaluate_model(# parameters=note.params,# feature_names=note.features,# target_name=note.target)# ...# Add your calculated evaluation metricsnote.metrics["train"]={"recall":train_recall,"precision":train_precision}note.metrics["test"]={"recall":test_recall,"precision":test_precision}store=Store("hyperstore.json")store.add(note)
加载注释
存储实例提供load
方法,该方法可用于检索整个存储。默认情况下,它返回一个排序的列表(首先是最近的注释)。
notes=store.load()most_recent_note=notes[0]
如果安装了pandas,则可以使用return_dataframe
参数返回pandas数据帧。
notes_df=store.load(return_dataframe=True)notes_df.head()
返回的pandas数据帧示例:
start_datetime | end_datetime | text | model | identifier | metrics.test.precision | metrics.test.recall | metrics.train.precision | metrics.train.recall | parameters.min_sample_split | parameters.num_estimators | parameters.sample_weight | features.binary | features.categorical | features.identifier | features.numerical | target | git.branch | git.commit | git.repo_name | info.important_stuff | python_path | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2019-05-21 16:44:48 | 2019-05-21 17:05:21 | Another useful description | randomforest | 0f84217d-e01b-466d-9a73-001827c60584 | 0.29 | 0.29 | 0.40 | 0.50 | 7 | 150 | None | [bool1] | [cat1, cat2] | [id] | [num1] | target | master | 5e098ab | C:/path_to_your_repo | something noteworthy | C:/example_path/python.exe |
1 | 2019-05-21 16:12:53 | 2019-05-21 16:30:16 | Useful description | randomforest | dd8bbc32-ff8f-433d-9eec-a24a7859622f | 0.82 | 0.29 | 0.91 | 0.98 | 7 | 100 | balanced | [bool1] | [cat1, cat2] | [id] | [num1] | target | master | 5e098ab | C:/path_to_your_repo | something noteworthy | C:/example_path/python.exe |
更新注释
如果要更新notes,可以直接在包含notes的json文件中执行此操作,也可以如上所述加载notes,更改相关的notes,并将其传递给update
方法。
notes=store.load()updated_notes=[]fornoteinnotes[:2]:note.info["something_new"]="..."updated_notes.append(note)store.update(updated_notes)
删除注释
如果要删除注释,可以直接在包含注释的json文件中执行此操作,也可以如上所述加载注释,并将要删除的注释传递给remove
方法。
notes=store.load()notes_to_remove=notes[:2]store.remove(notes_to_remove)
从另一个注释中创建注释
当评估多个模型参数(例如,在网格搜索设置中)时,您可能会发现为每个参数集创建新注释很有用。要做到这一点,可以使用^ {CD8}}方法从现有的一个创建一个新的注释。这将接管所有现有的内容,但也会设置一个新的起始子项和标识符。创建后,注释是独立的,即修改一个注释不会影响另一个注释。
original_note=Note("Original")new_note=Note.from_note(original_note)
奖金
在浏览器中查看商店的内容
要快速浏览商店,可以使用命令行中的包。它将启动一个http服务器并在web浏览器中自动打开相关页面。页面包含一个交互式表,其中显示存储区中所有注释的最相关信息,如度量和参数。该表的样式与Load notes部分中显示的表相似。
$ python -m hypernotes hyperstore.json
这只需要一个现代化的web浏览器和一个internet连接来加载一些javascript库和css文件。
要查看所有可用选项,请传递--help
参数。
存储其他对象
如果你nt要存储实验中较大的工件,比如训练过的模型,可以创建一个单独的文件夹,并使用注释的标识符作为名称的一部分。
experiment_folder=f"experiment_{note.identifier}"
然后,您可以将任何其他对象存储到该文件夹中,并且将它们再次链接到使用hypernotes存储的超参数和度量时,将非常容易实现。
备选方案
如果您需要更好的多用户功能、更高级的再现性功能、数据集版本控制,MLflow、Sacred或DVC等工具,请查看。
开发
如果你发现了一个bug或者错过了一个特性,可以随意打开一个github问题或者提交一个pull请求。
开发软件包的任何要求都可以使用
pip install -r requirements_dev.txt
确保所有的毒物测试都通过。