Only encode to JSON once when serializing a Python model (#2178)

This commit is contained in:
tomsutch 2022-05-25 12:39:10 +01:00 committed by GitHub
parent cc8c69a3ba
commit 84132a29eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 6 deletions

View file

@ -36,14 +36,13 @@ In Python, models should not be saved with pickle; the Stan backend attached to
```python ```python
# Python # Python
import json
from prophet.serialize import model_to_json, model_from_json from prophet.serialize import model_to_json, model_from_json
with open('serialized_model.json', 'w') as fout: with open('serialized_model.json', 'w') as fout:
json.dump(model_to_json(m), fout) # Save model fout.write(model_to_json(m)) # Save model
with open('serialized_model.json', 'r') as fin: with open('serialized_model.json', 'r') as fin:
m = model_from_json(json.load(fin)) # Load model m = model_from_json(fin.read()) # Load model
``` ```
The json file will be portable across systems, and deserialization is backwards compatible with older versions of prophet. The json file will be portable across systems, and deserialization is backwards compatible with older versions of prophet.

View file

@ -122,14 +122,13 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import json\n",
"from prophet.serialize import model_to_json, model_from_json\n", "from prophet.serialize import model_to_json, model_from_json\n",
"\n", "\n",
"with open('serialized_model.json', 'w') as fout:\n", "with open('serialized_model.json', 'w') as fout:\n",
" json.dump(model_to_json(m), fout) # Save model\n", " fout.write(model_to_json(m)) # Save model\n",
"\n", "\n",
"with open('serialized_model.json', 'r') as fin:\n", "with open('serialized_model.json', 'r') as fin:\n",
" m = model_from_json(json.load(fin)) # Load model" " m = model_from_json(fin.read()) # Load model"
] ]
}, },
{ {