Skip to content

Commit 5b63568

Browse files
authored
Merge pull request #8711 from JiayiFeng/add_save_load_layer
Add layers for save/load op
2 parents b02e12e + bd84409 commit 5b63568

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3206,7 +3206,7 @@ def one_hot(input, depth):
32063206
operator.
32073207
32083208
Args:
3209-
input(Tensor/LodTensor): A Tensor/LodTensor of indices, last dimension must be 1.
3209+
input(variable): A Tensor/LodTensor of indices, last dimension must be 1.
32103210
depth(scalar): an interger defining the depth of the one hot dimension.
32113211
32123212
Returns:

python/paddle/fluid/layers/tensor.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,75 @@ def zeros(shape, dtype, force_cpu=False):
362362
data = fluid.layers.zeros(shape=[1], dtype='int64')
363363
"""
364364
return fill_constant(value=0.0, **locals())
365+
366+
367+
def save(x, file_path, overwrite=True):
368+
"""
369+
Saves a variable as a file.
370+
371+
Args:
372+
x(variable): The Tensor/LoDTensor to be saved.
373+
file_path(str): The file path where the variable will be saved.
374+
overwrite(bool): Whether or not cover the given file when it has already
375+
existed. If it's set 'False' and the file is existed, a runtime
376+
error will be thrown.
377+
"""
378+
helper = LayerHelper("save", **locals())
379+
helper.append_op(
380+
type="save",
381+
inputs={"input": x},
382+
outputs={},
383+
args={"file_path": file_path,
384+
"overwrite": overwrite})
385+
386+
387+
def save_combine(x, file_path, overwrite=True):
388+
"""
389+
Saves a list of variables into a single file.
390+
391+
Args:
392+
x(list): A list of Tensor/LoDTensor to be saved together in a single file.
393+
file_path(str): The file path where variables will be saved.
394+
overwrite(bool): Whether or not cover the given file when it has already
395+
existed. If it's set 'False' and the file is existed, a runtime
396+
error will be thrown.
397+
"""
398+
helper = LayerHelper("save_combine", **locals())
399+
helper.append_op(
400+
type="save_combine",
401+
inputs={"input": x},
402+
outputs={},
403+
args={"file_path": file_path,
404+
"overwrite": overwrite})
405+
406+
407+
def load(out, file_path):
408+
"""
409+
Loads a variable from a given file.
410+
411+
Args:
412+
out(variable): The variable to be read from the disk file.
413+
file_path(str): The path of the disk file.
414+
"""
415+
helper = LayerHelper("load", **locals())
416+
helper.append_op(
417+
type="load",
418+
inputs={},
419+
output={"Out": out},
420+
args={"file_path": file_path})
421+
422+
423+
def load_combine(out, file_path):
424+
"""
425+
Loads a list of vairables from a single file.
426+
427+
Args:
428+
out(list): The list of variables to be read from the disk file.
429+
file_path(str): The path of the disk file.
430+
"""
431+
helper = LayerHelper("load_combine", **locals())
432+
helper.append_op(
433+
type="load_combine",
434+
inputs={},
435+
output={"Out": out},
436+
args={"file_path": file_path})

0 commit comments

Comments
 (0)