{"id":20029268,"url":"https://github.com/mathialo/parsnet","last_synced_at":"2026-05-10T13:44:57.895Z","repository":{"id":57450881,"uuid":"156849671","full_name":"mathialo/parsnet","owner":"mathialo","description":"TensorFlow implementation of the constraints necessary for Parseval Networks","archived":false,"fork":false,"pushed_at":"2019-04-28T19:42:50.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T21:02:28.403Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathialo.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":"2018-11-09T10:55:07.000Z","updated_at":"2023-03-01T21:55:28.000Z","dependencies_parsed_at":"2022-09-26T17:31:37.267Z","dependency_job_id":null,"html_url":"https://github.com/mathialo/parsnet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathialo%2Fparsnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathialo%2Fparsnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathialo%2Fparsnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathialo%2Fparsnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathialo","download_url":"https://codeload.github.com/mathialo/parsnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241460185,"owners_count":19966519,"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":[],"created_at":"2024-11-13T09:19:30.718Z","updated_at":"2026-05-10T13:44:52.847Z","avatar_url":"https://github.com/mathialo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parsnet\nTensorFlow implementation of the constraints necessary for [Parseval networks](https://arxiv.org/abs/1704.08847).\n\nParseval networks constrain the weight matrices of neural networks to be tight frames, so that the Lipschitz constant of the entire network is \u003c= 1. This makes the entire network a contraction, and limits the amount an adversarial perturbation can propagate through the network. For an in-depth introduction to this regularization technique, consult [the original article](https://arxiv.org/pdf/1704.08847.pdf).\n\n\n## Installation\nYou can install the `parsnet` library from the Python Package Index:\n``` bash\n$ pip install parsnet\n```\nor by cloning this repository and installing manually:\n``` bash\n$ git clone https://github.com/mathialo/parsnet.git\n$ cd parsnet\n$ pip install .\n```\n\n\n## Example use\nUsing the `parsnet` package is very easy. Simply import `parsnet` and use `parsnet.constraints.tight_frame` for the [`kernel_constraint`](https://www.tensorflow.org/api_docs/python/tf/layers/conv2d#arguments) keyword argument on your layers of choice:\n``` python\nimport tensorflow as tf\nimport parsnet\n\n\nimg_size = (32, 32, 3)\nbatch_size = 512\nretraction_par = 0.001\nnum_passes = 1\n\ninput_layer = tf.placeholder(tf.float32, shape=(batch_size, *img_size))\n\nlayer1 = tf.layers.conv2d(\n    inputs=input_layer,\n    kernel_size=(5, 5),\n    filters=64,\n    strides=(1, 1),\n    padding=\"SAME\",\n    activation=tf.nn.relu,\n    kernel_initializer=tf.initializers.orthogonal(),\n    name=\"convlayer1\",\n\n    # Applying Parseval constraint:\n    kernel_constraint=parsnet.constraints.tight_frame(retraction_par, num_passes)\n)\n\n...\n\n```\n\nSince the Parseval contraint limits the weight matrices to have orthonormal rows, we recommend using the `tf.initializers.orthogonal` initializer to ensure that this criteria is met when the network is initialized. \n\nIf you want to do residual blocks, you must use convex combinations instead of simple additions in order for the block to be a contraction. The `parsnet.nn.convex_add` method implements this with either a fixed or a trainable convex parameter:\n``` python\ndef res_block(input_layer):\n    layer1 = tf.layers.conv2d(\n        inputs=input_layer,\n        kernel_size=(3, 3),\n        filters=64,\n        strides=(1, 1),\n        padding=\"SAME\",\n        activation=tf.nn.relu,\n        kernel_initializer=tf.initializers.orthogonal(),\n        kernel_constraint=parsnet.constraints.tight_frame(0.001)\n    )\n    layer2 = tf.layers.conv2d(\n        inputs=layer1,\n        kernel_size=(3, 3),\n        filters=128,\n        strides=(1, 1),\n        padding=\"SAME\",\n        activation=tf.nn.relu,\n        kernel_initializer=tf.initializers.orthogonal(),\n        kernel_constraint=parsnet.constraints.tight_frame(0.001)\n    )    \n    layer3 = tf.layers.conv2d(\n        inputs=layer2,\n        kernel_size=(3, 3),\n        filters=64,\n        strides=(1, 1),\n        padding=\"SAME\",\n        activation=tf.nn.relu,\n        kernel_initializer=tf.initializers.orthogonal(),\n        kernel_constraint=parsnet.constraints.tight_frame(0.001)\n    )\n\n    return parsnet.nn.convex_add(input_layer, layer3, \n        initial_convex_par=0.5,\n        trainable=True\n    )\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathialo%2Fparsnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathialo%2Fparsnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathialo%2Fparsnet/lists"}