update samples from Release-73 as a part of SDK release

This commit is contained in:
amlrelsa-ms
2020-10-30 06:37:05 +00:00
parent e255c000ab
commit 322087a58c

View File

@@ -1,4 +1,5 @@
import argparse import argparse
import os
import numpy as np import numpy as np
import pandas as pd import pandas as pd
@@ -10,7 +11,12 @@ from sklearn.metrics import mean_absolute_error, mean_squared_error
from azureml.automl.runtime.shared.score import scoring, constants from azureml.automl.runtime.shared.score import scoring, constants
from azureml.core import Run from azureml.core import Run
import torch try:
import torch
_torch_present = True
except ImportError:
_torch_present = False
def align_outputs(y_predicted, X_trans, X_test, y_test, def align_outputs(y_predicted, X_trans, X_test, y_test,
@@ -50,7 +56,7 @@ def align_outputs(y_predicted, X_trans, X_test, y_test,
# or at edges of time due to lags/rolling windows # or at edges of time due to lags/rolling windows
clean = together[together[[target_column_name, clean = together[together[[target_column_name,
predicted_column_name]].notnull().all(axis=1)] predicted_column_name]].notnull().all(axis=1)]
return(clean) return (clean)
def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test, def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
@@ -85,8 +91,7 @@ def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
if origin_time != X[time_column_name].min(): if origin_time != X[time_column_name].min():
# Set the context by including actuals up-to the origin time # Set the context by including actuals up-to the origin time
test_context_expand_wind = (X[time_column_name] < origin_time) test_context_expand_wind = (X[time_column_name] < origin_time)
context_expand_wind = ( context_expand_wind = (X_test_expand[time_column_name] < origin_time)
X_test_expand[time_column_name] < origin_time)
y_query_expand[context_expand_wind] = y[test_context_expand_wind] y_query_expand[context_expand_wind] = y[test_context_expand_wind]
# Print some debug info # Print some debug info
@@ -117,8 +122,7 @@ def do_rolling_forecast_with_lookback(fitted_model, X_test, y_test,
# Align forecast with test set for dates within # Align forecast with test set for dates within
# the current rolling window # the current rolling window
trans_tindex = X_trans.index.get_level_values(time_column_name) trans_tindex = X_trans.index.get_level_values(time_column_name)
trans_roll_wind = (trans_tindex >= origin_time) & ( trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
trans_tindex < horizon_time)
test_roll_wind = expand_wind & (X[time_column_name] >= origin_time) test_roll_wind = expand_wind & (X[time_column_name] >= origin_time)
df_list.append(align_outputs( df_list.append(align_outputs(
y_fcst[trans_roll_wind], X_trans[trans_roll_wind], y_fcst[trans_roll_wind], X_trans[trans_roll_wind],
@@ -157,8 +161,7 @@ def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq='D'):
if origin_time != X_test[time_column_name].min(): if origin_time != X_test[time_column_name].min():
# Set the context by including actuals up-to the origin time # Set the context by including actuals up-to the origin time
test_context_expand_wind = (X_test[time_column_name] < origin_time) test_context_expand_wind = (X_test[time_column_name] < origin_time)
context_expand_wind = ( context_expand_wind = (X_test_expand[time_column_name] < origin_time)
X_test_expand[time_column_name] < origin_time)
y_query_expand[context_expand_wind] = y_test[ y_query_expand[context_expand_wind] = y_test[
test_context_expand_wind] test_context_expand_wind]
@@ -188,10 +191,8 @@ def do_rolling_forecast(fitted_model, X_test, y_test, max_horizon, freq='D'):
# Align forecast with test set for dates within the # Align forecast with test set for dates within the
# current rolling window # current rolling window
trans_tindex = X_trans.index.get_level_values(time_column_name) trans_tindex = X_trans.index.get_level_values(time_column_name)
trans_roll_wind = (trans_tindex >= origin_time) & ( trans_roll_wind = (trans_tindex >= origin_time) & (trans_tindex < horizon_time)
trans_tindex < horizon_time) test_roll_wind = expand_wind & (X_test[time_column_name] >= origin_time)
test_roll_wind = expand_wind & (
X_test[time_column_name] >= origin_time)
df_list.append(align_outputs(y_fcst[trans_roll_wind], df_list.append(align_outputs(y_fcst[trans_roll_wind],
X_trans[trans_roll_wind], X_trans[trans_roll_wind],
X_test[test_roll_wind], X_test[test_roll_wind],
@@ -244,7 +245,6 @@ parser.add_argument(
'--model_path', type=str, dest='model_path', '--model_path', type=str, dest='model_path',
default='model.pkl', help='Filename of model to be loaded') default='model.pkl', help='Filename of model to be loaded')
args = parser.parse_args() args = parser.parse_args()
max_horizon = args.max_horizon max_horizon = args.max_horizon
target_column_name = args.target_column_name target_column_name = args.target_column_name
@@ -252,7 +252,6 @@ time_column_name = args.time_column_name
freq = args.freq freq = args.freq
model_path = args.model_path model_path = args.model_path
print('args passed are: ') print('args passed are: ')
print(max_horizon) print(max_horizon)
print(target_column_name) print(target_column_name)
@@ -280,13 +279,19 @@ X_lookback_df = lookback_dataset.drop_columns(columns=[target_column_name])
y_lookback_df = lookback_dataset.with_timestamp_columns( y_lookback_df = lookback_dataset.with_timestamp_columns(
None).keep_columns(columns=[target_column_name]) None).keep_columns(columns=[target_column_name])
# Load the trained model with torch. _, ext = os.path.splitext(model_path)
if torch.cuda.is_available(): if ext == '.pt':
# Load the fc-tcn torch model.
assert _torch_present
if torch.cuda.is_available():
map_location = map_location_cuda map_location = map_location_cuda
else: else:
map_location = 'cpu' map_location = 'cpu'
with open(model_path, 'rb') as fh: with open(model_path, 'rb') as fh:
fitted_model = torch.load(fh, map_location=map_location) fitted_model = torch.load(fh, map_location=map_location)
else:
# Load the sklearn pipeline.
fitted_model = joblib.load(model_path)
if hasattr(fitted_model, 'get_lookback'): if hasattr(fitted_model, 'get_lookback'):
lookback = fitted_model.get_lookback() lookback = fitted_model.get_lookback()