{"id":13717508,"url":"https://github.com/blue-season/pywarm","last_synced_at":"2025-04-09T23:19:49.643Z","repository":{"id":57458420,"uuid":"205091759","full_name":"blue-season/pywarm","owner":"blue-season","description":"A cleaner way to build neural networks for PyTorch.","archived":false,"fork":false,"pushed_at":"2019-09-23T16:43:17.000Z","size":46578,"stargazers_count":184,"open_issues_count":0,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-02T20:11:15.600Z","etag":null,"topics":["clean-code","data-science","deep-learning","keras","machine-learning","neural-network","neural-networks","python3","pytorch"],"latest_commit_sha":null,"homepage":"https://blue-season.github.io/pywarm/","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/blue-season.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-29T06:01:35.000Z","updated_at":"2024-11-15T21:10:34.000Z","dependencies_parsed_at":"2022-09-09T22:50:23.926Z","dependency_job_id":null,"html_url":"https://github.com/blue-season/pywarm","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blue-season%2Fpywarm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blue-season%2Fpywarm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blue-season%2Fpywarm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blue-season%2Fpywarm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blue-season","download_url":"https://codeload.github.com/blue-season/pywarm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125977,"owners_count":21051842,"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":["clean-code","data-science","deep-learning","keras","machine-learning","neural-network","neural-networks","python3","pytorch"],"created_at":"2024-08-03T00:01:23.313Z","updated_at":"2025-04-09T23:19:49.595Z","avatar_url":"https://github.com/blue-season.png","language":"Python","funding_links":[],"categories":["Pytorch \u0026 related libraries｜Pytorch \u0026 相关库","Python","Pytorch \u0026 related libraries"],"sub_categories":["Other libraries｜其他库:","Other libraries:"],"readme":"\n[![PyWarm - A cleaner way to build neural networks for PyTorch](https://github.com/blue-season/pywarm/raw/gh-pages/docs/pywarm-logo.png)](https://blue-season.github.io/pywarm/)\n\n# PyWarm\n\nA cleaner way to build neural networks for PyTorch.\n\n[![PyPI Python Version](https://img.shields.io/pypi/pyversions/pywarm)](https://github.com/blue-season/pywarm)\n[![PyPI Version](https://img.shields.io/pypi/v/pywarm)](https://pypi.org/project/pywarm/)\n[![License](https://img.shields.io/github/license/blue-season/pywarm)](https://github.com/blue-season/pywarm/blob/master/LICENSE)\n\n[Examples](https://blue-season.github.io/pywarm/docs/example/)  |  [Tutorial](https://blue-season.github.io/pywarm/docs/tutorial/)  |   [API reference](https://blue-season.github.io/pywarm/reference/warm/functional/)\n\n----\n\n## Introduction\n\nPyWarm is a lightweight, high-level neural network construction API for PyTorch.\nIt enables defining all parts of NNs in the functional way.\n\nWith PyWarm, you can put *all* network data flow logic in the `forward()` method of\nyour model, without the need to define children modules in the `__init__()` method\nand then call it again in the `forward()`.\nThis result in a much more readable model definition in fewer lines of code.\n\nPyWarm only aims to simplify the network definition, and does not attempt to cover\nmodel training, validation or data handling.\n\n----\n\nFor example, a convnet for MNIST:\n(If needed, click the tabs to switch between Warm and Torch versions)\n\n\n``` Python tab=\"Warm\" linenums=\"1\"\n# powered by PyWarm\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport warm\nimport warm.functional as W\n\n\nclass ConvNet(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n        warm.up(self, [2, 1, 28, 28])\n\n    def forward(self, x):\n        x = W.conv(x, 20, 5, activation='relu')\n        x = F.max_pool2d(x, 2)\n        x = W.conv(x, 50, 5, activation='relu')\n        x = F.max_pool2d(x, 2)\n        x = x.view(-1, 800)\n        x = W.linear(x, 500, activation='relu')\n        x = W.linear(x, 10)\n        return F.log_softmax(x, dim=1)\n```\n\n``` Python tab=\"Torch\" linenums=\"1\"\n# vanilla PyTorch version, taken from\n# pytorch tutorials/beginner_source/blitz/neural_networks_tutorial.py \nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass ConvNet(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(1, 20, 5, 1)\n        self.conv2 = nn.Conv2d(20, 50, 5, 1)\n        self.fc1 = nn.Linear(4*4*50, 500)\n        self.fc2 = nn.Linear(500, 10)\n\n    def forward(self, x):\n        x = F.relu(self.conv1(x))\n        x = F.max_pool2d(x, 2, 2)\n        x = F.relu(self.conv2(x))\n        x = F.max_pool2d(x, 2, 2)\n        x = x.view(-1, 4*4*50)\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        return F.log_softmax(x, dim=1)\n```\n\n----\n\nA couple of things you may have noticed:\n\n-   First of all, in the PyWarm version, the entire network definition and\n    data flow logic resides in the `forward()` method. You don't have to look\n    up and down repeatedly to understand what `self.conv1`, `self.fc1` etc.\n    is doing.\n\n-   You do not need to track and specify `in_channels` (or `in_features`, etc.)\n    for network layers. PyWarm can infer the information for you. e.g.\n\n```Python\n# Warm\nx = W.conv(x, 20, 5, activation='relu')\nx = W.conv(x, 50, 5, activation='relu')\n\n\n# Torch\nself.conv1 = nn.Conv2d(1, 20, 5, 1)\nself.conv2 = nn.Conv2d(20, 50, 5, 1)\n```\n\n-   One unified `W.conv` for all 1D, 2D, and 3D cases. Fewer things to keep track of!\n\n-   `activation='relu'`. All `warm.functional` APIs accept an optional `activation` keyword,\n    which is basically equivalent to `F.relu(W.conv(...))`. The keyword `activation` can also \n    take in a callable, for example `activation=torch.nn.ReLU(inplace=True)` or `activation=swish`.\n\nFor deeper neural networks, see additional [examples](https://blue-season.github.io/pywarm/docs/example/).\n\n----\n## Installation\n\n    pip3 install pywarm\n\n----\n## Quick start: 30 seconds to PyWarm\n\nIf you already have experinces with PyTorch, using PyWarm is very straightforward:\n\n-   First, import PyWarm in you model file:\n```Python\nimport warm\nimport warm.functional as W\n```\n\n-   Second, remove child module definitions in the model's `__init__()` method.\n    In stead, use `W.conv`, `W.linear` ... etc. in the model's `forward()` method,\n    just like how you would use torch nn functional `F.max_pool2d`, `F.relu` ... etc.\n\n    For example, instead of writing:\n\n```Python\n# Torch\nclass MyModule(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size)\n        # other child module definitions\n    def forward(self, x):\n        x = self.conv1(x)\n        # more forward steps\n```\n\n-   You can now write in the warm way:\n\n```Python\n# Warm\nclass MyWarmModule(nn.Module):\n    def __init__(self):\n        super().__init__()\n        warm.up(self, input_shape_or_data)\n    def forward(self, x):\n        x = W.conv(x, out_channels, kernel_size) # no in_channels needed\n        # more forward steps\n```\n\n-   Finally, don't forget to warmify the model by adding\n    \n    `warm.up(self, input_shape_or_data)`\n\n    at the end of the model's `__init__()` method. You need to supply\n    `input_shape_or_data`, which is either a tensor of input data, \n    or just its shape, e.g. `[2, 1, 28, 28]` for MNIST inputs.\n    \n    The model is now ready to use, just like any other PyTorch models.\n\nCheck out the [tutorial](https://blue-season.github.io/pywarm/docs/tutorial/) \nand [examples](https://blue-season.github.io/pywarm/docs/example/) if you want to learn more!\n\n----\n## Testing\n\nClone the repository first, then\n\n    cd pywarm\n    pytest -v\n\n----\n## Documentation\n\nDocumentations are generated using the excellent [Portray](https://timothycrosley.github.io/portray/) package.\n\n-   [Examples](https://blue-season.github.io/pywarm/docs/example/)\n\n-   [Tutorial](https://blue-season.github.io/pywarm/docs/tutorial/) \n\n-   [API reference](https://blue-season.github.io/pywarm/reference/warm/functional/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblue-season%2Fpywarm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblue-season%2Fpywarm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblue-season%2Fpywarm/lists"}