https://github.com/karenullrich/pytorch-binary-converter
Turning float tensors to binary tensors according to IEEE-754 standard.
https://github.com/karenullrich/pytorch-binary-converter
binary floating-point ieee-754 ieee754 pytorch
Last synced: about 1 month ago
JSON representation
Turning float tensors to binary tensors according to IEEE-754 standard.
- Host: GitHub
- URL: https://github.com/karenullrich/pytorch-binary-converter
- Owner: KarenUllrich
- License: gpl-3.0
- Created: 2019-06-17T12:36:18.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-17T16:03:58.000Z (over 6 years ago)
- Last Synced: 2025-08-03T04:42:37.566Z (2 months ago)
- Topics: binary, floating-point, ieee-754, ieee754, pytorch
- Language: Python
- Size: 29.3 KB
- Stars: 37
- Watchers: 1
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Binary Converter
This is a tool to turn pytorch's floats into binary tensors and back.
This code converts tensors of floats or bits into the respective other.
We use the IEEE-754 guideline [1] to convert. The default for conversion are
based on 32 bit / single precision floats: 8 exponent bits and 23 mantissa bits.
Other common formats are|num total bits | precision | exponent bits | mantissa bits | bias |
|------------|-------------------|-------------------|-------------------|-------------------|
|64 bits | double | 11 | 52 | 1023|
| 32 bits | single | 8 | 23 | 127|
| 16 bits | half | 5 | 10 | 15|### Usage
To turn a float tensor into a binary one
from binary_converter import float2bit
binary_tensor = float2bit(float_tensor, num_e_bits=8, num_m_bits=23, bias=127.)To turn a binary tensor into a float one
from binary_converter import bit2float
float_tensor = bit2float(binary_tensor, num_e_bits=8, num_m_bits=23, bias=127.)### Requirements
This code has been tested with
- `python 3.6`
- `pytorch 1.1.0`### Maintenance
Please be warned that this repository is not going to be maintained regularly.
### References
[1] IEEE Computer Society (2008-08-29). IEEE Standard for Floating-Point
Arithmetic. IEEE Std 754-2008. IEEE. pp. 1–70. doi:10.1109/IEEESTD.2008.4610935.
ISBN 978-0-7381-5753-5. IEEE Std 754-2008