https://github.com/avik-pal/pruning_neural_networks
Solution for the For Ai Pruning Challenge
Science Score: 23.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
1 of 1 committers (100.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (9.9%) to scientific vocabulary
Repository
Solution for the For Ai Pruning Challenge
Basic Info
- Host: GitHub
- Owner: avik-pal
- Language: Python
- Default Branch: master
- Size: 415 KB
Statistics
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Pruning Challenge
Problem Description
We have a simple 3 hidden layered neural network. All the layers are a simple linear layer without any bias, i.e., a simple matmul and followed by relu activation.
python
out = tf.maximum(tf.matmul(x, W), 0.0)
The entire neural network can be simply described as:
python
out1 = tf.maximum(tf.matmul(inp, W1), 0.0)
out2 = tf.maximum(tf.matmul(out1, W2), 0.0)
out3 = tf.maximum(tf.matmul(out2, W3), 0.0)
out4 = tf.maximum(tf.matmul(out3, W4), 0.0)
out5 = tf.maximum(tf.matmul(out4, W5), 0.0)
Now we have 2 pruning strategies - 1. Weight Pruning 2. Neuron/Unit Pruning
We need to compare the two strategies based on the accuracy attained while training the model on MNIST.
Steps to Reproduce Results
```bash
Create a virtual environment
python3 -m virtualenv pruning
Activate the environment
source pruning/bin/activate
Install the dependencies
pip install -r requirements.txt ```
The pruning step is expensive and the models are trained for 100 epochs, so it is recommended to carry out the training in a GPU/TPU environment.
```bash
Train the models
python main.py
To get the plots
python plotting.py ```
Don't alter the directories generated by main.py, plotting.py needs them in the exact order.
NOTE: We train 2 models at a time. So it is recommended to have sufficient GPU memory.
You can use train.py to train individual models. For the instructions use python train.py --help. This will list the arguments it needs.
If you want to train 1 model at a time use the main_alternate.py script.
The results will be logged using tensorboard, so they can be viewed by using tensorboard --logdir logs
Results
Accuracy vs Epoch Plots
|
|
|
|:-:|:-:|
|
|
|
Accuracy vs Sparsity
|
|
|
|:-:|:-:|
|
|
|
Conclusions
From the accuracy curves it is clearly visible that we should always prefer using
Weight PruningoverNeuron Pruning. However, it should be duly noted that Weight Pruning is a very expensive process. And the total training time for this is twice of that of the Neuron Pruning. However, the inference time for both is the same. So if we are able to sacrifice some training time we get some strong performance for Weight Pruning.Making the networks sparse allow us to store the weights much more efficiently (in terms of space). Also it hardly affects inference time compared to dense models.
In Weight Pruning, we drop the weights which have the least absolute value. This is intuitive because those weights were not affecting the output much compared to the other neurons. Hence those weights were not carrying too much valuable information. So dropping them results in the other weights easily capture the information contained in them. Though the above plots might lead us into believing that we can indefinitely (ofcourse within reasonable bounds) increase sparsity without affecting performance. This is indeed incorrect. On furthur increasing the sparsity our result is as follows:
|
|
|
|:-:|:-:|
In Neuron Pruning, dropping an entire neuron affects the model much more critically. Since we are making the value of a neuron zero, we are stopping the flow of a lot of information. Because, every layer loses
k%of its neurons, the effective sparsity in this case is much larger. In case of weight pruning, making individual weights zero doesn't lead to a lot of information loss while propagation because effectively the value of the neurons in each hidden layer does not change much, so it does not affect the following layer.Also if inference time is not of much concern, we can convert the dense weights to sparse tensors, this will allow us to save a lot of memory without sacrificing much speed. The conversion is pretty simple as well:
```python
W1 is a Dense Tensor
SW1 = tf.contrib.layers.densetosparse(W1)
Of course we need to take care of the dimensions of x.
If we time it we see only a small speed bump.
tf.sparse.matmul(SW1, x) ```
Owner
- Name: Avik Pal
- Login: avik-pal
- Kind: user
- Location: Cambridge, MA
- Company: Massachusetts Institute of Technology
- Website: https://avik-pal.github.io
- Twitter: avikpal1410
- Repositories: 46
- Profile: https://github.com/avik-pal
PhD Student @mit || Prev: BTech CSE IITK
GitHub Events
Total
Last Year
Committers
Last synced: over 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Avik Pal | a****l@i****n | 5 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: over 1 year ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0