Save dict of tensors to file Parameters ---------- filename : str File name to store dict of tensors. tensor_dict: dict of dgl NDArray or backend tensor Python dict using string as key and tensor as value Returns ---------- status : bool Ret
(filename, tensor_dict)
| 11 | |
| 12 | |
| 13 | def save_tensors(filename, tensor_dict): |
| 14 | """ |
| 15 | Save dict of tensors to file |
| 16 | |
| 17 | Parameters |
| 18 | ---------- |
| 19 | filename : str |
| 20 | File name to store dict of tensors. |
| 21 | tensor_dict: dict of dgl NDArray or backend tensor |
| 22 | Python dict using string as key and tensor as value |
| 23 | |
| 24 | Returns |
| 25 | ---------- |
| 26 | status : bool |
| 27 | Return whether save operation succeeds |
| 28 | """ |
| 29 | nd_dict = {} |
| 30 | is_empty_dict = len(tensor_dict) == 0 |
| 31 | for key, value in tensor_dict.items(): |
| 32 | if not isinstance(key, str): |
| 33 | raise Exception("Dict key has to be str") |
| 34 | if F.is_tensor(value): |
| 35 | nd_dict[key] = F.zerocopy_to_dgl_ndarray(value) |
| 36 | elif isinstance(value, NDArray): |
| 37 | nd_dict[key] = value |
| 38 | else: |
| 39 | raise Exception( |
| 40 | "Dict value has to be backend tensor or dgl ndarray" |
| 41 | ) |
| 42 | |
| 43 | return _CAPI_SaveNDArrayDict(filename, nd_dict, is_empty_dict) |
| 44 | |
| 45 | |
| 46 | def load_tensors(filename, return_dgl_ndarray=False): |