awslabs / gluonts

Probabilistic time series modeling in Python
https://ts.gluon.ai
Apache License 2.0
4.62k stars 753 forks source link

save and reuse trained estimator from estimator.train_model function #1249

Open stellaywu opened 3 years ago

stellaywu commented 3 years ago

Hi, I'm trying to save an estimator trained from the train_model function, tried .save_parameters(file_name) and .serialize(Path(file_name)), got AttributeError: 'DeepAREstimator' object has no attribute 'serialize'

Please help.

Thanks

stellaywu commented 3 years ago

figured out it should be train_model.trained_net.save_parameters(file_name)

stellaywu commented 3 years ago

sorry still need help, not sure how to load the saved params back to estimator in inference. If I save the model with the following code will get a non-serializable error.

estimator = DeepStateEstimator(
        prediction_length=prediction_length,
        freq=freq,
        past_length = context_length,
        cardinality = [1],
        use_feat_static_cat = False,
        trainer=Trainer
        (
            learning_rate=1e-3, 
            epochs=50, 
            num_batches_per_epoch=100,
            ctx = ctx            
        ),
    )
predictor= estimator.train_model(train_ds)

from pathlib import Path
estimator.serialize(Path(file_name))

will raise error AttributeError: 'DeepStateEstimator' object has no attribute 'serialize'

mbohlkeschneider commented 3 years ago

Hi @stellaywu,

do you really want to save the estimator? If your goal is to save a model to do inference later, you actually want to serialize the predictor, like this.

stellaywu commented 3 years ago

thanks for the reply @mbohlkeschneider I got this error when serialize because I am using estimator.train_model instead of estimator.train as I wanted to get the distribution which only exist in train_model. Any idea how to bypass that?

AttributeError                            Traceback (most recent call last)
<ipython-input-17-79a7c4bc6053> in <module>()
      3 
      4 from pathlib import Path
----> 5 predictor_deepar.serialize(Path(file_name))

AttributeError: 'TrainOutput' object has no attribute 'serialize'

Thanks!

hdsong1996 commented 2 years ago

Maybe you could initialize weights by

estimator = DeepStateEstimator(
        prediction_length=prediction_length,

        freq=freq,
        past_length = context_length,
        cardinality = [1],
        use_feat_static_cat = False,
        trainer=Trainer
        (
            learning_rate=1e-3, 
            epochs=50, 
            num_batches_per_epoch=100,
            ctx = ctx            
           init=init.Load(file_name)
        ),

    )

Trainer has a initialization way of init