MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _as_lodtensor

Function _as_lodtensor

python/paddle/base/executor.py:697–762  ·  view source on GitHub ↗

Convert numpy.ndarray to Tensor, its only support Tensor without LoD information. For higher dimensional sequence data, please use DenseTensor directly. Examples: .. code-block:: pycon >>> import numpy as np >>> import paddle.base as base >

(data, place, dtype=None)

Source from the content-addressed store, hash-verified

695
696
697def _as_lodtensor(data, place, dtype=None):
698 """
699 Convert numpy.ndarray to Tensor, its only support Tensor without LoD information.
700 For higher dimensional sequence data, please use DenseTensor directly.
701
702 Examples:
703
704 .. code-block:: pycon
705
706 >>> import numpy as np
707 >>> import paddle.base as base
708 >>> place = base.CPUPlace()
709 >>> exe = base.Executor(place)
710 >>> data = np.array((100, 200, 300))
711 >>> np_outs = map(lambda x: base.executor._as_lodtensor(x, place), data)
712
713 Args:
714 data(numpy.ndarray|list|tuple|scalar): a instance of array, scalar, list or tuple
715 data(core.Place): the place of created tensor
716 dtype(str|paddle.dtype|np.dtype, optional): the expected data type of created tensor
717
718 Returns:
719 DenseTensor
720 """
721 # NOTE(zhiqiu): convert python builtin, like float, int, and list, to numpy ndarray
722 if not isinstance(data, np.ndarray):
723 assert dtype is not None, (
724 'The dtype should be given when feed data is not np.ndarray'
725 )
726 dtype = convert_dtype(dtype)
727 if np.isscalar(data):
728 data = np.array(data).astype(dtype)
729 elif isinstance(data, (list, tuple)):
730 data = np.array(data)
731 if data.dtype == np.object_:
732 raise TypeError(
733 "\n\tFailed to convert input data to a regular ndarray :\n\t* Usually "
734 "this means the input data contains nested lists with different lengths. "
735 "Please consider using 'base.create_lod_tensor' to convert it to a LoD-Tensor."
736 )
737 data = data.astype(dtype)
738 else:
739 raise TypeError(
740 f"Convert data of type {type(data)} to Tensor is not supported"
741 )
742
743 if core.is_compiled_with_custom_device("iluvatar_gpu") and os.environ.get(
744 'FLAG_FORCE_FLOAT32', ''
745 ).lower() in ['1', 'true', 'on']:
746 import logging
747
748 if data.dtype == np.float64:
749 logging.warning(
750 "Input data type is float64 which is not supported on iluvatar gpu, we will forcibly set tensor dtype to float32!"
751 )
752 data = data.astype(np.float32)
753 elif data.dtype == np.complex128:
754 logging.warning(

Callers 5

_alloc_and_fill_varFunction · 0.90
_feed_dataMethod · 0.85
_pir_feed_dataMethod · 0.85
_run_implMethod · 0.85
_run_pir_implMethod · 0.85

Calls 8

TypeErrorClass · 0.85
astypeMethod · 0.80
lowerMethod · 0.80
convert_dtypeFunction · 0.70
typeFunction · 0.50
getMethod · 0.45
DenseTensorMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected