jax.numpy.unpackbits#
- jax.numpy.unpackbits(a, axis=None, count=None, bitorder='big')[原始碼]#
解包 uint8 陣列中的位元。
JAX 實作的
numpy.unpackbits()
。- 參數:
a (ArrayLike) – N 維
uint8
型別陣列。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)