MCPcopy Index your code
hub / github.com/pytorch/pytorch / set

Method set

caffe2/python/schema.py:795–876  ·  view source on GitHub ↗

Set the type and/or blob of this scalar. See __init__ for details. Args: dtype: can be any numpy type. If not provided and `blob` is provided, it will be inferred. If no argument is provided, this Scalar will be of type np.void.

(self, dtype=None, blob=None, metadata=None, unsafe=False)

Source from the content-addressed store, hash-verified

793 self.set(dtype=self._original_dtype, blob=blob, unsafe=unsafe)
794
795 def set(self, dtype=None, blob=None, metadata=None, unsafe=False):
796 """Set the type and/or blob of this scalar. See __init__ for details.
797
798 Args:
799 dtype: can be any numpy type. If not provided and `blob` is
800 provided, it will be inferred. If no argument is provided,
801 this Scalar will be of type np.void.
802 blob: if provided, can be either a BlobReference or a
803 numpy.ndarray. If a value of different type is passed,
804 a conversion to numpy.ndarray is attempted. Strings aren't
805 accepted, since they can be ambiguous. If you want to pass
806 a string, to either BlobReference(blob) or np.array(blob).
807 metadata: optional instance of Metadata, if provided overrides
808 the metadata information of the scalar
809 """
810 if not unsafe:
811 logger.warning(
812 "Scalar should be considered immutable. Only call Scalar.set() "
813 "on newly created Scalar with unsafe=True. This will become an "
814 "error soon."
815 )
816 if blob is not None and isinstance(blob, basestring):
817 raise ValueError(
818 'Passing str blob to Scalar.set() is ambiguous. '
819 'Do either set(blob=np.array(blob)) or '
820 'set(blob=BlobReference(blob))'
821 )
822
823 self._original_dtype = dtype
824 # Numpy will collapse a shape of 1 into an unindexed data array (shape = ()),
825 # which betrays the docstring of this class (which expects shape = (1,)).
826 # >>> import numpy as np
827 # >> np.dtype((np.int32, 1))
828 # dtype('int32')
829 # >>> np.dtype((np.int32, 5))
830 # dtype(('<i4', (5,)))
831 if dtype is not None and isinstance(dtype, tuple) and dtype[1] == 1:
832 dtype = (dtype[0], (1,))
833 if dtype is not None:
834 if isinstance(dtype, tuple) and dtype[0] == np.void:
835 raise TypeError(
836 "Cannot set the Scalar with type {} for blob {}."
837 "If this blob is the output of some operation, "
838 "please verify the input of that operation has "
839 "proper type.".format(dtype, blob)
840 )
841 dtype = np.dtype(dtype)
842 # If blob is not None and it is not a BlobReference, we assume that
843 # it is actual tensor data, so we will try to cast it to a numpy array.
844 if blob is not None and not isinstance(blob, BlobReference):
845 preserve_shape = isinstance(blob, np.ndarray)
846 if dtype is not None and dtype != np.void:
847 blob = np.array(blob, dtype=dtype.base)
848 # if array is empty we may need to reshape a little
849 if blob.size == 0 and not preserve_shape:
850 blob = blob.reshape((0, ) + dtype.shape)
851 else:
852 assert isinstance(blob, np.ndarray), (

Callers 9

__init__Method · 0.95
set_valueMethod · 0.95
proc1Method · 0.45
proc2Method · 0.45
proc1Method · 0.45
proc2Method · 0.45
test_dataset_opsMethod · 0.45
_run_metricsMethod · 0.45
dump_plotFunction · 0.45

Calls 7

set_metadataMethod · 0.95
_validate_metadataMethod · 0.95
isinstanceFunction · 0.85
warningMethod · 0.80
formatMethod · 0.45
dtypeMethod · 0.45
reshapeMethod · 0.45

Tested by 5

proc1Method · 0.36
proc2Method · 0.36
proc1Method · 0.36
proc2Method · 0.36
test_dataset_opsMethod · 0.36