Skip to main content

Keras ❤️ torchtext

Keras is love
Keras is life
Keras loves torchtext

torchtext is a great library, putting a layer of abstraction over the usually very heavy data component in NLP projects, making the work with complex datasets a pace. Sadly, as torchtext is based and built on PyTorch, using it with Keras is not directly possible.

Keras ❤️ torchtext is to the rescue by providing lightweight wrappers for some Torchtext classes, making them easily work with Keras.

Installation

pip install keras-loves-torchtext

Examples

Wrap a torchtext.data.Iterator with WrapIterator and use it to train a Keras model:

from torchtext.data import Dataset, Field, Iterator
from kltt import WrapIterator

...

fields  = [('text', Field()),
           ('label', Field(sequential=False))]
dataset = Dataset(examples, fields)
iterator = Iterator(dataset, batch_size=32)

# Keras ❤️ torchtext comes to play
data_gen = WrapIterator(iterator, x_fields=['text'], y_fields=['label'])

model.fit_generator(iter(data_gen), steps_per_epoch=len(data_gen))

Easily wrap multiple iterators at once:

from torchtext.data import Dataset, Field, Iterator
from kltt import WrapIterator

...

fields  = [('text', Field()),
           ('label', Field(sequential=False))]
dataset = Dataset(examples, fields)
splits = dataset.split()

iterators = Iterator.splits(splits, batch_size=32)
train, valid, test = WrapIterator.wraps(iterators, x_fields=['text'], y_fields=['label'])
model.fit_generator(iter(train), steps_per_epoch=len(train),
                    validation_data=iter(valid), validation_steps=len(valid))
loss, acc = model.evaluate_generator(iter(test), steps=len(test))

Further and full working examples can be found in the examples folder.

Documentation

Todo

See examples and inline documentation for now.

Metadata

Release files for keras-loves-torchtext 0.0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keras-loves-torchtext 0.0.1.1
File Size Uploaded
keras-loves-torchtext-0.0.1.1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keras-loves-torchtext 0.0.1.1
File Interpreter ABI Platform
keras_loves_torchtext-0.0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.1 kB

Release files / keras-loves-torchtext-0.0.1.1.tar.gz

Download URL keras-loves-torchtext-0.0.1.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4ae04689f30ea2b3ae5250fac604ab143f19f513ffaa4683dc30009ace68063d
BLAKE2b-256 checksum
How to use checksums
406c2c95a107df8e3afd0a247212a838fdfbaf780d387ebdcb4d19b600a70faf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release files / keras_loves_torchtext-0.0.1.1-py3-none-any.whl

Download URL keras_loves_torchtext-0.0.1.1-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d34d9308e7dac577330daf8add3e590e86f8267e81bc6e7989f0ba3393541d2e
BLAKE2b-256 checksum
How to use checksums
2b533869583538fa61e1a559a627c0ee5c2ba41c1a88e3b7d076dfc73f48957d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.0.1.1 This release

2 release files

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page