{"id":13716821,"url":"https://github.com/oval-group/mlogger","last_synced_at":"2025-05-07T06:31:42.844Z","repository":{"id":56405833,"uuid":"83698697","full_name":"oval-group/mlogger","owner":"oval-group","description":"a lightweight and simple logger for Machine Learning","archived":false,"fork":false,"pushed_at":"2020-11-09T22:31:16.000Z","size":174,"stargazers_count":127,"open_issues_count":1,"forks_count":12,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-05-23T06:47:34.710Z","etag":null,"topics":["deep-learning","experiments","logging","machine-learning","optimization","python","pytorch","tensorboard","visdom","visualization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oval-group.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-02T16:19:08.000Z","updated_at":"2024-01-04T16:11:57.000Z","dependencies_parsed_at":"2022-08-15T18:10:14.366Z","dependency_job_id":null,"html_url":"https://github.com/oval-group/mlogger","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oval-group%2Fmlogger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oval-group%2Fmlogger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oval-group%2Fmlogger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oval-group%2Fmlogger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oval-group","download_url":"https://codeload.github.com/oval-group/mlogger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252405984,"owners_count":21742689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","experiments","logging","machine-learning","optimization","python","pytorch","tensorboard","visdom","visualization"],"created_at":"2024-08-03T00:01:14.788Z","updated_at":"2025-05-07T06:31:42.498Z","avatar_url":"https://github.com/oval-group.png","language":"Python","readme":"# MLogger: a Machine Learning logger\n\nCurrently in version alpha, the API might undergo some minor changes.\n\n## Installation\n\nTo install the package, run:\n* `pip install mlogger`\n\n## Why Use MLogger?\nThese are the strengths of `mlogger` that make it a useful tool for logging machine learning experiments.\n\n* Readable code that is easy to add to current projects:\n```python\nacc = mlogger.metric.Average()\nacc.update(100)\nacc.update(92)\nprint(acc.value)  # 96.0\nacc.log()  # internally stores value of 96.0 with automatic time-stamp\nacc.reset()  # reset average value\n```\n* Flexible use of metrics with containers, easy to save and re-load:\n```python\nxp = mlogger.Container()\nxp.train = mlogger.Container()\nxp.train.accuracy = mlogger.metric.Average()\nxp.total_timer = mlogger.metric.Timer()\n\nxp.total_timer.reset()  # start timer\nxp.train.accuracy.update(97)\nxp.total_timer.update()  # say 0.0001 second has elapsed since timer started, current_value is 0.0001\nxp.save_to('saved_state.json')\n\nnew_xp = mlogger.load_container('saved_state.json')\nprint(new_xp.train.accuracy.value)  # 97.0\nprint(new_xp.total_timer.value)  # 0.0001\n```\n\n* Improve your user experience with `visdom`:\n    * Ease of use:\n    ```python\n    plotter = mlogger.VisdomPlotter(({'env': 'my_experiment', 'server': 'http://localhost', 'port': 8097}))\n    acc = mlogger.metric.Average(plotter=plotter, plot_title=\"Accuracy\")\n    acc.update(100)\n    acc.update(92)\n    print(acc.value)  # 96.0\n    acc.log()  # automatically sends 96.0 to visdom server on window with title 'Accuracy'\n    ```\n    * Robustness: if `visdom` fails to send data (due to a network instability for instance), `logger` automatically caches it and tries to send it together with the next request\n    * Performance: you can manually choose when to update the `visdom` plots. This permits to batch the data being sent and yields considerable speedups when logging thousands or more points per second.\n\n* Save all output printed in the console to a text file\n```python\nwith mlogger.stdout_to('printed_stuff.txt'):\n    # code printing stuff here...\n```\n* Automatically save information about the date, time, current directory, machine name, version control status of the code.\n```python\ncfg = mlogger.Config(get_general_info=True, get_git_info=True)\nprint(cfg.date_and_time, cfg.cwd, cfg.git_hash, cfg.git_diff)\n```\n\n## Example\nThe following example shows some functionalities of the package (full example code in `examples/example.py`):\n\n```python\nimport mlogger\nimport numpy as np\n\n#...\n# code to generate fake data\n#...\n\n\n# some hyper-parameters of the experiment\nuse_visdom = True\nlr = 0.01\nn_epochs = 10\n\n#----------------------------------------------------------\n# Prepare logging\n#----------------------------------------------------------\n\n# log the hyperparameters of the experiment\nif use_visdom:\n    plotter = mlogger.VisdomPlotter({'env': 'my_experiment', 'server': 'http://localhost', 'port': 8097},\n                                   manual_update=True)\nelse:\n    plotter = None\n\nxp = mlogger.Container()\n\nxp.config = mlogger.Config(plotter=plotter)\nxp.config.update(lr=lr, n_epochs=n_epochs)\n\nxp.epoch = mlogger.metric.Simple()\n\nxp.train = mlogger.Container()\nxp.train.acc1 = mlogger.metric.Average(plotter=plotter, plot_title=\"Accuracy@1\", plot_legend=\"training\")\nxp.train.acck = mlogger.metric.Average(plotter=plotter, plot_title=\"Accuracy@k\", plot_legend=\"training\")\nxp.train.loss = mlogger.metric.Average(plotter=plotter, plot_title=\"Objective\")\nxp.train.timer = mlogger.metric.Timer(plotter=plotter, plot_title=\"Time\", plot_legend=\"training\")\n\nxp.val = mlogger.Container()\nxp.val.acc1 = mlogger.metric.Average(plotter=plotter, plot_title=\"Accuracy@1\", plot_legend=\"validation\")\nxp.val.acck = mlogger.metric.Average(plotter=plotter, plot_title=\"Accuracy@k\", plot_legend=\"validation\")\nxp.val.timer = mlogger.metric.Timer(plotter=plotter, plot_title=\"Time\", plot_legend=\"validation\")\n\nxp.val_best = mlogger.Container()\nxp.val_best.acc1 = mlogger.metric.Maximum(plotter=plotter, plot_title=\"Accuracy@1\", plot_legend=\"validation-best\")\nxp.val_best.acck = mlogger.metric.Maximum(plotter=plotter, plot_title=\"Accuracy@k\", plot_legend=\"validation-best\")\n\n\n#----------------------------------------------------------\n# Training\n#----------------------------------------------------------\n\n\nfor epoch in range(n_epochs):\n    # train model\n    for metric in xp.train.metrics():\n        metric.reset()\n    for (x, y) in training_data():\n        loss, acc1, acck = oracle(x, y)\n        # accumulate metrics (average over mini-batches)\n        batch_size = len(x)\n        xp.train.loss.update(loss, weighting=batch_size)\n        xp.train.acc1.update(acc1, weighting=batch_size)\n        xp.train.acck.update(acck, weighting=batch_size)\n    xp.train.timer.update()\n    for metric in xp.train.metrics():\n        metric.log()\n\n    # reset metrics in container xp.val\n    # (does not include xp.val_best.acc1 and xp.val_best.acck, which we do not want to reset)\n    for metric in xp.val.metrics():\n        metric.reset()\n\n    # update values on validation set\n    for (x, y) in validation_data():\n        _, acc1, acck = oracle(x, y)\n        batch_size = len(x)\n        xp.val.acc1.update(acc1, weighting=batch_size)\n        xp.val.acck.update(acck, weighting=batch_size)\n    xp.val.timer.update()\n    # log values on validation set\n    for metric in xp.val.metrics():\n        metric.log()\n\n    # update best values on validation set\n    xp.val_best.acc1.update(xp.val.acc1.value)\n    xp.val_best.acck.update(xp.val.acck.value)\n    # log best values on validation set\n    for metric in xp.val_best.metrics():\n        metric.log()\n\nprint(\"=\" * 50)\nprint(\"Best Performance On Validation Data:\")\nprint(\"-\" * 50)\nprint(\"Prec@1: \\t {0:.2f}%\".format(xp.val_best.acc1.value))\nprint(\"Prec@k: \\t {0:.2f}%\".format(xp.val_best.acck.value))\n\nplotter.update_plots()\n\n#----------------------------------------------------------\n# Save \u0026 load experiment\n#----------------------------------------------------------\n\nxp.train.loss.reset()\nxp.train.loss.update(1)\nprint('Train loss value before saving state: {}'.format(xp.train.loss.value))\n\nxp.save_to('state.json')\n\nnew_plotter = mlogger.VisdomPlotter(visdom_opts={'env': 'my_experiment', 'server': 'http://localhost', 'port': 8097},\n                                    manual_update=True)\n\nnew_xp = mlogger.load_container('state.json')\nnew_xp.plot_on(new_plotter)\nnew_plotter.update_plots()\n\nprint('Current train loss value: {}'.format(new_xp.train.loss.value))\nnew_xp.train.loss.update(2)\nprint('Updated train loss value: {}'.format(new_xp.train.loss.value))\n\n# # remove the file\nos.remove('state.json')\n```\n\nThis generates (twice) the following plots on `visdom`:\n![alt text](examples/example.jpg)\n\n\n## Acknowledgements\n\nFull credits to the authors of [tnt](https://github.com/pytorch/tnt) for the structure with metrics.\n","funding_links":[],"categories":["Pytorch \u0026 related libraries｜Pytorch \u0026 相关库","Python"],"sub_categories":["Other libraries｜其他库:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foval-group%2Fmlogger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foval-group%2Fmlogger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foval-group%2Fmlogger/lists"}