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)
| 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), ( |