mindspore.ops.TensorDump

View Source On Gitee
class mindspore.ops.TensorDump[source]

Save the Tensor as an npy file in numpy format.

The file name will automatically have a prefix added based on the execution order. For example, if file is a, the first saved file will be named 0_a.npy, and the second one will be named 1_a.npy, and so on.

Warning

  • If a large amount of data is stored within a short period, it may lead to memory overflow on the device side. Consider slicing the data to reduce the data scale.

  • Since data saving is processed asynchronously, when the amount of data is too large or the main process exits too quickly, data loss may occur. You need to actively control the destruction time of the main process, such as using sleep.

Inputs:
  • file (str) - The path of the file to be saved.

  • input_x (Tensor) - Input Tensor of any dimension.

Raises
Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> import time
>>> from mindspore import nn, Tensor, ops
>>> ms.set_context(mode=ms.GRAPH_MODE, device_target="Ascend")
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.dump = ops.TensorDump()
...
...     def construct(self, x):
...         x += 1.
...         self.dump('add', x)
...         x /= 2.
...         self.dump('div', x)
...         x *= 5.
...         self.dump('mul', x)
...         return x
...
>>> x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]).astype(np.float32)
>>> input_x = Tensor(x)
>>> net = Net()
>>> out = net(input_x)
>>> time.sleep(0.5)
>>> add = np.load('0_add.npy')
>>> print(add)
[[2. 3. 4. 5.]
 [6. 7. 8. 9.]]