การใช้งาน TensorFlow/Pytorch | กระดาษ | ผลลัพธ์


เอาต์พุตของเลเยอร์ทั่วไปและตีความได้ของ nbeats
เป็นไปได้ที่จะติดตั้งสองแบ็กเอนด์ในเวลาเดียวกัน
ติดตั้งแบ็กเอนด์ TensorFlow/Keras: pip install nbeats-keras
ติดตั้ง pytorch backend: pip install nbeats-pytorch
การติดตั้งขึ้นอยู่กับ makefile
คำสั่งให้ติดตั้ง n-beats ด้วย keras: make install-keras
คำสั่งในการติดตั้ง n-beats ด้วย pytorch: make install-pytorch
เคล็ดลับนี้ไม่จำเป็นสำหรับ TensorFlow เวอร์ชันล่าสุดอีกต่อไป เพื่อบังคับให้ใช้ประโยชน์จาก GPU (ด้วยแบ็กเอนด์ Keras) ให้เรียกใช้: pip uninstall -y tensorflow && pip install tensorflow-gpu
นี่คือตัวอย่างที่จะทำความคุ้นเคยกับแบ็กเอนด์ทั้งสอง โปรดทราบว่าเฉพาะแบ็กเอนด์ Keras เท่านั้นที่รองรับ input_dim>1 ในขณะนี้
import warnings
import numpy as np
from nbeats_keras . model import NBeatsNet as NBeatsKeras
from nbeats_pytorch . model import NBeatsNet as NBeatsPytorch
warnings . filterwarnings ( action = 'ignore' , message = 'Setting attributes' )
def main ():
# https://keras.io/layers/recurrent/
# At the moment only Keras supports input_dim > 1. In the original paper, input_dim=1.
num_samples , time_steps , input_dim , output_dim = 50_000 , 10 , 1 , 1
# This example is for both Keras and Pytorch. In practice, choose the one you prefer.
for BackendType in [ NBeatsKeras , NBeatsPytorch ]:
# NOTE: If you choose the Keras backend with input_dim>1, you have
# to set the value here too (in the constructor).
backend = BackendType (
backcast_length = time_steps , forecast_length = output_dim ,
stack_types = ( NBeatsKeras . GENERIC_BLOCK , NBeatsKeras . GENERIC_BLOCK ),
nb_blocks_per_stack = 2 , thetas_dim = ( 4 , 4 ), share_weights_in_stack = True ,
hidden_layer_units = 64
)
# Definition of the objective function and the optimizer.
backend . compile ( loss = 'mae' , optimizer = 'adam' )
# Definition of the data. The problem to solve is to find f such as | f(x) - y | -> 0.
# where f = np.mean.
x = np . random . uniform ( size = ( num_samples , time_steps , input_dim ))
y = np . mean ( x , axis = 1 , keepdims = True )
# Split data into training and testing datasets.
c = num_samples // 10
x_train , y_train , x_test , y_test = x [ c :], y [ c :], x [: c ], y [: c ]
test_size = len ( x_test )
# Train the model.
print ( 'Training...' )
backend . fit ( x_train , y_train , validation_data = ( x_test , y_test ), epochs = 20 , batch_size = 128 )
# Save the model for later.
backend . save ( 'n_beats_model.h5' )
# Predict on the testing set (forecast).
predictions_forecast = backend . predict ( x_test )
np . testing . assert_equal ( predictions_forecast . shape , ( test_size , backend . forecast_length , output_dim ))
# Predict on the testing set (backcast).
predictions_backcast = backend . predict ( x_test , return_backcast = True )
np . testing . assert_equal ( predictions_backcast . shape , ( test_size , backend . backcast_length , output_dim ))
# Load the model.
model_2 = BackendType . load ( 'n_beats_model.h5' )
np . testing . assert_almost_equal ( predictions_forecast , model_2 . predict ( x_test ))
if __name__ == '__main__' :
main ()เรียกดูตัวอย่างเพิ่มเติม มันมีสมุดบันทึก Jupyter
สมุดบันทึก Jupyter: nbeats.ipynb: make run-jupyter
@misc{NBeatsPRemy,
author = {Philippe Remy},
title = {N-BEATS: Neural basis expansion analysis for interpretable time series forecasting},
year = {2020},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {url{https://github.com/philipperemy/n-beats}},
}
ขอบคุณ!