NeuralProphet是一個python庫,用於基於神經網絡對時間序列數據進行建模。它建立在PyTorch之上,並受到Facebook Prophet和AR-Net庫的極大啟發。
NeuralProphet 和 Prophet對比
從庫名稱中,您可能會問Facebook的Prophet庫和NeuralProphet之間的主要區別是什麼。根據NeuralProphet的文檔,增加的功能是[1]:
使用PyTorch的Gradient Descent進行優化,使建模過程比Prophet快得多
使用AR-Net建模時間序列自相關(也稱為序列相關)
自定義損失和指標
具有前饋神經網絡的可配置非線性層,
等等
項目維護者
基於該項目的GitHub頁面,該項目的主要維護者是史丹福大學的Oskar Triebe,Facebook和莫納什大學的合作。
安裝
該項目處於測試階段,因此,如果您要在生產環境中使用此庫,我建議您謹慎使用。
不幸的是,在撰寫本文時,該庫沒有pip或conda軟體包。只能通過克隆存儲庫並運行pip install。來安裝它。但是,如果要在Jupyter Notebook環境中使用該軟體包,則應安裝其實時版本這將提供更多功能,例如使用plot_live_loss()實時訓練和驗證損失。
git clone https://github.com/ourownstory/neural_prophet
cd neural_prophet
pip install .[live]
我建議創建一個新環境(conda或venv),並從新環境安裝NeuralProphet軟體包,讓安裝程序處理所有依賴項(它具有Pandas,Jupyter Notebook,PyTorch作為依賴項)。
現在我們已經安裝了軟體包,讓我們開始吧!
案例分析實踐
在這裡,我使用在Kaggle上的2013年至2017年德裡的每日氣候數據。首先,讓我們導入主要包。
import pandas as pd
from neuralprophet import NeuralProphet
然後,我們可以將數據讀取到Panda DataFrame中。NeuralProphet對象期望時間序列數據具有一個名為ds的日期列,而我們希望將其預測為y。
# Data is from https://www.kaggle.com/sumanthvrao/daily-climate-time-series-data
df = pd.read_csv("./DailyDelhiClimateTrain.csv", parse_dates=["date"])
df = df[["date", "meantemp"]]
df.rename(columns={"date": "ds", "meantemp": "y"}, inplace=True)
現在,讓我們初始化模型,為NeuralProphet對象定義的所有默認參數,包括有關某些參數的其他信息。這些是您可以在模型中配置的超參數。當然,如果您打算使用默認變量,則只需執行model = NeuralProphet()。
# model = NeuralProphet() if you're using default variables below.
model = NeuralProphet(
growth="linear", # Determine trend types: 'linear', 'discontinuous', 'off'
changepoints=None, # list of dates that may include change points (None -> automatic )
n_changepoints=5,
changepoints_range=0.8,
trend_reg=0,
trend_reg_threshold=False,
yearly_seasonality="auto",
weekly_seasonality="auto",
daily_seasonality="auto",
seasonality_mode="additive",
seasonality_reg=0,
n_forecasts=1,
n_lags=0,
num_hidden_layers=0,
d_hidden=None, # Dimension of hidden layers of AR-Net
ar_sparsity=None, # Sparcity in the AR coefficients
learning_rate=None,
epochs=40,
loss_func="Huber",
normalize="auto", # Type of normalization ('minmax', 'standardize', 'soft', 'off')
impute_missing=True,
log_level=None, # Determines the logging level of the logger object
)
配置模型及其超參數後,我們需要訓練模型並進行預測。讓我們對溫度進行一年的預測。
metrics = model.fit(df, validate_each_epoch=True, freq="D")
future = model.make_future_dataframe(df, periods=365, n_historic_predictions=len(df))
forecast = model.predict(future)
您可以通過調用model.plot(forecast)來簡單地繪製預測,如下所示:
fig, ax = plt.subplots(figsize=(14, 10))
model.plot(forecast, xlabel="Date", ylabel="Temp", ax=ax)
ax.set_title("Mean Temperature in Delhi", fontsize=28, fontweight="bold")
上面顯示了一年的預測圖,其中從2017-01-01到2018-01-01之間的時間段是預測。可以看出,預測圖類似於歷史時間序列。它既捕獲了季節性,又捕獲了線性趨勢的緩慢增長。
也可以通過調用model.plot_parameters()來繪製參數。
使用平均絕對誤差(MAE)的模型損失如下圖所示。您也可以使用「平滑的L1損失」功能。
fig, ax = plt.subplots(figsize=(14, 10))
ax.plot(metrics["MAE"], 'ob', linewidth=6, label="Training Loss")
ax.plot(metrics["MAE_val"], '-r', linewidth=2, label="Validation Loss")
# You can use metrics["SmoothL1Loss"] and metrics["SmoothL1Loss_val"] too.
總結
在本文中,我們討論了NeuralProphet,這是一個基於神經網絡對時間序列進行建模的python庫。該庫使用PyTorch作為後端。作為案例研究,我們為德裡的每日氣候時間序列數據創建了一個預測模型,並進行了一年的預測。使用此庫的一個優點是其語法與Facebook的Prophet庫類似。
您可以在GitHub上找到此博客文章的Jupyter筆記本。謝謝閱讀!
https://github.com/e-alizadeh/medium/blob/master/notebooks/NeuralProphet/neural_prophet.ipynb
參考文獻
[1] NeuralProphet
[2] O. J. Triebe et al, AR-Net: A Simple Auto-Regressive Neural Network For Time-Series, (2019)
[3]https://facebook.github.io/prophet/
[4]https://github.com/ourownstory/AR-Net
作者:Esmaeil Alizadeh
deephub翻譯組
原文地址:https://towardsdatascience.com/neural-prophet-a-time-series-modeling-library-based-on-neural-networks-dd02dc8d868d