-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e2f924
commit 7f90603
Showing
6 changed files
with
152 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,90 @@ | ||
# EdgeML FastCells on a sample public dataset | ||
|
||
This directory includes example notebook and general execution script of | ||
FastCells (FastRNN & FastGRNN) developed as part of EdgeML along with modified | ||
This directory includes example notebooks and scripts of | ||
FastCells (FastRNN & FastGRNN) along with modified | ||
UGRNN, GRU and LSTM to support the LSQ training routine. | ||
Also, we include a sample cleanup and use-case on the USPS10 public dataset. | ||
|
||
`edgeml_pytorch.graph.rnn` implements the custom RNN cells of **FastRNN** ([`FastRNNCell`](../../pytorch_edgeml/graph/rnn.py#L226)) and **FastGRNN** ([`FastGRNNCell`](../../pytorch_edgeml/graph/rnn.py#L80)) with | ||
multiple additional features like Low-Rank parameterisation, custom | ||
non-linearities etc., Similar to Bonsai and ProtoNN, the three-phase training | ||
routine for FastRNN and FastGRNN is decoupled from the custom cells to | ||
facilitate a plug and play behaviour of the custom RNN cells in other | ||
architectures (NMT, Encoder-Decoder etc.,) in place of the inbuilt `RNNCell`, `GRUCell`, `BasicLSTMCell` etc., | ||
`edgeml_pytorch.graph.rnn` also contains modified RNN cells of **UGRNN** ([`UGRNNLRCell`](../../pytorch_edgeml/graph/rnn.py#L742)), | ||
**GRU** ([`GRULRCell`](../../edgeml/graph/rnn.py#L565)) and **LSTM** ([`LSTMLRCell`](../../pytorch_edgeml/graph/rnn.py#L369)). These cells also can be substituted for FastCells where ever feasible. | ||
|
||
`edgeml_pytorch.graph.rnn` also contains fully wrapped RNNs which are equivalent to `nn.LSTM` and `nn.GRU`. Implemented cells: | ||
**FastRNN** ([`FastRNN`](../../pytorch_edgeml/graph/rnn.py#L968)), **FastGRNN** ([`FastGRNN`](../../pytorch_edgeml/graph/rnn.py#L993)), **UGRNN** ([`UGRNN`](../../edgeml_pytorch/graph/rnn.py#L945)), **GRU** ([`GRU`](../../edgeml/graph/rnn.py#L922)) and **LSTM** ([`LSTM`](../../pytorch_edgeml/graph/rnn.py#L899)). | ||
|
||
Note that all the cells and wrappers (when used independently from `fastcell_example.py` or `edgeml_pytorch.trainer.fastTrainer`) take in data in a batch first format ie., [batchSize, timeSteps, inputDims] by default but it can also support [timeSteps, batchSize, inputDims] format by setting `batch_first` argument to False when used. `fast_example.py` automatically takes care it while assuming the standard format between tf, c++ and pytorch. | ||
There is also a sample cleanup and train/test script for the USPS10 public dataset. | ||
|
||
[`edgeml_pytorch.graph.rnn`](../../../pytorch/pytorch_edgeml/graph/rnn.py) | ||
provides two RNN cells **FastRNNCell** and **FastGRNNCell** with additional | ||
features like low-rank parameterisation and custom non-linearities. Akin to | ||
Bonsai and ProtoNN, the three-phase training routine for FastRNN and FastGRNN | ||
is decoupled from the custom cells to facilitate a plug and play behaviour of | ||
the custom RNN cells in other architectures (NMT, Encoder-Decoder etc.). | ||
Additionally, numerically equivalent CUDA-based implementations FastRNNCuda | ||
and FastGRNNCuda are provided for faster training. | ||
`edgeml_pytorch.graph.rnn` also contains modified RNN cells of **UGRNNCell**, | ||
**GRUCell**, and **LSTMCell**, which can be substituted for Fast(G)RNN, | ||
as well as untrolled RNNs which are equivalent to `nn.LSTM` and `nn.GRU`. | ||
|
||
Note that all the cells and wrappers, when used independently from `fastcell_example.py` | ||
or `edgeml_pytorch.trainer.fastTrainer`, take in data in a batch first format, i.e., | ||
[batchSize, timeSteps, inputDims] by default, but can also support [timeSteps, | ||
batchSize, inputDims] format if `batch_first` argument is set to False. | ||
`fast_example.py` automatically adjusts to the correct format across tf, c++ and pytorch. | ||
|
||
For training FastCells, `edgeml_pytorch.trainer.fastTrainer` implements the three-phase | ||
FastCell training routine in PyTorch. A simple example, | ||
`examples/fastcell_example.py` is provided to illustrate its usage. | ||
|
||
Note that `fastcell_example.py` assumes that data is in a specific format. It | ||
is assumed that train and test data is contained in two files, `train.npy` and | ||
`test.npy`. Each containing a 2D numpy array of dimension `[numberOfExamples, | ||
FastCell training routine in PyTorch. A simple example `fastcell_example.py` is provided | ||
to illustrate its usage. Note that `fastcell_example.py` assumes that data is in a specific format. | ||
It is assumed that train and test data is contained in two files, `train.npy` and | ||
`test.npy`, each containing a 2D numpy array of dimension `[numberOfExamples, | ||
numberOfFeatures]`. numberOfFeatures is `timesteps x inputDims`, flattened | ||
across timestep dimension. So the input of 1st timestep followed by second and | ||
so on. For an N-Class problem, we assume the labels are integers from 0 | ||
across timestep dimension with the input of the first time step followed by the second | ||
and so on. For an N-Class problem, we assume the labels are integers from 0 | ||
through N-1. Lastly, the training data, `train.npy`, is assumed to well shuffled | ||
as the training routine doesn't shuffle internally. | ||
|
||
**Tested With:** PyTorch = 1.1 with Python 3.6 | ||
|
||
## Download and clean up sample dataset | ||
|
||
We will be testing out the validation of the code by using the USPS dataset. | ||
The download and cleanup of the dataset to match the above-mentioned format is | ||
done by the script [fetch_usps.py](fetch_usps.py) and | ||
To validate the code with USPS dataset, first download and format the dataset to match | ||
the required format using the script [fetch_usps.py](fetch_usps.py) and | ||
[process_usps.py](process_usps.py) | ||
|
||
``` | ||
python fetch_usps.py | ||
python process_usps.py | ||
``` | ||
|
||
Note: Even though usps10 is not a time-series dataset, it can be regarding as a time-series | ||
dataset where time step sees a new row. So the number of timesteps = 16 and inputDims = 16. | ||
|
||
## Sample command for FastCells on USPS10 | ||
The following sample run on usps10 should validate your library: | ||
|
||
Note: Even though usps10 is not a time-series dataset, it can be assumed as, a time-series where each row is coming in at one single time. | ||
So the number of timesteps = 16 and inputDims = 16 | ||
The following is a sample run on usps10 : | ||
|
||
```bash | ||
python fastcell_example.py -dir usps10/ -id 16 -hd 32 | ||
``` | ||
This command should give you a final output screen which reads roughly similar to (might not be exact numbers due to various version mismatches): | ||
This command should give you a final output that reads roughly similar to | ||
(might not be exact numbers due to various version mismatches): | ||
|
||
``` | ||
Maximum Test accuracy at compressed model size(including early stopping): 0.9407075 at Epoch: 262 | ||
Final Test Accuracy: 0.93721974 | ||
Non-Zeros: 1932 Model Size: 7.546875 KB hasSparse: False | ||
``` | ||
`usps10/` directory will now have a consolidated results file called `FastRNNResults.txt` or `FastGRNNResults.txt` depending on the choice of the RNN cell. | ||
A directory `FastRNNResults` or `FastGRNNResults` with the corresponding models with each run of the code on the `usps10` dataset. | ||
`usps10/` directory will now have a consolidated results file called `FastRNNResults.txt` or | ||
`FastGRNNResults.txt` depending on the choice of the RNN cell. A directory `FastRNNResults` or | ||
`FastGRNNResults` with the corresponding models with each run of the code on the `usps10` dataset. | ||
|
||
Note that the scalars like `alpha`, `beta`, `zeta` and `nu` are all before the application of the sigmoid function over them. | ||
Note that the scalars like `alpha`, `beta`, `zeta` and `nu` correspond to the values before | ||
the application of the sigmoid function. | ||
|
||
## Byte Quantization(Q) for model compression | ||
If you wish to quantize the generated model to use byte quantized integers use `quantizeFastModels.py`. Usage Instructions: | ||
If you wish to quantize the generated model, use `quantizeFastModels.py`. Usage Instructions: | ||
|
||
``` | ||
python quantizeFastModels.py -h | ||
``` | ||
|
||
This will generate quantized models with a suffix of `q` before every param stored in a new directory `QuantizedFastModel` inside the model directory. | ||
One can use this model further on edge devices. | ||
This will generate quantized models with a suffix of `q` before every param stored in a | ||
new directory `QuantizedFastModel` inside the model directory. | ||
|
||
Note that the scalars like `qalpha`, `qbeta`, `qzeta` and `qnu` are all after the application of the sigmoid function over them and quantization, they can be directly plugged into the inference pipleines. | ||
Note that the scalars like `qalpha`, `qbeta`, `qzeta` and `qnu` correspond to values | ||
after the application of the sigmoid function over them post quantization; | ||
they can be directly plugged into the inference pipleines. | ||
|
||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
||
Licensed under the MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.