@@ -362,3 +362,75 @@ def zeros(shape, dtype, force_cpu=False):
362
362
data = fluid.layers.zeros(shape=[1], dtype='int64')
363
363
"""
364
364
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