when i use Combine for lag_transforms everything i...
# mlforecast
j
when i use Combine for lag_transforms everything is fine during training, but for inference I get an attribute error. Maybe based on some version issues?
Copy code
AttributeError: 'Combine' object has no attribute '_core_tfm'
j
Hey. Can you provide how you're using the combine?
j
inside MLForecast in lag_transform lke this, even if i tried no offset I got the same error. and as i said: no issue during training, but when i load the model and want to do inference it fails:
Copy code
lag_transforms={
                        1: [
                            ExpandingMax(), 
                            RollingMean(window_size=7, min_samples=1),  # 7-day rolling mean
                            RollingStd(window_size=7, min_samples=1),
                            
                             RollingMean(window_size=14, min_samples=1),  # 7-day rolling mean
                                Combine(
                                    RollingMean(window_size=14, min_samples=1),  # Current 7-day rolling mean
                                    Offset(RollingMean(window_size=14, min_samples=1), n=357),  # Previous day's 7-day rolling mean
                                    operator.sub,  # Division to compare current to previous
                                ),
j
I tried the following and it works:
Copy code
import operator

from mlforecast import MLForecast
from mlforecast.lag_transforms import RollingMean, Offset, Combine
from sklearn.linear_model import LinearRegression
from utilsforecast.data import generate_series

series = generate_series(1, min_length=500)
mlf = MLForecast(
    models=[LinearRegression()],
    freq='D',
    lag_transforms={
        1: [
            Combine(
                RollingMean(window_size=14, min_samples=1),
                Offset(RollingMean(window_size=14, min_samples=1), n=357),
                operator.sub,
            )
        ]
    }
)
mlf.fit(series).save('model')
MLForecast.load('model').predict(10)
Are you able to share an example that fails?
j
Not so easy to share the full code. It is pretty long and I use dill for saving in MLFlow. Might be an issue with serialization of the transformation within dill. But why would dill fail, while joblib or others work well? mhh, annoying 🙂
j
You can try using the mlflow flavor we added recently, this works as well:
Copy code
import operator

import mlflow
import mlforecast.flavor
from mlforecast import MLForecast
from mlforecast.lag_transforms import RollingMean, Offset, Combine
from sklearn.linear_model import LinearRegression
from utilsforecast.data import generate_series

series = generate_series(1, min_length=500)
mlf = MLForecast(
    models=[LinearRegression()],
    freq='D',
    lag_transforms={
        1: [
            Combine(
                RollingMean(window_size=14, min_samples=1),
                Offset(RollingMean(window_size=14, min_samples=1), n=357),
                operator.sub,
            )
        ]
    }
)
mlf.fit(series)
artifact_path = "mlf_model"
mlforecast.flavor.log_model(model=mlf, artifact_path=artifact_path)
model_uri = mlflow.get_artifact_uri(artifact_path)
mlforecast.flavor.load_model(model_uri).predict(10)
j
👍
i havent checked it out yet. can you set tags etc and use the full mlflow infrastructure?
j
It should be possible
👍 1