A LabeledView makes Divisi tensors refer to data using meaningful indices that you choose. This allows you to use Tensors like Python dictionaries.
Here is an example of using a LabeledView to store labeled data:
>>> from csc.divisi.labeled_view import make_sparse_labeled_tensor
>>> t = make_sparse_labeled_tensor(ndim=2)
>>> t['grass', 'green'] = 2
>>> t['grass', 'red'] = -2
>>> t['apple', 'red'] = 3
>>> t
<LabeledView of <DictTensor shape: (2, 2); 3 items>, keys like: ('grass', 'green')>
Now let’s see what we got by removing layers of abstraction:
>>> t.unwrap()
<DictTensor shape: (2, 2); 3 items>
>>> t.unwrap().unwrap()
{0: {0: 2, 1: -2}, 1: {1: 3}}
>>> t.label_lists()
[OrderedSet(['grass', 'apple']), OrderedSet(['green', 'red'])]
See Views for more about what a View does.
A LabeledView creates a layer of labels (probably strings) that map to the numerical keys of the underlying tensor.
Apply an operation to the tensor inside (which must be a DenseTensor), and return the result wrapped in the same view.
This assumes the resulting tensor has the same shape, unless you provide a labels= parameter.
For each of the top n items with greatest values, return a pair of the item and its value.
If the tensor is 1D, gives indices instead of one-item tuples. Always expresses the output in floats directly.
Create a sparse labeled tensor.
ndim: number of dimensions (usually 2)
labels: if you already have label lists, pass them in here. (A None in this list means an unlabeled dimension. If you simply don’t have labels yet, pass an OrderedSet().)
initial / accumulate: sequences of (key, value) pairs to add to the tensor. initial is applied first by .update, meaning that later values will override earlier ones. accumulate is applied afterwards, and all values add to anything already there.