jax.numpy.unpackbits#

jax.numpy.unpackbits(a, axis=None, count=None, bitorder='big')[源代码]#

解包 uint8 数组中的位。

JAX 实现的 numpy.unpackbits()

参数:
  • a (ArrayLike) – 类型为 uint8 的 N 维数组。

  • axis (int | None) – 可选轴,沿该轴解包。 如果未指定,则 a 将被展平

  • count (int | None) – 指定要解包的位数(如果为正),或者从末尾修剪的位数(如果为负)。

  • bitorder (str) – "big" (默认) 或 "little": 指定位顺序是大端还是小端。

返回:

解包后的位的 uint8 数组。

返回类型:

Array

另请参阅

示例

从标量解包位

>>> 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)