Skip to main content

LaminarFlow

Streamline your TensorFlow workflow.

Installation

pip install laminarflow

Usage

TFRecord Datasets

LaminarFlow has two classes for writing to and reading from TFRecord datasets, DatasetWriter and DatasetReader.

When creating your datasets with DatasetWriter, you can pass in raw Python or Numpy data, and it will automatically get converted into TensorFlow Examples or SequenceExamples and be written to a TFRecord file.

Then when reading from the TFRecord file, DatasetReader takes care of creating the input pipeline that will parse your stored Examples or SequenceExamples, prepare them as needed (batching, padding, shuffling, etc.), then pass them to your TensorFlow Estimator, implementing the recommended best practices as outlined in TensorFlow's Input Pipline Performance Guide.

To demonstrate, we'll create some datasets.

import laminarflow as lf

train_writer = lf.DatasetWriter('data/train.tfrecord')
test_writer = lf.DatasetWriter('data/test.tfrecord')

train_writer.write({
  'input': [3.1, 4.1, 5.9],
  'label': 2
})

train_writer.write({
  'input': [2.7, 1.8, 2.8],
  'label': 1
})

test_writer.write({
  'input': [0.1, 1.2, 3.5],
  'label': 8
})

train_writer.close()
test_writer.close()

We create a DatasetWriter, then call the write method on it for each TensorFlow Example or SequenceExample we want to add to the dataset. When we call the write method, we pass in a dictionary where the keys are the feature names and the values are the feature values. The values can be Numpy arrays or any values that can be converted into Numpy arrays, such as Python ints, floats, or lists of ints or floats. The shape of the values can be multidimensional, but must be the same between Examples. Creating SequenceExamples, which support variable length data, is discussed below.

When we are done writing data with a Writer, we call the close() method on it.

The data will be written to a TFRecord file and the shapes and data types of your features will be stored in a separate metadata JSON file, which will have the same filename as the TFRecord file, except the extension will be changed to '.json'.

data/
├── test.json
├── test.tfrecord
├── train.json
└── train.tfrecord

We can then train a model on our datasets.

train_dataset = lf.DatasetReader('data/train.tfrecord')
test_dataset = lf.DatasetReader('data/test.tfrecord')

estimator = tf.estimator.Estimator(
  model_fn=model_fn,
  model_dir=MODEL_DIR,
  params=PARAMS)

train_spec = tf.estimator.TrainSpec(
    input_fn=train_dataset.input_fn,
    max_steps=1000)

eval_spec = tf.estimator.EvalSpec(
    input_fn=test_dataset.input_fn)

tf.estimator.train_and_evaluate(
    estimator=estimator,
    train_spec=train_spec,
    eval_spec=eval_spec)

Calling lf.DatasetReader('data/train.tfrecord') creates a dataset using the TFRecord file and its corresponding metadata JSON file. The path to the metadata JSON file data/train.json is inferred from the TFRecord path.

The created dataset has an input_fn method that you can pass in as the input function to a TensorFlow Estimator. The input_fn method automatically creates the input pipeline for your dataset.

For a more complete example of creating datasets, training a model, and making predictions with that model, check out: xor.py

Using a with Statement

A DatasetWriter can also be created using a with statement, in which case the close() method does not need to be called.

with lf.DatasetWriter('data/train.tfrecord') as train_writer:
  train_writer.write({
    'input': [1.4, 1.4, 2.1],
    'label': 3
  })

SequenceExamples

The default behavior of the write method is to write a TensorFlow Example. To write a SequenceExample, instead of passing in features to the first parameter of the write method, pass in features using the context_features and sequence_features parameters.

train_writer.write(
  context_features={
    'category': 7
  },
  sequence_features={
    'inputs': [[1.4, 0.0], [1.4, 0.0], [1.4, 0.0]],
    'labels': [3, 5, 3]
  })

train_writer.write(
  context_features={
    'category': 5
  },
  sequence_features={
    'inputs': [[1.4, 0.0], [1.4, 0.0]],
    'labels': [3, 5]
  })

Passing in context_features is optional, but if used, their values must have the same shape between SequenceExamples, similar to Example features.

The shape of the sequence_features values must have a rank of at least 1. The length of the first dimension must be the same for all sequence_features within a SequenceExample, but can vary between SequenceExamples. And the lengths of the rest of the dimensions can vary between features, but must be the same between SequenceExamples.

When a batch of SequenceExamples is created, any sequences that are shorter than the longest sequence will be padded with zeros.

The length of each sequence will be extracted from the data as one of the steps in the input pipeline when reading from the dataset. The lengths of the sequences will be made available as one of the feature values passed into the model_fn, features['lengths']. It will be a batch size length list of ints, that are the lengths of each of the sequences in the batch before that sequence was possibly padded with zeros.

Metadata

Release files for laminarflow 0.0.7

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

Source distribution (sdist)

Source distribution for laminarflow 0.0.7
File Size Uploaded
laminarflow-0.0.7.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for laminarflow 0.0.7
File Interpreter ABI Platform
laminarflow-0.0.7-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.1 kB

Release files / laminarflow-0.0.7.tar.gz

Download URL laminarflow-0.0.7.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4358074a1df00c78bce151a3a7f54bdd04702f9ede1d757b419c311810273ba4
BLAKE2b-256 checksum
How to use checksums
4f9667e6a1a4ee55ec7bcc2f41509a500a6af5905d964762881f357929dd0f6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / laminarflow-0.0.7-py2.py3-none-any.whl

Download URL laminarflow-0.0.7-py2.py3-none-any.whl
Size 7.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4a0347eab9c67fb426e6b8439e00d5bbaa1c4fb851edb50afccdf4e44da0fc9f
BLAKE2b-256 checksum
How to use checksums
54807da0d5f5731a5e13dcdd394c57b7656dc3422d62590e161677f30e6477e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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