A tensor with annotated axes. It has the following invariants: 1) The dimensionality of the tensor is equal to the number of elements in axes. 2) The number of coordinate values in the ith dimension is equal to the size of the tensor in the ith dimension. Attributes: tensor
| 263 | |
| 264 | |
| 265 | class LabeledTensor(object): |
| 266 | """A tensor with annotated axes. |
| 267 | |
| 268 | It has the following invariants: |
| 269 | 1) The dimensionality of the tensor is equal to the number of elements |
| 270 | in axes. |
| 271 | 2) The number of coordinate values in the ith dimension is equal to the |
| 272 | size of the tensor in the ith dimension. |
| 273 | |
| 274 | Attributes: |
| 275 | tensor: tf.Tensor containing the data. |
| 276 | axes: lt.Axes containing axis names and coordinate labels. |
| 277 | """ |
| 278 | |
| 279 | @tc.accepts(object, ops.Tensor, |
| 280 | tc.Union(Axes, tc.Collection(tc.Union(string_types, AxisLike)))) |
| 281 | def __init__(self, tensor, axes): |
| 282 | """Construct a LabeledTensor. |
| 283 | |
| 284 | Args: |
| 285 | tensor: The underlying tensor containing the data. |
| 286 | axes: An Axes object, or a collection of strings, Axis objects or tuples |
| 287 | of (name, value) pairs indicating the axes. |
| 288 | |
| 289 | Raises: |
| 290 | ValueError: If the provided axes do not satisfy the class invariants. |
| 291 | """ |
| 292 | self._tensor = tensor |
| 293 | shape = tensor.get_shape() |
| 294 | |
| 295 | if isinstance(axes, Axes): |
| 296 | unvalidated_axes = axes |
| 297 | else: |
| 298 | mutable_axes = [] |
| 299 | |
| 300 | for position, axis_like in enumerate(axes): |
| 301 | if isinstance(axis_like, string_types): |
| 302 | # The coordinates for this axes are unlabeled. |
| 303 | # Infer the size of the axis. |
| 304 | value = shape[position] |
| 305 | axis_like = (axis_like, value) |
| 306 | |
| 307 | mutable_axes.append(axis_like) |
| 308 | |
| 309 | # Construct the Axis object, which will additionally validate the contents |
| 310 | # of the object. |
| 311 | unvalidated_axes = Axes(mutable_axes) |
| 312 | |
| 313 | # Check our invariants. |
| 314 | |
| 315 | # First, the rank of the tensor must be equal to the number of axes. |
| 316 | if len(shape) != len(unvalidated_axes): |
| 317 | raise ValueError( |
| 318 | 'Tensor rank was not equal to the number of axes: %r, %r' % |
| 319 | (shape, unvalidated_axes)) |
| 320 | |
| 321 | # Second, the size of each tensor dimension must match the size of the |
| 322 | # corresponding indices. |
no outgoing calls
no test coverage detected