Package: keras-hrp
keras-hrpHashed Random Projection layer for TF2/Keras.UsageHashed Random Projections (HRP), binary representations, encoding/decoding for storage(notebook)Generate a HRP layer with a new hyperplaneThe random projection or hyperplane is randomly initialized.
The initial state of the PRNG (random_state) is required (Default: 42) to ensure reproducibility.importkeras_hrpaskhrpimporttensorflowastfBATCH_SIZE=32NUM_FEATURES=64OUTPUT_SIZE=1024# demo inputsinputs=tf.random.normal(shape=(BATCH_SIZE,NUM_FEATURES))# instantiate layerlayer=khrp.HashedRandomProjection(output_size=OUTPUT_SIZE,random_state=42# Default: 42)# run itoutputs=layer(inputs)assertoutputs.shape==(BATCH_SIZE,OUTPUT_SIZE)Instiantiate HRP layer with given hyperplaneimportkeras_hrpaskhrpimporttensorflowastfimportnumpyasnpBATCH_SIZE=32NUM_FEATURES=64OUTPUT_SIZE=1024# demo inputsinputs=tf.random.normal(shape=(BATCH_SIZE,NUM_FEATURES))# create hyperplane as numpy arraymyhyperplane=np.random.randn(NUM_FEATURES,OUTPUT_SIZE)# instantiate layerlayer=khrp.HashedRandomProjection(hyperplane=myhyperplane)# run itoutputs=layer(inputs)assertoutputs.shape==(BATCH_SIZE,OUTPUT_SIZE)Serialize Boolean to Int8Python stores 1-bit boolean values always as 8-bit integers or 1-byte.
Some database technologies behave in similar way, and use up 8x-times of the theoretically required storage space (e.g., Postgresbooleanuses 1-byte instead of 1-bit).
In order to save memory or storage space, chuncks of 8 boolean vector elements can be transformed into one 1-byte int8 number.importkeras_hrpaskhrpimportnumpyasnp# given boolean valueshashvalues=np.array([1,0,1,0,1,1,0,0])# serialize boolean to int8serialized=khrp.bool_to_int8(hashvalues)# deserialize int8 to booleandeserialized=khrp.int8_to_bool(serialized)# checknp.testing.assert_array_equal(deserialized,hashvalues)AppendixInstallationThekeras-hrpgit repois available asPyPi packagepipinstallkeras-hrp
pipinstallgit+ssh://git@github.com/ulf1/keras-hrp.gitInstall a virtual environmentpython3-mvenv.venvsource.venv/bin/activate
pipinstall--upgradepip
pipinstall-rrequirements.txt--no-cache-dir
pipinstall-rrequirements-dev.txt--no-cache-dir
pipinstall-rrequirements-demo.txt--no-cache-dir(If your git repo is stored in a folder with whitespaces, then don't use the subfolder.venv. Use an absolute path without whitespaces.)Python commandsJupyter for the examples:jupyter labCheck syntax:flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')Run Unit Tests:PYTHONPATH=. pytestPublish# pandoc README.md --from markdown --to rst -s -o README.rstpythonsetup.pysdisttwineupload-rpypidist/*Clean upfind.-typef-name"*.pyc"|xargsrm
find.-typed-name"__pycache__"|xargsrm-r
rm-r.pytest_cache
rm-r.venvSupportPleaseopen an issuefor support.ContributingPlease contribute usingGithub Flow. Create a branch, add commits, andopen a pull request.AcknowledgementsThe "Evidence" project was funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) -433249742(GU 798/27-1; GE 1119/11-1).Maintenancetill 31.Aug.2023 (v0.1.0) the code repository was maintained within the DFG project433249742since 01.Sep.2023 (v0.2.0) the code repository is maintained by@ulf1.CitationPlease cite the arXiv Preprint when using this software for any purpose.@misc{hamster2023rediscovering,
      title={Rediscovering Hashed Random Projections for Efficient Quantization of Contextualized Sentence Embeddings}, 
      author={Ulf A. Hamster and Ji-Ung Lee and Alexander Geyken and Iryna Gurevych},
      year={2023},
      eprint={2304.02481},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}