Skip to main content

Drop Connect

The paper DropConnectPapper introduces a regularization technique that is similar to Dropout, but instead of dropping out individual units, it drops out individual connections between units. This is done by applying a mask to the weights of the network, which is sampled from a Bernoulli distribution.

DropConnectImage

Installing

pip install dropconnect

Usage

from torch import Tensor
from dropconnect import Dropconnect

layer = Dropconnect(in_features=5, out_features=10, bias=True, p=0.5)
input = Tensor([[1,2,3,4,5],[2,3,4,5,6]])
output = layer(input)
print(output) # Can be used just like a drop-in replacement for linear layer.

Training

Let $X \in \mathbb{R}^{n \times d}$ a tensor with $n$ examples and $d$ features a $W \in \mathbb{R}^{l \times d}$ a tensor of weights.

For training, a mask matrix $M$ is created from a Bernoulli distribution to mask elements of the weight matrix $W$ , using the Hadamard product, in order to drop neuron connections instead of turning off neurons like in dropout

For a single example, the implementation is straightforward, just apply a mask $M$ to a weight tensor $W$. However, according to the paper: "A key component to successfully training with DropConnect is the selection of a different mask for each training example. Selecting a single mask for a subset of training examples, such as a mini-batch of 128 examples, does not regularize the model enough in practice."

Therefore, a mask tensor $M \in \mathbb{R}^{n \times l \times d}$ must be chosen, so the linear layer with DropConnect should be implemented as:

$$ \text{DropConnect}(X, W, M) = \begin{bmatrix} \frac{1}{1-p}\begin{bmatrix} x^1{}_1 & x^1{}_2 & \cdots & x^1{}_d \end{bmatrix} \left(\begin{bmatrix} m^{11}{}_1 & m^{11}{}_2 & \cdots & m^{11}{}_l \ m^{12}{}_1 & m^{12}{}_2 & \cdots & m^{12}{}_l \ \vdots & \vdots & \ddots & \vdots \ m^{1d}{}_1 & m^{1d}{}_2 & \cdots & m^{1d}{}_l \ \end{bmatrix} \odot \begin{bmatrix} w^1{}_1 & w^1{}_2 & \cdots & w^1{}_l \ w^2{}_1 & w^2{}_2 & \cdots & w^2{}_l \ \vdots & \vdots & \ddots & \vdots \ w^d{}_1 & w^d{}_2 & \cdots & w^d{}_l \ \end{bmatrix} \right) \ \ \ \vdots \ \ \frac{1}{1-p}\begin{bmatrix} x^n{}_1 & x^n{}_2 & \cdots & x^n{}_d \end{bmatrix} \left(\begin{bmatrix} m^{n1}{}_1 & m^{n1}{}_2 & \cdots & m^{n1}{}_l \ m^{n2}{}_1 & m^{n2}{}_2 & \cdots & m^{n2}{}_l \ \vdots & \vdots & \ddots & \vdots \ m^{nd}{}_1 & m^{nd}{}_2 & \cdots & m^{nd}{}_l \ \end{bmatrix} \odot \begin{bmatrix} w^1{}_1 & w^1{}_2 & \cdots & w^1{}_l \ w^2{}_1 & w^2{}_2 & \cdots & w^2{}_l \ \vdots & \vdots & \ddots & \vdots \ w^d{}_1 & w^d{}_2 & \cdots & w^d{}_l \ \end{bmatrix} \right) \ \end{bmatrix} $$

Backpropagation

In order to update the weight matrix $W$ in a DropConnect layer, the mask is applied to the gradient to update only those elements that were active in the forward pass. but this is already done by the automatic differentiation in Pytorch, since if $J$ is the gradient coming from the linear operation, the gradient propagated by the Hadamard product with respect to $W$ will be:

$$ J \odot M $$

So there is no need to implement an additional backpropagation operation, and only the Hadamard product already provided by Pytorch is needed.

Release files for dropconnect 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dropconnect 0.1.2
File Size Uploaded
dropconnect-0.1.2.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dropconnect 0.1.2
File Interpreter ABI Platform
dropconnect-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / dropconnect-0.1.2.tar.gz

Download URL dropconnect-0.1.2.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7c923f2a06dbb08011322b09b87fb0afcee356ec088a19df33480d3584264ddd
BLAKE2b-256 checksum
How to use checksums
cd657f46f84b865335ea0d1358ab43686acc26eac3c5d93b60fd7f387e88317f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.12.1 Linux/6.11.0-1012-azure

Release files / dropconnect-0.1.2-py3-none-any.whl

Download URL dropconnect-0.1.2-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b806e6ea329a5725ea13c95b52b5ed8a3583832e7aee8dedd8b8ee423e487831
BLAKE2b-256 checksum
How to use checksums
a6b0a4ae637e22074bd6cb1de9dd95cdb9b5b708d9fff65ba1ac4a4e970c81cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.12.1 Linux/6.11.0-1012-azure

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page