mygrad.load#

mygrad.load(file: str | Path | BinaryIO) Tensor[source]#

Loads a saved Tensor and its gradient information (if applicable).

This docstring was adapted from that of numpy.load()

Parameters:
filestr | Path | BinaryIO

The name of the file that holds the tensor data to load.

Returns:
loadedTensor

The loaded tensor (whose gradient will be loaded if it was saved).

See also

mygrad.save

Examples

>>> import mygrad as mg
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> x = mg.arange(10.0)
>>> mg.save(outfile, x)
>>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> mg.load(outfile)
Tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])

An example of saving a tensor that has an associated gradient.

>>> (x * x).backward()
>>> x.grad
array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18.])
>>> outfile = TemporaryFile()
>>> x = mg.arange(10.0)
>>> mg.save(outfile, x)
>>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> loaded = mg.load(outfile)
>>> loaded
Tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> loaded.grad
array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18.])