mindspore.Tensor.set_
- Tensor.set_(source=None, storage_offset=0, size=None, stride=None) Tensor
Sets the underlying storage, size, and stride. If source is a tensor, the self tensor will share the same storage with it, along with the same size and stride. Modifications to elements of one tensor will be reflected in the other.
This method supports multiple parameter combinations, with the valid call signatures as follows:
set_() -> Tensor: Parameterless call that sets the current tensor to an uninitialized empty tensor.set_(source: Storage) -> Tensor: Sets the underlying storage of the self tensor to the specifiedStorage.set_(source: Storage, storage_offset: int, \ size: tuple | list, stride: tuple | list) -> Tensor: Sets the underlying storage of the self tensor to the specifiedStorage, and simultaneously sets the size and stride of the self tensor to the provided size and stride.set_(source: Tensor) -> Tensor: Makes the self tensor share the same underlying storage as the source tensor, and the storage_offset, size, and stride of the self tensor are the same as those of the source tensor.set_(source: Tensor, storage_offset: int, \ size: tuple | list, stride: tuple | list) -> Tensor: Makes the self tensor share the same underlying storage as the source tensor, and simultaneously sets the size and stride of the self tensor to the provided size and stride.
Note
If the device of the current tensor when calling set_ is
CPUand it needs to be used onAscendsubsequently, it is recommended to explicitly copy the tensor to Ascend for use.If the device of the current tensor when calling set_ is
CPU, setting a non-contiguous underlying storage for the tensor will cause subsequent in-place modifications on CPU to not take effect.
- Parameters
source (Tensor or Storage) – The Tensor or Storage that needs to share the underlying storage.
storage_offset (int) – Specifies the offset of the current tensor relative to the underlying storage.
size (tuple or list) – Specifies the size of the current tensor in the underlying storage.
stride (tuple or list, optional) – Specifies the stride of the current tensor in the underlying storage. Default:
None, which uses row-contiguous strides by default.
- Raises
TypeError – The input parameter type does not meet the requirements, or the number of input parameters does not match.
RuntimeError – If the passed size is the same as the original size of self, the underlying size being set exceeds the underlying size of source.
RuntimeError – The passed storage_offset is less than 0.
RuntimeError – The set size contains a value less than 0.
RuntimeError – The set stride contains a value less than 0.
RuntimeError – The number of elements in the set size and stride is not the same.
RuntimeError – The number of elements in the set size exceeds 8.
RuntimeError – When source is a Tensor and parameters such as size are provided, but the source tensor is non-contiguous.
RuntimeError – When source is of Tensor and no other parameters are provided, the dtype of the self tensor and source are different.
RuntimeError – When source is of Storage, the dtype of the self tensor and source are different.
RuntimeError – The device of the self tensor and source is not the same.
Examples
>>> import mindspore as ms >>> import numpy as np >>> data = ms.Tensor([10, 20, 30], dtype = ms.float32) >>> target = ms.Tensor(np.zeros(3)) >>> print(target) [0., 0., 0.] >>> target.set_(data) >>> print(target) [10., 20., 30.]