No project description provided
Project description
ENN Ragged Buffer
This Python package implements an efficient RaggedBuffer
datatype that is similar to
a 3D numpy array, but which allows for variable sequence length in the second
dimension. It was created primarily for use in ENN-PPO
and currently only supports a small selection of the numpy array methods.
User Guide
Install the package with pip install ragged-buffer
.
The package currently supports two RaggedBuffer
variants, RaggedBufferF32
(storing float32 values) and RaggedBufferI64
(storing int64 values).
Creating a RaggedBuffer
There are three ways to create a RaggedBuffer
:
RaggedBufferF32(features: int)
creates an emptyRaggedBuffer
with the specified number of features.RaggedBufferF32.from_flattened(flattened: np.ndarray, lenghts: np.ndarray)
creates aRaggedBuffer
from a flattened 2D numpy array and a 1D numpy array of lengths.RaggedBufferF32.from_array
creates aRaggedBuffer
(with equal sequence lenghts) from a 3D numpy array.
Creating an empty buffer and pushing each row:
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create an empty RaggedBuffer with a feature size of 3
buffer = RaggedBufferF32(3)
# Push sequences with 3, 5, 0, and 1 elements
buffer.push(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32))
buffer.push(np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]], dtype=np.float32))
buffer.push(np.array([], dtype=np.float32))
buffer.push(np.array([[25, 25, 27]], dtype=np.float32))
Creating a RaggedBuffer from a flat 2D numpy array which combines the first and second dimension, and an array of sequence lengths:
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_flattened(
np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 25, 27]], dtype=np.float32),
np.array([3, 5, 0, 1], dtype=np.int64))
)
Creating a RaggedBuffer from a 3D numpy array (all sequences have the same length):
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
Get size
The size0
, size1
, and size2
methods return the number of sequences, the number of elements in a sequence, and the number of features respectively.
import numpy as np
from ragged_buffer import RaggedBufferF32
buffer = RaggedBufferF32.from_flattened(
np.zeros((9, 64), dtype=np.float32),
np.array([3, 5, 0, 1], dtype=np.int64))
)
# Get size of the first/batch dimension.
assert buffer.size0() == 10
# Get size of individual sequences.
assert buffer.size1(1) == 5
assert buffer.size1(2) == 0
# Get size of the last/feature dimension.
assert buffer.size2() == 64
Convert to numpy array
as_aray
converts a RaggedBuffer
to a flat 2D numpy array that combines the first and second dimension.
import numpy as np
from ragged_buffer import RaggedBufferI64
buffer = RaggedBufferI64(1)
buffer.push(np.array([[1], [1], [1]], dtype=np.int64))
buffer.push(np.array([[2], [2]], dtype=np.int64))
assert np.all(buffer.as_array(), np.array([[1], [1], [1], [2], [2]], dtype=np.int64))
Indexing
You can index a RaggedBuffer
with a single integer (returning a RaggedBuffer
with a single sequence), or with a numpy array of integers selecting/permuting multiple sequences.
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create a new `RaggedBufferF32`
buffer = RaggedBufferF32.from_flattened(
np.arange(0, 40, dtype=np.float32).reshape(10, 4),
np.array([3, 5, 0, 1], dtype=np.int64)
)
# Retrieve the first sequence.
assert np.all(
buffer[0].as_array() ==
np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], dtype=np.float32)
)
# Get a RaggedBatch with 2 randomly selected sequences.
buffer[np.random.permutation(4)[:2]]
Addition
You can add two RaggedBuffer
s with the +
operator if they have the same number of sequences, sequence lengths, and features. You can also add a RaggedBuffer
where all sequences have a length of 1 to a RaggedBuffer
with variable length sequences, broadcasting along each sequence.
import numpy as np
from ragged_buffer import RaggedBufferF32
# Create ragged buffer with dimensions (3, [1, 3, 2], 1)
rb3 = RaggedBufferI64(1)
rb3.push(np.array([[0]], dtype=np.int64))
rb3.push(np.array([[0], [1], [2]], dtype=np.int64))
rb3.push(np.array([[0], [5]], dtype=np.int64))
# Create ragged buffer with dimensions (3, [1, 1, 1], 1)
rb4 = RaggedBufferI64.from_array(np.array([0, 3, 10], dtype=np.int64).reshape(3, 1, 1))
# Add rb3 and rb4, broadcasting along the sequence dimension.
rb5 = rb3 + rb4
assert np.all(
rb5.as_array() == np.array([[0], [3], [4], [5], [10], [15]], dtype=np.int64)
)
Concatenation
The extend
method can be used to mutate a RaggedBuffer
by appending another RaggedBuffer
to it.
import numpy as np
from ragged_buffer import RaggedBufferF32
rb1 = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
rb2 = RaggedBufferF32.from_array(np.zeros((2, 5, 3), dtype=np.float32))
rb1.extend(r2)
assert rb1.size0() == 6
Clear
The clear
method removes all elements from a RaggedBuffer
without deallocating the underlying memory.
import numpy as np
from ragged_buffer import RaggedBufferF32
rb = RaggedBufferF32.from_array(np.zeros((4, 5, 3), dtype=np.float32))
rb.clear()
assert rb.size0() == 0
License
ENN Ragged Buffer dual-licensed under Apache-2.0 and MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for ragged_buffer-0.2.3-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f73c775b7fd9fdb40e519a6b9f3ddfadc1e86460de23efd1e735dae2e39bc49d |
|
MD5 | 334b81b9291d55125f2324e4c9635aeb |
|
BLAKE2b-256 | d2187205120450fe90e67b954f8af3444667031d3041efb9b05fd4369e6987ff |
Hashes for ragged_buffer-0.2.3-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 546b7a2e6ca878731e28463d008bccfd18dc333f80a57fc70d5635f01e517eb6 |
|
MD5 | 4aecc0859772d39973c7bcbd49f33c6b |
|
BLAKE2b-256 | afb14388c3db3d3dcbb255ed56fca5d89abb25d60c69381c084deeb9429f6d25 |
Hashes for ragged_buffer-0.2.3-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38b5ffd470b3babdf42de269c38a020e877ae01561a468b2c2018fd777f3c98a |
|
MD5 | 56fa845e3c8a0955ff072d5b96ecaab2 |
|
BLAKE2b-256 | db9aba78275ece0cf05da5e28aafa6ac9121feaf33cadf5bb1002d3b32b0f77a |
Hashes for ragged_buffer-0.2.3-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01306558435b69770b1f15313fcbdec06cc60769cd231e8df29a5dd41ce3b5dc |
|
MD5 | 0e357e1d969d0f9ccfd520df237904c1 |
|
BLAKE2b-256 | 6d08535084e83099a6618d82644943fe9f9ecff2112e57de6e2c35df5dbcaf5d |
Hashes for ragged_buffer-0.2.3-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ad5f9a952bce6dcaf2ae19995717aa02b613b8607e508dc6168aef671c8952 |
|
MD5 | bcbb2d6f9620f8006c6c6cfc742ec85a |
|
BLAKE2b-256 | cbd1c0adebc307df55ce128dbc177f78e9c1af4bff8c449ee71afb90a58c9cb9 |
Hashes for ragged_buffer-0.2.3-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3b4ef1ed52db8cbb3794318bf9435bb28dd55f4f5f0c3048117b00f989920a2 |
|
MD5 | 881361091faf4d7346294e8e32265758 |
|
BLAKE2b-256 | d56e5cfdf56709588b2408cb2bc4e112a94cf7e4560cafb16bbec2a27030ae14 |
Hashes for ragged_buffer-0.2.3-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f1a4d3863f330c13dd1b7d6bcf597ad833c851b55320039c59c906050a0552e |
|
MD5 | ef13b84f27487160f03999fe8767e994 |
|
BLAKE2b-256 | 4407da32c857c3320e7d56d2ca74ce8b9558e3970d10f4e759f67c1a90f170e1 |
Hashes for ragged_buffer-0.2.3-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7771ee715e943a4b5fb2b71491d89238abd639db6380a660ce254ced9680d4b9 |
|
MD5 | 5ca7c9d9d68481f96a0b36d6994db3ec |
|
BLAKE2b-256 | 594949b35f5c13ca783539bcc84ee8821789d2a940a8e2dd98d71e65d2073e09 |
Hashes for ragged_buffer-0.2.3-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74e689e23e86e0fee9e82401d9ae8dd8a22b819559767347207d6ed1b63aec6f |
|
MD5 | 233133bf1ed758d4bdbff71eef886186 |
|
BLAKE2b-256 | 859b0f9a557423e1611ede16070c6c20116f51ae6aca3f42b7d426fa6989fb28 |
Hashes for ragged_buffer-0.2.3-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e01ec5a1478eaeff92cb6b9212f6441399f4efa5b85724484cc27cef102acc0 |
|
MD5 | 49e728532433848362a1b9cb3b04d058 |
|
BLAKE2b-256 | b2075313fa5860e1e56d84123880989309c5ca63a8e59b11b0432b778d1cc344 |