jax.numpy.unpackbits#
- jax.numpy.unpackbits(a, axis=None, count=None, bitorder='big')[源代码]#
解压 uint8 数组中的位。
numpy.unpackbits()
的 JAX 实现。- 参数:
a (类数组) – 类型为
uint8
的 N 维数组。axis (int | None) – 可选的解包轴。如果未指定,则
a
将被展平count (int | None) – 指定要解包的位数(如果为正数),或者从末尾修剪的位数(如果为负数)。
bitorder (str) –
"big"
(默认)或"little"
:指定位顺序是大端还是小端。
- 返回:
一个解包后的 uint8 位数组。
- 返回类型:
另请参阅
jax.numpy.packbits()
:这是unpackbits
的逆操作。
示例
从标量解包位
>>> jnp.unpackbits(jnp.uint8(27)) # big-endian by default Array([0, 0, 0, 1, 1, 0, 1, 1], dtype=uint8) >>> jnp.unpackbits(jnp.uint8(27), bitorder="little") Array([1, 1, 0, 1, 1, 0, 0, 0], dtype=uint8)
将其与 Python 的二进制表示进行比较
>>> 0b00011011 27
沿轴解包位
>>> vals = jnp.array([[154], ... [ 49]], dtype='uint8') >>> bits = jnp.unpackbits(vals, axis=1) >>> bits Array([[1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 0, 1]], dtype=uint8)
使用
packbits()
来反转此操作>>> jnp.packbits(bits, axis=1) Array([[154], [ 49]], dtype=uint8)
在并非所有位都存在的情况下,
count
关键字允许unpackbits
作为packbits
的逆操作>>> bits = jnp.array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1]) # 11 bits >>> vals = jnp.packbits(bits) >>> vals Array([219, 96], dtype=uint8) >>> jnp.unpackbits(vals) # 16 zero-padded bits Array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0], dtype=uint8) >>> jnp.unpackbits(vals, count=11) # specify 11 output bits Array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1], dtype=uint8) >>> jnp.unpackbits(vals, count=-5) # specify 5 bits to be trimmed Array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1], dtype=uint8)