Skip to content

Instantly share code, notes, and snippets.

@anna-hope
Last active March 18, 2018 18:03
Show Gist options
  • Save anna-hope/9d3cb9284d8d5667857baeb46b4880fa to your computer and use it in GitHub Desktop.
Save anna-hope/9d3cb9284d8d5667857baeb46b4880fa to your computer and use it in GitHub Desktop.
Make torchtext training examples from pandas.DataFrame on the fly
import pandas as pd
from torchtext import data
from tqdm import tqdm
def make_examples(df: pd.DataFrame, fields: Dict[str, data.Field]):
fields = {field_name: (field_name, field)
for field_name, field in fields.items()}
for _, row in tqdm(df.iterrows()):
example = data.Example.fromdict(row, fields)
yield example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment