{"id":21321698,"url":"https://github.com/jamormoussa/keratorch","last_synced_at":"2025-04-14T22:26:15.772Z","repository":{"id":262812715,"uuid":"888431566","full_name":"JamorMoussa/keratorch","owner":"JamorMoussa","description":"Keratorch: A Keras-style high-level API for building and training models in PyTorch","archived":false,"fork":false,"pushed_at":"2024-12-20T21:09:57.000Z","size":909,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T10:47:21.071Z","etag":null,"topics":["deep-learning","keras","machine-learning","model","pytorch","tensorflow","training"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JamorMoussa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":"ROADMAP.md","authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-14T11:44:35.000Z","updated_at":"2024-12-19T02:56:11.000Z","dependencies_parsed_at":"2024-11-14T12:46:01.569Z","dependency_job_id":"e10572c6-7625-47e6-bada-c586406bb63d","html_url":"https://github.com/JamorMoussa/keratorch","commit_stats":null,"previous_names":["jamormoussa/keratorch"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JamorMoussa%2Fkeratorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JamorMoussa%2Fkeratorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JamorMoussa%2Fkeratorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JamorMoussa%2Fkeratorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JamorMoussa","download_url":"https://codeload.github.com/JamorMoussa/keratorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248970547,"owners_count":21191450,"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","keras","machine-learning","model","pytorch","tensorflow","training"],"created_at":"2024-11-21T20:08:39.635Z","updated_at":"2025-04-14T22:26:15.750Z","avatar_url":"https://github.com/JamorMoussa.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keratorch\n\n**Keratorch** is a high-level API for PyTorch, inspired by Keras, aimed at simplifying the process of defining, compiling, and training models in PyTorch. Designed to enhance user experience, Keratorch enables developers to build neural networks in a modular and intuitive way, making PyTorch feel even more accessible.\n\n## Key Features\n\n- **Keras-like API**: Intuitive interface that follows the Keras workflow.\n- **Flexible Model Architecture**: Easily define custom model layers and transformations.\n- **Seamless Training Process**: Built-in methods for compiling and training models, reducing boilerplate code.\n\n## Installation\n\nTo install Keratorch, clone the repository and install dependencies:\n\n```bash\ngit clone https://github.com/JamorMoussa/keratorch.git\ncd keratorch\npip install -r requirements.txt\n```\n\n## Getting Started\n\nHere’s a quick example to get started with **Keratorch**.\n\n### Import Packages\n\n```python\nimport keratorch as kt\n\nimport torch as tr\nimport torch.nn as nn\nfrom torch.utils.data import TensorDataset, DataLoader\nimport matplotlib.pyplot as plt\n```\n\n### Model Definition\n\nUse `Sequential`, a Keras-inspired container, to build the model. The `Lambda` layer allows custom transformations to be integrated easily.\n\n```python\nclass Model(kt.nn.ktModule):\n\n    def __init__(self):\n        super(Model, self).__init__()\n\n        self.fc = nn.Sequential(\n            kt.nn.Lambda(lambda x: x.unsqueeze(1)),\n            nn.Conv1d(1, 10, kernel_size=2),\n            nn.ReLU(),\n            nn.Flatten(start_dim=1),\n            nn.Linear(10 * 6 , 1)\n        )\n\n    def forward(self, x):\n        return self.fc(x)\n```\n\nCreate an instance of the `Model`: \n\n```python\nmodel = Model()\n```\n\n#### Make a Custom Metric:\n\nYou can make a custom metric:\n\n```python\nclass PersonMetric(kt.metrics.Metric):\n\n    def compute_metric(self, state: kt.state.State):\n        \n        outs = state.outputs.flatten()\n        targets = state.batch[1].flatten()\n\n        outs_ = outs - outs.mean()\n        targets_ = targets - targets.mean()\n        self.metric_value += (outs_ * targets_).sum().item() / ((outs_**2).sum().sqrt() * (targets_**2).sum().sqrt()).item()\n\n```\n\n### Model Compilation\n\nCompile the model with a loss function and an optimizer.\n\n```python\nmodel.compile(\n    loss_fn= nn.MSELoss(),\n    optimizer= kt.optim.Adam(lr=0.001),\n    metrics= [\n        kt.metrics.Loss(name=\"loss\"),\n        PersonMetric(name=\"person\")\n        # kt.metrics.Accuracy()\n    ],\n    callbacks= []\n)\n```\n\n### Data Preparation\n\nCreate synthetic data for training.\n\n```python\nx = tr.rand(1000, 7)\ny = tr.mm(x, tr.rand(1, 7).t())\n\ndataset = TensorDataset(x, y)\nloader = DataLoader(dataset, batch_size=25)\n```\n\n### Training the Model\n\nTrain the model using the `fit` method.\n\n```python\nhist = model.fit(trainloader= loader, num_iters=5, num_records=40)\n```\n\nTraining output:\n```\nEpoch: [0/10] | loss: 0.1505 | person: 0.6649: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 286.71it/s]\nEpoch: [1/10] | loss: 0.0804 | person: 0.8177: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 290.66it/s]\nEpoch: [2/10] | loss: 0.0702 | person: 0.8433: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 300.28it/s]\nEpoch: [3/10] | loss: 0.0685 | person: 0.8481: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 309.34it/s]\nEpoch: [4/10] | loss: 0.0683 | person: 0.8489: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 288.74it/s]\nEpoch: [5/10] | loss: 0.0683 | person: 0.8491: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 287.12it/s]\nEpoch: [6/10] | loss: 0.0684 | person: 0.8491: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 276.69it/s]\nEpoch: [7/10] | loss: 0.0684 | person: 0.8492: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 247.33it/s]\nEpoch: [8/10] | loss: 0.0684 | person: 0.8492: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 293.97it/s]\nEpoch: [9/10] | loss: 0.0685 | person: 0.8493: 100%|██████████████████████████████████| 400/400 [00:01\u003c00:00, 291.07it/s]\ndict_keys(['loss', 'person'])\n```\n\n### Visualizing Training Loss\n\nPlot the training loss to observe the model's learning progress.\n\n```python\nplt.plot(hist.history[\"loss\"])\nplt.show()\nplt.plot(hist.history[\"person\"])\nplt.show()\n```\n\n![Training Loss](https://raw.githubusercontent.com/JamorMoussa/images/refs/heads/main/src/keratorch/loss_plot.png)\n\n## Contributing\n\nContributions are welcome! Please feel free to submit issues, fork the repository, and send pull requests.\n\n## License\n\n\u003c!-- This project is licensed under the MIT License. --\u003e\n\n## Acknowledgments\n\nThanks to the PyTorch and Keras communities for their inspiring frameworks and continued innovations.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamormoussa%2Fkeratorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamormoussa%2Fkeratorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamormoussa%2Fkeratorch/lists"}