The labeled_view module

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.

class csc.divisi.labeled_view.LabeledView(tensor, label_lists=None)

A LabeledView creates a layer of labels (probably strings) that map to the numerical keys of the underlying tensor.

array_op(op, labels=None, *args, **kwargs)

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.

bake()
Simplify the representation.
dot(other)
For first-order tensors, this is a dot product. For second-order, it performs matrix multiplication. Like Numpy matrices (not arrays), this is the operation performed by the * operator.
to_dense()
Change the underlying representation to a dense tensor.
top_items(n=10, largest=True, key=None, abs_val=None)

For each of the top n items with greatest values, return a pair of the item and its value.

Parameters:
n: number of items to return largest: True (default) to get the largest items, else get the smallest key: key to use when sorting. (e.g., abs)

If the tensor is 1D, gives indices instead of one-item tuples. Always expresses the output in floats directly.

unfolded_labels(mode)
Return the set of labels for the remaining dimensions if this view were to be unfolded at the given mode.
vector_to_dict()
Like #items for 1D tensors, but uses indices directly instead of one-item tuples.
with_zeros_removed()
Make a new LabeledView of a DictTensor with all the non-zero items in this tensor.
csc.divisi.labeled_view.make_sparse_labeled_tensor(ndim, labels=None, initial=None, accumulate=None, normalize=False)

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.

normalize:
an int or tuple of ints: normalize along that dimension True: normalize along axis 0 ‘tfidf’: use tf-idf ‘tfidf.T’: use tf-idf, transposed (matrix is documents by terms) a class: adds that class as a layer.

Previous topic

The ordered_set module

Next topic

The svd module

This Page