Skip to main content

A set of utilities to create and manage configuration

Project description

SMASHED

Sequential MAppers for Sequences of HEterogeneous Dictionaries is a set of Python interfaces designed to apply transformations to samples in datasets, which are often implemented as sequences of dictionaries.

Example of Usage

Mappers are initialized and then applied sequentially. In the following example, we create a mapper that is applied to a samples, each containing a sequence of strings. The mappers are responsible for the following operations.

  1. Tokenize each sequence, cropping it to a maximum length if necessary.
  2. Stride sequences together to a maximum length or number of samples.
  3. Add padding symbols to sequences and attention masks.
  4. Concatenate all sequences from a stride into a single sequence.
import transformers
from smashed.interfaces.simple import (
    Dataset,
    TokenizerMapper,
    MultiSequenceStriderMapper,
    TokensSequencesPaddingMapper,
    AttentionMaskSequencePaddingMapper,
    SequencesConcatenateMapper,
)

tokenizer = transformers.AutoTokenizer.from_pretrained(
    pretrained_model_name_or_path='bert-base-uncased',
)

mappers = [
    TokenizerMapper(
        input_field='sentences',
        tokenizer=tokenizer,
        add_special_tokens=False,
        truncation=True,
        max_length=80
    ),
    MultiSequenceStriderMapper(
        max_stride_count=2,
        max_length=512,
        tokenizer=tokenizer,
        length_reference_field='input_ids'
    ),
    TokensSequencesPaddingMapper(
        tokenizer=tokenizer,
        input_field='input_ids'
    ),
    AttentionMaskSequencePaddingMapper(
        tokenizer=tokenizer,
        input_field='attention_mask'
    ),
    SequencesConcatenateMapper()
]

dataset = Dataset([
    {
        'sentences': [
            'This is a sentence.',
            'This is another sentence.',
            'Together, they make a paragraph.',
        ]
    },
    {
        'sentences': [
            'This sentence belongs to another sample',
            'Overall, the dataset is made of multiple samples.',
            'Each sample is made of multiple sentences.',
            'Samples might have a different number of sentences.',
            'And that is the story!',
        ]
    }
])

for mapper in mappers:
    dataset = mapper.map(dataset)

print(len(dataset))

# >>> 5

print(dataset[0])

# >>> {
#    'input_ids': [
#        101, 
#        2023, 
#        2003, 
#        1037, 
#        6251, 
#        1012, 
#        102, 
#        2023, 
#        2003, 
#        2178, 
#        6251, 
#        1012, 
#        102
#    ], 
#    'attention_mask': [
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1, 
#        1
#    ]
# }

Dataset Interfaces Available

The initial version of SMASHED supports two interfaces for dataset:

  1. interfaces.simple.Dataset: A simple dataset representation that is just a list of python dictionaries with some extra convenience methods to make it work with SMASHED. You can crate a simple dataset by passing a list of dictionaries to interfaces.simple.Dataset.
  2. HuggingFace datasets library. SMASHED mappers work with any datasets from HuggingFace, whether it is a regular or iterable dataset.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smashed-0.1.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

smashed-0.1.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file smashed-0.1.2.tar.gz.

File metadata

  • Download URL: smashed-0.1.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for smashed-0.1.2.tar.gz
Algorithm Hash digest
SHA256 48af30a3fc269163f8f7e9ae8e1156be0aaba9cf8cc26263e59813c429801dff
MD5 8c1bf7d6e7f4a320afa585de02b916c2
BLAKE2b-256 2197d1827981f907f4a8d17b1c5f2c91a5cdcc6dbbbc56537a4756f3b23d0ad3

See more details on using hashes here.

File details

Details for the file smashed-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: smashed-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.7

File hashes

Hashes for smashed-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24388058638ae388d6902d75d44d60b3f368fd8f5dc25db6e97f0bd824d35b00
MD5 ae3ceb8158ae3cb22a469b49ddcc0635
BLAKE2b-256 86c6f1d27d7fb25cc0e5554b3f658292cf26d81352cdd1185be2b8031a7f1fce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page