Imp.Example (Imp v0.5.0)

Copy Markdown View Source

Flexible row of named data used for train/dev/test sets.

Examples are plain data with one important Imp convention: call with_inputs/2 to mark which fields a program may see. The remaining fields are labels for evaluation, bootstrapping, demonstrations, and optimizers.

A key keeps the type it was given: %{"question" => ...} is stored under the string and %{question: ...} under the atom, and no string is turned into an atom. Every function that takes a key compares keys by their text, so get(example, :question) reads a field stored under "question".

Internal fields whose names start with imp_ are omitted from keys/1, items/1, and values/1, but to_map/1 remains lossless for persistence and debugging.

Example

iex> example =
...>   Imp.Example.new(question: "2+2?", answer: "4", imp_trace: :kept)
...>   |> Imp.Example.with_inputs(:question)
iex> Imp.Example.to_map(Imp.Example.inputs(example))
%{question: "2+2?"}
iex> Imp.Example.to_map(Imp.Example.labels(example))
%{answer: "4", imp_trace: :kept}
iex> Imp.Example.keys(example) |> Enum.sort()
[:answer, :question]

Summary

Functions

Returns a copy of the example with one field removed.

Reads a field or raises KeyError when it is missing.

Reads a field, returning default when it is missing.

Returns an example containing only the marked input fields.

Returns non-internal {key, value} field pairs.

Returns non-internal field keys.

Returns an example containing label fields, excluding marked inputs.

Builds an example from a map, field pair list, or existing example.

Returns a copy of the example with one field set.

Returns the full field map, including internal imp_ fields.

Returns non-internal field values.

Attaches demonstrations to an example.

Marks which fields are inputs for programs and optimizers.

Types

t()

@type t() :: %Imp.Example{
  demos: list(),
  fields: map(),
  input_keys: [atom() | String.t()] | nil
}

Functions

delete(example, key)

Returns a copy of the example with one field removed.

fetch!(example, key)

Reads a field or raises KeyError when it is missing.

get(example, key, default \\ nil)

Reads a field, returning default when it is missing.

inputs(example)

Returns an example containing only the marked input fields.

items(example)

Returns non-internal {key, value} field pairs.

keys(example)

Returns non-internal field keys.

labels(example)

Returns an example containing label fields, excluding marked inputs.

new(fields \\ %{})

Builds an example from a map, field pair list, or existing example.

put(example, key, value)

Returns a copy of the example with one field set.

to_map(example)

Returns the full field map, including internal imp_ fields.

values(example)

Returns non-internal field values.

with_demos(example, demos)

Attaches demonstrations to an example.

with_inputs(example, keys)

Marks which fields are inputs for programs and optimizers.