Skip to content

Embedding

docarray.typing.tensor.embedding.embedding

AnyEmbedding

Bases: AnyTensor, EmbeddingMixin

Represents an embedding tensor object that can be used with TensorFlow, PyTorch, and NumPy type.


'''python from docarray import BaseDoc from docarray.typing import AnyEmbedding

class MyEmbeddingDoc(BaseDoc): embedding: AnyEmbedding

Example usage with TensorFlow:

import tensorflow as tf

doc = MyEmbeddingDoc(embedding=tf.zeros(1000, 2)) type(doc.embedding) # TensorFlowEmbedding

Example usage with PyTorch:

import torch

doc = MyEmbeddingDoc(embedding=torch.zeros(1000, 2)) type(doc.embedding) # TorchEmbedding

Example usage with NumPy:

import numpy as np

doc = MyEmbeddingDoc(embedding=np.zeros((1000, 2))) type(doc.embedding) # NdArrayEmbedding '''


Raises: TypeError: If the type of the value is not one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray]

Source code in docarray/typing/tensor/embedding/embedding.py
class AnyEmbedding(AnyTensor, EmbeddingMixin):
    """
    Represents an embedding tensor object that can be used with TensorFlow, PyTorch, and NumPy type.

    ---
    '''python
    from docarray import BaseDoc
    from docarray.typing import AnyEmbedding


    class MyEmbeddingDoc(BaseDoc):
        embedding: AnyEmbedding


    # Example usage with TensorFlow:
    import tensorflow as tf

    doc = MyEmbeddingDoc(embedding=tf.zeros(1000, 2))
    type(doc.embedding)  # TensorFlowEmbedding

    # Example usage with PyTorch:
    import torch

    doc = MyEmbeddingDoc(embedding=torch.zeros(1000, 2))
    type(doc.embedding)  # TorchEmbedding

    # Example usage with NumPy:
    import numpy as np

    doc = MyEmbeddingDoc(embedding=np.zeros((1000, 2)))
    type(doc.embedding)  # NdArrayEmbedding
    '''
    ---

    Raises:
        TypeError: If the type of the value is not one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray]
    """

    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(
        cls: Type[T],
        value: Union[T, np.ndarray, Any],
        field: "ModelField",
        config: "BaseConfig",
    ):
        if torch_available:
            if isinstance(value, TorchTensor):
                return cast(TorchEmbedding, value)
            elif isinstance(value, torch.Tensor):
                return TorchEmbedding._docarray_from_native(value)  # noqa
        if tf_available:
            if isinstance(value, TensorFlowTensor):
                return cast(TensorFlowEmbedding, value)
            elif isinstance(value, tf.Tensor):
                return TensorFlowEmbedding._docarray_from_native(value)  # noqa
        try:
            return NdArrayEmbedding.validate(value, field, config)
        except Exception:  # noqa
            pass
        raise TypeError(
            f"Expected one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray] "
            f"compatible type, got {type(value)}"
        )

docarray.typing.tensor.embedding.embedding_mixin

docarray.typing.tensor.embedding.ndarray

docarray.typing.tensor.embedding.tensorflow

docarray.typing.tensor.embedding.torch

TorchEmbedding

Bases: TorchTensor, EmbeddingMixin

Source code in docarray/typing/tensor/embedding/torch.py
@_register_proto(proto_type_name='torch_embedding')
class TorchEmbedding(TorchTensor, EmbeddingMixin, metaclass=metaTorchAndEmbedding):
    alternative_type = TorchTensor

    def new_empty(self, *args, **kwargs):
        """
        This method enables the deepcopy of `TorchEmbedding` by returning another instance of this subclass.
        If this function is not implemented, the deepcopy will throw an RuntimeError from Torch.
        """
        return self.__class__(TorchTensor.new_empty(self, *args, **kwargs))

new_empty(*args, **kwargs)

This method enables the deepcopy of TorchEmbedding by returning another instance of this subclass. If this function is not implemented, the deepcopy will throw an RuntimeError from Torch.

Source code in docarray/typing/tensor/embedding/torch.py
def new_empty(self, *args, **kwargs):
    """
    This method enables the deepcopy of `TorchEmbedding` by returning another instance of this subclass.
    If this function is not implemented, the deepcopy will throw an RuntimeError from Torch.
    """
    return self.__class__(TorchTensor.new_empty(self, *args, **kwargs))