{"id":28235006,"url":"https://github.com/unnir/cancelout","last_synced_at":"2025-09-21T06:03:52.731Z","repository":{"id":192988829,"uuid":"161810468","full_name":"unnir/CancelOut","owner":"unnir","description":"CancelOut is a special layer for deep neural networks that can help identify a subset of relevant input features for streaming or static data.","archived":false,"fork":false,"pushed_at":"2020-11-09T10:45:49.000Z","size":218,"stargazers_count":26,"open_issues_count":2,"forks_count":13,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-22T01:38:20.969Z","etag":null,"topics":["deep-learning","feature-importance","feature-selection"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/unnir.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,"governance":null}},"created_at":"2018-12-14T16:13:25.000Z","updated_at":"2024-03-28T00:51:33.000Z","dependencies_parsed_at":"2023-09-06T08:49:37.118Z","dependency_job_id":"7d5b0146-8e82-4f5a-8937-5b48f82742f8","html_url":"https://github.com/unnir/CancelOut","commit_stats":null,"previous_names":["unnir/cancelout"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/unnir/CancelOut","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unnir%2FCancelOut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unnir%2FCancelOut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unnir%2FCancelOut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unnir%2FCancelOut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unnir","download_url":"https://codeload.github.com/unnir/CancelOut/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unnir%2FCancelOut/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276199876,"owners_count":25601879,"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","status":"online","status_checked_at":"2025-09-21T02:00:07.055Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","feature-importance","feature-selection"],"created_at":"2025-05-18T22:14:44.483Z","updated_at":"2025-09-21T06:03:52.726Z","avatar_url":"https://github.com/unnir.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CancelOut\n# TL;DR\n**CancelOut** is a layer for deep neural networks, that can help identify a subset of relevant input features for streaming or static data.  \n\nTAGS: Feature Importance, Feature Ranking, Feature Selection, Deep Learning Sensitivity Analysis.\n\n# Intuition \n\n\u003cimg src=\"http://vadimborisov.com/CancelOut.png\" width=\"480\"\u003e\n\nThe main idea is to update weights (W) of CancelOut during a training stage, so that ''noisy'' or less essential features will be canceled out with a negative weight. Otherwise, the best features, which contribute more to a learning process is going to be passed through with a positive weight. One can see CancelOut is a \"gate\" input, there NN decides who is going to pass through (see the equation below). \n\n![equation](https://latex.codecogs.com/gif.latex?CancelOut(\\boldsymbol{X})\u0026space;=\u0026space;\\boldsymbol{X}\u0026space;\\odot\u0026space;g\u0026space;({W_{CO}}))\n\n![where](https://latex.codecogs.com/gif.latex?$\\hspace{2mm}\u0026space;where\u0026space;$\\boldsymbol{X}$\u0026space;is\u0026space;an\u0026space;input\u0026space;vector\u0026space;$\\boldsymbol{X}\u0026space;\\in\u0026space;\\mathbb{R}^N$,\u0026space;$W_{CO}$\u0026space;is\u0026space;a\u0026space;weight\u0026space;vector\u0026space;$W_{CO}\u0026space;\\in\u0026space;\\mathbb{R}^N$,\u0026space;$N$\u0026space;is\u0026space;a\u0026space;feature\u0026space;size,\u0026space;and\u0026space;$g$\u0026space;is\u0026space;an\u0026space;activation\u0026space;function.\u0026space;Note,\u0026space;$g(x)$\u0026space;denotes\u0026space;here\u0026space;elementwise\u0026space;application,\u0026space;e.g.\u0026space;$\u0026space;\\boldsymbol{X}\u0026space;=\\begin{bmatrix}\u0026space;a\u0026space;\\\\\u0026space;b\u0026space;\\\\\u0026space;c\u0026space;\\\\\u0026space;\\end{bmatrix}\u0026space;$,\u0026space;then\u0026space;$g(\\boldsymbol{X})\u0026space;=\u0026space;g\\biggl(\\begin{bmatrix}\u0026space;a\u0026space;\\\\\u0026space;b\u0026space;\\\\\u0026space;c\u0026space;\\\\\u0026space;\\end{bmatrix}\\biggl)\u0026space;=\u0026space;\\biggl(\\begin{bmatrix}\u0026space;g(a)\u0026space;\\\\\u0026space;g(b)\u0026space;\\\\\u0026space;g(c)\u0026space;\\\\\u0026space;\\end{bmatrix}\\bigg)$.)\n\n# Example \n\nFor examples, please refer to the `\u003cframework\u003e_example.ipynb` files.  \n\nOr just copy the code: \n\n\n## PyTorch implementation:\n```python\n\nclass CancelOut(nn.Module):\n    '''\n    CancelOut Layer\n    \n    x - an input data (vector, matrix, tensor)\n    '''\n    def __init__(self,inp, *kargs, **kwargs):\n        super(CancelOut, self).__init__()\n        self.weights = nn.Parameter(torch.zeros(inp,requires_grad = True) + 4)\n    def forward(self, x):\n        return (x * torch.sigmoid(self.weights.float()))\n\n```\n## Keras/TensorFlow implementation:\n\n```python\nclass CancelOut(keras.layers.Layer):\n    '''\n    CancelOut Layer\n    '''\n    def __init__(self, activation='sigmoid', cancelout_loss=True, lambda_1=0.002, lambda_2=0.001):\n        super(CancelOut, self).__init__()\n        self.lambda_1 = lambda_1\n        self.lambda_2 = lambda_2\n        self.cancelout_loss = cancelout_loss\n        \n        if activation == 'sigmoid': self.activation = tf.sigmoid\n        if activation == 'softmax': self.activation = tf.nn.softmax\n\n    def build(self, input_shape):\n        self.w = self.add_weight(\n            shape=(input_shape[-1],),\n            initializer=tf.keras.initializers.Constant(1),\n            trainable=True)\n        \n    def call(self, inputs):\n        if self.cancelout_loss:\n            self.add_loss( self.lambda_1 * tf.norm(self.w, ord=1) + self.lambda_2 * tf.norm(self.w, ord=2))\n        return tf.math.multiply(inputs, self.activation(self.w))\n    \n    def get_config(self):\n        return {\"activation\": self.activation}    \n```\n\n#  * Work in progress. *\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funnir%2Fcancelout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funnir%2Fcancelout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funnir%2Fcancelout/lists"}