{"id":13717188,"url":"https://github.com/cogitare-ai/cogitare","last_synced_at":"2025-12-30T16:12:20.907Z","repository":{"id":62563422,"uuid":"94737131","full_name":"cogitare-ai/cogitare","owner":"cogitare-ai","description":"🔥 Cogitare - A Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python ","archived":true,"fork":false,"pushed_at":"2024-04-28T01:40:22.000Z","size":3135,"stargazers_count":77,"open_issues_count":22,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-20T18:03:58.971Z","etag":null,"topics":["deep-learning","framework","machine-learning","modular-deep-learning","pytorch","toolbox","toolchain"],"latest_commit_sha":null,"homepage":"http://cogitare-ai.aronbordin.com","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/cogitare-ai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-19T04:41:48.000Z","updated_at":"2024-10-10T19:58:00.000Z","dependencies_parsed_at":"2024-11-14T05:31:15.783Z","dependency_job_id":"0869cb12-d732-4a8f-9e41-b1d1ede00e3b","html_url":"https://github.com/cogitare-ai/cogitare","commit_stats":{"total_commits":100,"total_committers":1,"mean_commits":100.0,"dds":0.0,"last_synced_commit":"fa99b8ef30e2f74e16fb542f2992582d1bd3ac2c"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogitare-ai%2Fcogitare","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogitare-ai%2Fcogitare/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogitare-ai%2Fcogitare/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogitare-ai%2Fcogitare/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cogitare-ai","download_url":"https://codeload.github.com/cogitare-ai/cogitare/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252833396,"owners_count":21811174,"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","framework","machine-learning","modular-deep-learning","pytorch","toolbox","toolchain"],"created_at":"2024-08-03T00:01:19.072Z","updated_at":"2025-12-13T22:06:48.445Z","avatar_url":"https://github.com/cogitare-ai.png","language":"Python","funding_links":[],"categories":["Pytorch \u0026 related libraries｜Pytorch \u0026 相关库","Pytorch \u0026 related libraries","Python"],"sub_categories":["Other libraries｜其他库:","Other libraries:","General-Purpose Machine Learning"],"readme":"\u003cp align=\"center\"\u003e\u003cimg width=\"80%\" src=\"https://raw.githubusercontent.com/cogitare-ai/cogitare/master/docs/source/art/logo-line.png\" /\u003e\u003c/p\u003e\n\n\u003ch3 align=\"center\"\u003e\u003cb\u003eCogitare\u003c/b\u003e is a Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python. A friendly interface for beginners and a powerful toolset for experts.\u003c/h3\u003e\n\n\u003ch3 align=\"center\"\u003e\u003cb\u003eCogitare\u003c/b\u003e is built on top of PyTorch.\u003c/h3\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://docs.cogitare-ai.org/\"\u003eDocumentation\u003c/a\u003e •\n  \u003ca href=\"http://tutorials.cogitare-ai.org/\"\u003eTutorials\u003c/a\u003e •\n  \u003ca href=\"#1-about\"\u003eAbout\u003c/a\u003e •\n  \u003ca href=\"#2-install\"\u003eInstall\u003c/a\u003e •\n  \u003ca href=\"#3-quickstart\"\u003eQuickstart\u003c/a\u003e •\n  \u003ca href=\"#4-contribution\"\u003eContribution\u003c/a\u003e\n\u003c/p\u003e\n\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://travis-ci.org/cogitare-ai/cogitare\"\u003e\n\t\t\u003cimg src=\"https://travis-ci.org/cogitare-ai/cogitare.svg?branch=master\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://codecov.io/gh/cogitare-ai/cogitare\"\u003e\n\t\t\u003cimg src=\"https://codecov.io/gh/cogitare-ai/cogitare/branch/master/graph/badge.svg\" /\u003e\n\t\u003c/a\u003e\n\t\u003ca href=\"https://badge.fury.io/py/cogitare\"\u003e\n\t\t\u003cimg src=\"https://badge.fury.io/py/cogitare.svg\" alt=\"PyPI version\" height=\"18\"\u003e\n\t\u003c/a\u003e\n\u003c/p\u003e\n\n\n# 1. About\n\nIt uses the best of [PyTorch](http://pytorch.org/), [Dask](https://dask.pydata.org/), [NumPy](http://www.numpy.org/), and others tools through a simple interface to train, to evaluate, to test\nmodels and more.\n\nWith Cogitare, you can use classical machine learning algorithms with high\nperformance and develop state-of-the-art models quickly.\n\nCheck the tutorials at http://tutorials.cogitare-ai.org/\n\nThe primary objectives of Cogitare are:\n\n- provide an easy-to-use interface to train and evaluate models;\n- provide tools to debug and analyze the model;\n- provide implementations of state-of-the-art models (models for common tasks, ready\n  to train and ready to use);\n- provide ready-to-use implementations of straightforward and classical models (such as\n  LogisticRegression);\n- be compatible with models for a broad range of problems;\n- be compatible with other tools (scikit-learn, etcs);\n- keep growing with the community: accept as many new features as possible;\n- provide a friendly interface to beginners, and powerful features for experts;\n- take the best of the hardware through multi-processing and multi-threading;\n- and others.\n\nCurrently, it's a work in progress project that aims to provide a complete\ntoolchain for machine learning and deep learning development, taking the best\nof cuda and multi-core processing.\n\n# 2. Install\n\n- Install PyTorch from http://pytorch.org/\n- Install Cogitare from PIP:\n\n      pip install cogitare\n\n- Cogitare is in active development, so it's recommended to get the latest version from GitHub. To install directly from GitHub, use:\n\n      pip install -e git+https://github.com/cogitare-ai/cogitare#egg=cogitare\n\n# 3. Quickstart\n\n\nThis is a simple tutorial to get started with Cogitare main functionalities.\n\nIn this tutorial, we will write a Convolutional Neural Network (CNN) to\nclassify handwritten digits (MNIST).\n\n### 3.1 Model\n\nWe start by defining our CNN model.\n\nWhen developing a model with Cogitare, your model must extend the ``cogitare.Model`` class. This class provides the Model interface, which allows you to train and evaluate the model efficiently.\n\nTo implement a model, you must extend the ``cogitare.Model`` class and implement the ``forward()`` and ``loss()`` methods. The forward method will receive the batch. In this way, it is necessary to implement the forward pass through the network in this method, and then return the output of the net. The loss method will receive the output of the ``forward()`` and the batch received from the iterator, apply a loss function, compute and return it.\n\nThe Model interface will iterate over the dataset, and execute each batch on ``forward``, ``loss``, and ``backward``.\n\n\n```python\n# adapted from https://github.com/pytorch/examples/blob/master/mnist/main.py\nfrom cogitare import Model\nfrom cogitare import utils\nfrom cogitare.data import DataSet, AsyncDataLoader\nfrom cogitare.plugins import EarlyStopping\nfrom cogitare.metrics.classification import accuracy\nimport cogitare\n\nimport torch.nn as nn\nimport torch\nimport torch.nn.functional as F\nfrom torch.nn.utils import clip_grad_norm\nimport torch.optim as optim\n\nfrom sklearn.datasets import fetch_mldata\n\nimport numpy as np\n\nCUDA = True\n\n\ncogitare.utils.set_cuda(CUDA)\n```\n\n\n```python\nclass CNN(Model):\n    \n    def __init__(self):\n        super(CNN, self).__init__()\n        \n        # define the model\n        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)\n        self.conv2_drop = nn.Dropout2d()\n        self.fc1 = nn.Linear(320, 50)\n        self.fc2 = nn.Linear(50, 10)\n    \n    def forward(self, batch):\n        # in this sample, each batch will be a tuple containing (input_batch, expected_batch)\n        # in forward in are only interested in input so that we can ignore the second item of the tuple\n        input, _ = batch\n        \n        # batch X flat tensor -\u003e batch X 1 channel (gray) X width X heigth\n        input = input.view(32, 1, 28, 28)\n        \n        # pass the data in the net\n        x = F.relu(F.max_pool2d(self.conv1(input), 2))\n        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))\n        x = x.view(-1, 320)\n        x = F.relu(self.fc1(x))\n        x = F.dropout(x, training=self.training)\n        x = self.fc2(x)\n\n        # return the model output\n        return F.log_softmax(x, dim=1)\n    \n    def loss(self, output, batch):\n        # in this sample, each batch will be a tuple containing (input_batch, expected_batch)\n        # in loss in are only interested in expected so that we can ignore the first item of the tuple\n        _, expected = batch\n        \n        return F.nll_loss(output, expected)\n```\n\nThe model class is simple; it only requires de forward and loss methods. By default, Cogitare will backward the loss returned by the ``loss()`` method, and optimize the model parameters. If you want to disable the Cogitare backward and optimization steps, just return ``None`` in the loss function. If you return None, you are responsible by backwarding and optimizing the parameters.\n\n### 3.2 Data Loading\nIn this step, we will load the data from sklearn package.\n\n\n```python\nmnist = fetch_mldata('MNIST original')\nmnist.data = (mnist.data / 255).astype(np.float32)\n```\n\nCogitare provides a toolbox to load and pre-process data for your models. In this introduction, we will use the ``DataSet`` and the ``AsyncDataLoader`` as examples.\n\nThe ``DataSet`` is responsible by iterating over multiples data iterators (in our case, we'll have two data iterators: input samples, expected samples).\n\n\n```python\n# as input, the DataSet is expected a list of iterators. In our case, the first iterator is the input \n# data and the second iterator is the target data\n\n# also, we set the batch size to 32 and enable the shuffling\n\n# drop the last batch if its size is different of 32\ndata = DataSet([mnist.data, mnist.target.astype(int)], batch_size=32, shuffle=True, drop_last=True)\n\n# then, we split our dataset into a train and into a validation sets, by a ratio of 0.8\ndata_train, data_validation = data.split(0.8)\n```\n\nNotice that Cogitare accepts any iterator as input. Instead of using our DataSet, you can use the mnist.data itself, PyTorch's data loaders, or any other input that acts as an iterator.\n\nIn some cases, we can increase the model performance by loading the data using multiples threads/processes or by pre-loading the data before being requested by the model.\n\nWith the ``AsyncDataLoader``, we can load N batches ahead of the model execution in parallel. We present this technique in this sample because it can increase performance in a wide range of models (when the data loading or pre-processing is slower than the model execution).\n\n\n```python\ndef pre_process(batch):\n    input, expected = batch\n    \n    # the data is a numpy.ndarray (loaded from sklearn), so we need to convert it to Variable\n    input = utils.to_variable(input, dtype=torch.FloatTensor)  # converts to a torch Variable of LongTensor\n    expected = utils.to_variable(expected, dtype=torch.LongTensor)  # converts to a torch Variable of LongTensor\n    return input, expected\n\n\n# we wrap our data_train and data_validation iterators over the async data loader.\n# each loader will load 16 batches ahead of the model execution using 8 workers (8 threads, in this case).\n# for each batch, it will be pre-processed in parallel with the preprocess function, that will load the data\n# on GPU\ndata_train = AsyncDataLoader(data_train, buffer_size=16, mode='threaded', workers=8, on_batch_loaded=pre_process)\ndata_validation = AsyncDataLoader(data_validation, buffer_size=16, mode='threaded', workers=8, on_batch_loaded=pre_process)\n```\n\nto cache the async buffer before training, we can:\n\n\n```python\ndata_train.cache()\ndata_validation.cache()\n```\n\n## 3.3 Training\n\nNow, we can train our model.\n\nFirst, lets create the model instance and add the default plugins to watch the training status.\nThe default plugin includes:\n\n- Progress bar per batch and epoch\n- Plot training and validation losses (if validation_dataset is present)\n- Log training loss\n\n\n```python\nmodel = CNN()\nmodel.register_default_plugins()\n```\n\nBesides that, we may want to add some extra plugins, such as the EarlyStopping. So, if the model is not decreasing the loss after N epochs, the training stops and the best model is used.\n\nTo add the early stopping algorithm, you can use:\n\n\n```python\nearly = EarlyStopping(max_tries=10, path='/tmp/model.pt')\n# after 10 epochs without decreasing the loss, stop the training and the best model is saved at /tmp/model.pt\n\n# the plugin will execute in the end of each epoch\nmodel.register_plugin(early, 'on_end_epoch')\n```\n\nAlso, a common technique is to clip the gradient during training. If you want to clip the grad, you can use:\n\n\n```python\nmodel.register_plugin(lambda *args, **kw: clip_grad_norm(model.parameters(), 1.0), 'before_step')\n# will execute the clip_grad_norm before each optimization step\n```\n\nNow, we define the optimizator, and then start the model training:\n\n\n```python\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\nif CUDA:\n    model = model.cuda()\nmodel.learn(data_train, optimizer, data_validation, max_epochs=100)\n```\n\n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Model: \n    \n    CNN(\n      (conv1): Conv2d (1, 10, kernel_size=(5, 5), stride=(1, 1))\n      (conv2): Conv2d (10, 20, kernel_size=(5, 5), stride=(1, 1))\n      (conv2_drop): Dropout2d(p=0.5)\n      (fc1): Linear(in_features=320, out_features=50)\n      (fc2): Linear(in_features=50, out_features=10)\n    )\n    \n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Training data: \n    \n    DataSet with:\n        containers: [\n            TensorHolder with 1750x32 samples\n    \tTensorHolder with 1750x32 samples\n        ],\n        batch size: 32\n    \n    \n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Number of trainable parameters: 21,840\n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Number of non-trainable parameters: 0\n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Total number of parameters: 21,840\n    2018-02-02 20:59:23 sprawl cogitare.core.model[2443] INFO Starting the training ...\n    2018-02-02 21:02:04 sprawl cogitare.core.model[2443] INFO Training finished\n    \n    Stopping training after 10 tries. Best score 0.0909\n    Model restored from: /tmp/model.pt\n\n![](http://docs.cogitare-ai.org/_images/quickstart_23_3.png)\n\n\nTo check the model loss and accuracy on the validation dataset:\n\n\n```python\ndef model_accuracy(output, data):\n    _, indices = torch.max(output, 1)\n    \n    return accuracy(indices, data[1])\n\n# evaluate the model loss and accuracy over the validation dataset\nmetrics = model.evaluate_with_metrics(data_validation, {'loss': model.metric_loss, 'accuracy': model_accuracy})\n\n# the metrics is an dict mapping the metric name (loss or accuracy, in this sample) to a list of the accuracy output\n# we have a measurement per batch. So, to have a value of the full dataset, we take the mean value:\n\nmetrics_mean = {'loss': 0, 'accuracy': 0}\nfor loss, acc in zip(metrics['loss'], metrics['accuracy']):\n    metrics_mean['loss'] += loss\n    metrics_mean['accuracy'] += acc.item()\n\nqtd = len(metrics['loss'])\n\nprint('Loss: {}'.format(metrics_mean['loss'] / qtd))\nprint('Accuracy: {}'.format(metrics_mean['accuracy'] / qtd))\n```\n\n    Loss: 0.10143917564566948\n    Accuracy: 0.9846252860411899\n\n\nOne of the advantages of Cogitare is the plug-and-play APIs, which let you add/remove functionalities easily. With this sample, we trained a model with training progress bar, error plotting, early stopping, grad clipping, and model evaluation easily.\n\n# 4. Contribution\n\nCogitare is a work in progress project, and any contribution is welcome.\n\nYou can contribute testing and providing bug reports, proposing feature ideas,\nfixing bugs, pushing code, etcs.\n\n1. You want to propose a new Feature and implement it\n\t- post about your intended feature, and we shall discuss the design and implementation. Once we agree that the plan looks good, go ahead and implement it.\n2. You want to implement a feature or bug-fix for an outstanding issue\n    - Look at the outstanding issues here: https://github.com/cogitare-ai/cogitare/issues\n    - Pick an issue and comment on the task that you want to work on this feature\n    - If you need more context on a particular issue, please ask and we shall provide.\n\n\nOnce you finish implementing a feature or bugfix, please send a Pull Request to\nhttps://github.com/cogitare-ai/cogitare\n\nIf you are not familiar with creating a Pull Request, here are some guides:\n- http://stackoverflow.com/questions/14680711/how-to-do-a-github-pull-request\n- https://help.github.com/articles/creating-a-pull-request/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcogitare-ai%2Fcogitare","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcogitare-ai%2Fcogitare","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcogitare-ai%2Fcogitare/lists"}