跳转到主要内容

用于处理可变长度深度嵌套序列的PyTorch扩展

项目描述



Tests PyPI Coverage License

FoldedTensor: PyTorch扩展,用于处理深度嵌套的可变长度序列

foldedtensor是一个PyTorch扩展,提供对包含深度嵌套可变大小序列的张量的高效处理。它允许根据序列长度的内部结构来展开/折叠(或展开/折叠)数据维度。当处理可以以不同方式分割的数据时,该库特别有用,并允许您避免选择固定的表示。

安装

可以使用pip安装库

pip install foldedtensor

特性

  • 支持任意数量的嵌套维度
  • 处理已填充张量时无计算开销
  • 基于存储的内部长度动态重新填充(或重新折叠)数据
  • 每次张量重新折叠时自动生成和更新掩码
  • C++优化代码,用于快速从Python列表加载数据和折叠
  • 数据表示的灵活性,便于在需要时切换到不同的布局

示例

在最简单的情况下,foldedtensor可以用于将嵌套Python列表转换为PyTorch张量

from foldedtensor import as_folded_tensor

ft = as_folded_tensor(
    [
        [0, 1, 2],
        [3],
    ],
)
# FoldedTensor([[0, 1, 2],
#               [3, 0, 0]])

您也可以在创建时指定名称和平铺/非平铺维度

import torch
from foldedtensor import as_folded_tensor

# Creating a folded tensor from a nested list
# There are 2 samples, the first with 5 lines, the second with 1 line.
# Each line contain between 1 and 2 words.
ft = as_folded_tensor(
    [
        [[1], [], [], [], [2, 3]],
        [[4, 3]],
    ],
    data_dims=("samples", "words"),
    full_names=("samples", "lines", "words"),
    dtype=torch.long,
)
print(ft)
# FoldedTensor([[1, 2, 3],
#               [4, 3, 0]])

创建后,您可以通过折叠来改变张量的形状

# Refold on the lines and words dims (flatten the samples dim)
print(ft.refold(("lines", "words")))
# FoldedTensor([[1, 0],
#               [0, 0],
#               [0, 0],
#               [0, 0],
#               [2, 3],
#               [4, 3]])

# Refold on the words dim only: flatten everything
print(ft.refold(("words",)))
# FoldedTensor([1, 2, 3, 4, 3])

张量可以进一步与标准PyTorch操作一起使用

# Working with PyTorch operations
embedder = torch.nn.Embedding(10, 16)
embedding = embedder(ft.refold(("words",)))
print(embedding.shape)
# torch.Size([5, 16]) # 5 words total, 16 dims

refolded_embedding = embedding.refold(("samples", "words"))
print(refolded_embedding.shape)
# torch.Size([2, 5, 16]) # 2 samples, 5 words max, 16 dims

基准测试

在此处查看与各种替代方案的比较:基准测试文档

与替代方案的比较

与其他的锯齿或嵌套张量实现不同,FoldedTensor不会对嵌套数据进行特定结构的约束,也不需要填充所有维度。这在使用可以按数据转换的不同方式排列的数据时,为用户提供更大的灵活性。此外,C++优化确保了高性能,使其非常适合高效处理深层嵌套张量。

以下是与其他常见处理可变长度嵌套序列实现的比较

特性 嵌套张量 掩码张量 平铺张量
内部数据结构 平铺 填充 任意
最大嵌套级别 1 1
从嵌套Python列表生成
布局转换 到填充 任何
无填充的归约操作

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

foldedtensor-0.3.5.tar.gz (18.4 kB 查看哈希)

上传时间: 源代码

构建分发

foldedtensor-0.3.5-cp312-cp312-win_amd64.whl (83.4 kB 查看哈希)

上传时间: CPython 3.12 Windows x86-64

foldedtensor-0.3.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.3 kB 查看哈希)

上传时间: CPython 3.12 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp312-cp312-macosx_11_0_arm64.whl (83.2 kB 查看哈希)

上传时间: CPython 3.12 macOS 11.0+ ARM64

foldedtensor-0.3.5-cp312-cp312-macosx_10_9_x86_64.whl (86.4 kB 查看哈希)

上传时间: CPython 3.12 macOS 10.9+ x86-64

foldedtensor-0.3.5-cp311-cp311-win_amd64.whl (83.1 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86-64

foldedtensor-0.3.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.7 kB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp311-cp311-macosx_11_0_arm64.whl (84.6 kB 查看哈希值)

上传时间 CPython 3.11 macOS 11.0+ ARM64

foldedtensor-0.3.5-cp311-cp311-macosx_10_9_x86_64.whl (87.2 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ x86-64

foldedtensor-0.3.5-cp310-cp310-win_amd64.whl (81.9 kB 查看哈希值)

上传时间 CPython 3.10 Windows x86-64

foldedtensor-0.3.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (119.1 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp310-cp310-macosx_11_0_arm64.whl (83.3 kB 查看哈希值)

上传时间 CPython 3.10 macOS 11.0+ ARM64

foldedtensor-0.3.5-cp310-cp310-macosx_10_9_x86_64.whl (85.8 kB 查看哈希值)

上传时间 CPython 3.10 macOS 10.9+ x86-64

foldedtensor-0.3.5-cp39-cp39-win_amd64.whl (81.8 kB 查看哈希值)

上传时间 CPython 3.9 Windows x86-64

foldedtensor-0.3.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (119.1 kB 查看哈希值)

上传时间 CPython 3.9 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp39-cp39-macosx_11_0_arm64.whl (83.4 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

foldedtensor-0.3.5-cp39-cp39-macosx_10_9_x86_64.whl (85.9 kB 查看哈希)

上传于 CPython 3.9 macOS 10.9+ x86-64

foldedtensor-0.3.5-cp38-cp38-win_amd64.whl (81.9 kB 查看哈希)

上传于 CPython 3.8 Windows x86-64

foldedtensor-0.3.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (119.0 kB 查看哈希)

上传于 CPython 3.8 manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp38-cp38-macosx_11_0_arm64.whl (83.2 kB 查看哈希)

上传于 CPython 3.8 macOS 11.0+ ARM64

foldedtensor-0.3.5-cp38-cp38-macosx_10_9_x86_64.whl (85.6 kB 查看哈希)

上传于 CPython 3.8 macOS 10.9+ x86-64

foldedtensor-0.3.5-cp37-cp37m-win_amd64.whl (82.7 kB 查看哈希)

上传于 CPython 3.7m Windows x86-64

foldedtensor-0.3.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (121.0 kB 查看哈希)

上传于 CPython 3.7m manylinux: glibc 2.17+ x86-64

foldedtensor-0.3.5-cp37-cp37m-macosx_10_9_x86_64.whl (85.4 kB 查看哈希)

上传于 CPython 3.7m macOS 10.9+ x86-64

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面