예제 #1
0
def test_adam(show=False):

    problem = tigerforecast.problem('ARMA-v0')
    x = problem.initialize(p=2,q=0)

    method = tigerforecast.method('LSTM')
    method.initialize(n=1, m=1, l=3, h=10, optimizer=Adam) # initialize with class
    method.predict(1.0) # call methods to verify it works
    method.update(1.0)

    optimizer = Adam(learning_rate=0.1)
    method = tigerforecast.method('LSTM')
    method.initialize(n=1, m=1, l=3, h=10, optimizer=optimizer) # reinitialize with instance

    loss = []
    for t in range(1000):
        y_pred = method.predict(x)
        y_true = problem.step()
        loss.append(mse(y_pred, y_true))
        method.update(y_true)
        x = y_true

    if show:
        plt.plot(loss)
        plt.show(block=False)
        plt.pause(3)
        plt.close()
    print("test_adam passed")
예제 #2
0
def test_ons(show=False):

    #tigerforecast.set_key(0) # consistent randomness

    problem = tigerforecast.problem('ARMA-v0')
    x, y_true = problem.initialize()

    methods = []
    labels = ['OGD', 'ONS', 'Adam']  # don't run deprecated ONS

    method = tigerforecast.method('LSTM')
    method.initialize(n=1, m=1, optimizer=OGD)  # initialize with class
    methods.append(method)

    #method = tigerforecast.method('AutoRegressor')
    #method.initialize(optimizer=Adagrad) # initialize with class
    #methods.append(method)

    method = tigerforecast.method('LSTM')
    method.initialize(n=1, m=1, optimizer=ONS)  # initialize with class
    methods.append(method)

    #method = tigerforecast.method('AutoRegressor')
    #method.initialize(optimizer=Adam) # initialize with class
    #methods.append(method)

    losses = [[] for i in range(len(methods))]
    update_time = [0.0 for i in range(len(methods))]
    for t in tqdm(range(2000)):
        for i in range(len(methods)):
            l, method = losses[i], methods[i]
            y_pred = method.predict(x)
            l.append(mse(y_pred, y_true))

            t = time.time()
            method.update(y_true)
            update_time[i] += time.time() - t
        x, y_true = problem.step()

    print("time taken:")
    for t, label in zip(update_time, labels):
        print(label + ": " + str(t))

    if show:
        plt.yscale('log')
        for l, label in zip(losses, labels):
            plt.plot(l, label=label)
            #plt.plot(avg_regret(l), label = label)
        plt.legend()
        plt.title("Autoregressors on ENSO-T6")
        plt.show(block=False)
        plt.pause(300)
        plt.close()

    print("test_ons passed")
예제 #3
0
def test_least_squares(steps=1000, show_plot=True):
    T = steps 
    problem = tigerforecast.problem("ENSO-v0")
    x, y = problem.initialize(input_signals = ['nino12', 'nino34', 'nino4'])

    method = tigerforecast.method("LeastSquares")
    method.initialize(x, y, reg = 10.0 * steps)
    loss = lambda y_true, y_pred: np.sum((y_true - y_pred)**2)
 
    results = []

    for i in range(T):
        x, y_true = problem.step()
        y_pred = method.step(x, y_true)
        cur_loss = loss(y_true, y_pred)
        results.append(cur_loss)

    if show_plot:
        plt.plot(results)
        plt.title("LeastSquares method on ARMA problem")
        plt.show(block=False)
        plt.pause(3)
        plt.close()
    print("test_least_squares passed")
    return
예제 #4
0
def test_autoregressor(steps=100, show_plot=True):
    T = steps
    p, q = 3, 3
    n = 3
    problem = tigerforecast.problem("ARMA-v0")
    cur_x = problem.initialize(p, q, n=n)

    method = tigerforecast.method("AutoRegressor")
    #method.initialize(p, optimizer = ONS)
    method.initialize(p, optimizer=Adagrad)
    loss = lambda y_true, y_pred: np.sum((y_true - y_pred)**2)

    results = []

    for i in range(T):
        cur_y_pred = method.predict(cur_x)
        #print(cur_y_pred.shape)
        #method.forecast(cur_x, 3)
        cur_y_true = problem.step()
        cur_loss = loss(cur_y_true, cur_y_pred)
        method.update(cur_y_true)
        cur_x = cur_y_true
        results.append(cur_loss)

    if show_plot:
        plt.plot(results)
        plt.title("Autoregressive method on ARMA problem")
        plt.show(block=False)
        plt.pause(3)
        plt.close()
    print("test_autoregressor passed")
    return
예제 #5
0
def test_rnn(steps=100, show_plot=True):
    T = steps
    p, q = 3, 3
    n = 1
    problem = tigerforecast.problem("ARMA-v0")
    y_true = problem.initialize(p=p, q=q, n=1)
    method = tigerforecast.method("RNN")
    method.initialize(n=1, m=1, l=3, h=1)
    loss = lambda pred, true: np.sum((pred - true)**2)

    results = []
    for i in range(T):
        u = random.normal(generate_key(), (n, ))
        y_pred = method.predict(u)
        y_true = problem.step()
        results.append(loss(y_true, y_pred))
        method.update(y_true)

    if show_plot:
        plt.plot(results)
        plt.title("RNN method on LDS problem")
        plt.show(block=False)
        plt.pause(3)
        plt.close()
    print("test_rnn passed")
    return
예제 #6
0
def test_last_value(steps=1000, show_plot=True):
    T = steps
    p, q = 3, 3
    problem = tigerforecast.problem("ARMA-v0")
    cur_x = problem.initialize(p, q)
    method = tigerforecast.method("LastValue")
    method.initialize()
    loss = lambda y_true, y_pred: (y_true - y_pred)**2

    results = []
    for i in range(T):
        cur_y_pred = method.predict(cur_x)
        #print(method.forecast(cur_x, 3))
        cur_y_true = problem.step()
        cur_loss = loss(cur_y_true, cur_y_pred)
        results.append(cur_loss)
        method.update(cur_y_true)
        cur_x = cur_y_true

    if show_plot:
        plt.plot(results)
        plt.title("LastValue method on ARMA problem")
        plt.show(block=False)
        plt.pause(1)
        plt.close()
    print("test_last_value passed")
    return
예제 #7
0
def test_custom_method(steps=1000, show_plot=True):
    # initial preparation
    T = steps 
    p, q = 3, 3
    loss = lambda y_true, y_pred: (y_true - y_pred)**2
    problem = tigerforecast.problem("ARMA-v0")
    cur_x = problem.initialize(p, q)

    # simple LastValue custom method implementation
    class Custom(tigerforecast.CustomMethod):
        def initialize(self):
            self.x = 0.0
        def predict(self, x):
            self.x = x
            return self.x
        def update(self, y):
            pass

    # try registering and calling the custom method
    tigerforecast.register_custom_method(Custom, "TestCustomMethod")
    custom_method = tigerforecast.method("TestCustomMethod")
    custom_method.initialize()

    # regular LastValue method as sanity check
    reg_method = tigerforecast.method("LastValue")
    reg_method.initialize()
 
    results = []
    for i in range(T):
        cur_y_pred = custom_method.predict(cur_x)
        reg_y_pred = reg_method.predict(cur_x)
        assert cur_y_pred == reg_y_pred # check that CustomMethod outputs the correct thing
        cur_y_true = problem.step()
        custom_method.update(cur_y_true)
        reg_method.update(cur_y_true)
        results.append(loss(cur_y_true, cur_y_pred))
        cur_x = cur_y_true

    if show_plot:
        plt.plot(results)
        plt.title("Custom (last value) method on ARMA problem")
        plt.show(block=False)
        plt.pause(1)
        plt.close()
    print("test_custom_method passed")
    return
예제 #8
0
def test_grid_search_lstm(show=False):
    problem_id = "SP500-v0"
    method_id = "LSTM"
    problem_params = {}  # {'p':4, 'q':1} # params for ARMA problem
    method_params = {'n': 1, 'm': 1}
    loss = lambda a, b: np.sum((a - b)**2)
    search_space = {
        'l': [3, 4, 5, 6],
        'h': [2, 5, 8],
        'optimizer': []
    }  # parameters for ARMA method
    opts = [Adam, Adagrad, ONS, OGD]
    lr_start, lr_stop = -1, -3  # search learning rates from 10^start to 10^stop
    learning_rates = np.logspace(lr_start, lr_stop,
                                 1 + 2 * np.abs(lr_start - lr_stop))
    for opt, lr in itertools.product(opts, learning_rates):
        search_space['optimizer'].append(
            opt(learning_rate=lr))  # create instance and append

    trials, min_steps = 10, 100
    hpo = GridSearch()  # hyperparameter optimizer
    optimal_params, optimal_loss = hpo.search(
        method_id,
        method_params,
        problem_id,
        problem_params,
        loss,
        search_space,
        trials=trials,
        smoothing=10,
        min_steps=min_steps,
        verbose=show)  # run each model at least 1000 steps

    if show:
        print("optimal params: ", optimal_params)
        print("optimal loss: ", optimal_loss)

    # test resulting method params
    method = tigerforecast.method(method_id)
    method.initialize(**optimal_params)
    problem = tigerforecast.problem(problem_id)
    x = problem.initialize(**problem_params)
    loss = []
    if show:
        print("run final test with optimal parameters")
    for t in range(5000):
        y_pred = method.predict(x)
        y_true = problem.step()
        loss.append(mse(y_pred, y_true))
        method.update(y_true)
        x = y_true

    if show:
        print("plot results")
        plt.plot(loss)
        plt.show(block=False)
        plt.pause(10)
        plt.close()
예제 #9
0
def test_rnn_lstm_arma(steps=100, show_plot=True):
    T = steps
    p, q = 3, 0
    problem = tigerforecast.problem("ARMA-v0")
    cur_x = problem.initialize(p=p, q=q, n=1)

    method_RNN = tigerforecast.method("RNN")
    method_RNN.initialize(1, 1, l=p, h=1)

    method_LSTM = tigerforecast.method("LSTM")
    method_LSTM.initialize(1, 1, l=p, h=1)

    loss = lambda pred, true: np.sum((pred - true)**2)

    results_RNN = []
    results_LSTM = []
    for i in range(T):
        y_pred_RNN = method_RNN.predict(cur_x)
        y_pred_LSTM = method_LSTM.predict(cur_x)
        if (i == 0):
            print(method_RNN.forecast(cur_x, timeline=10))
            print(method_LSTM.forecast(cur_x, timeline=10))
        y_true = problem.step()
        results_RNN.append(loss(y_true, y_pred_RNN))
        results_LSTM.append(loss(y_true, y_pred_LSTM))
        cur_x = y_true
        method_RNN.update(y_true)
        method_LSTM.update(y_true)

    if show_plot:
        plt.plot(results_RNN, label='RNN')
        plt.plot(results_LSTM, label='LSTM')
        plt.legend()
        plt.title("RNN vs. LSTM on ARMA problem")
        plt.show(block=False)
        plt.pause(3)
        plt.close()
    print("test_rnn_lstm_arma passed")
    return
예제 #10
0
def test_autoregressor(show_plot=True):
    # TEST ARMA
    df_site_2231000 = get_site_df()
    seq_len = 61
    rows = df_site_2231000.shape[0]

    label_series = []
    for i in range(int(rows / seq_len)):
        label_series += ast.literal_eval(
            df_site_2231000[past_sequence_label_feature_name].iloc[61 * i])

    method = tigerforecast.method("AutoRegressor")
    #method.initialize(p, optimizer = ONS)
    method.initialize(p=3, n=1)
    loss = lambda y_true, y_pred: (y_true - y_pred)**2

    results = []
    running_average_loss = []

    for i in range(len(label_series) - 1):
        cur_y_pred = method.predict(label_series[i])
        cur_y_true = label_series[i + 1]
        cur_loss = loss(cur_y_true, cur_y_pred)
        method.update(cur_y_true)
        results.append(cur_loss)
        total_loss = np.sum(results[-100:]) / 100
        running_average_loss.append(total_loss)

    tigerforecast_dir = get_tigerforecast_dir()
    data_path = os.path.join(tigerforecast_dir, 'data/loss_site_2231000.csv')
    try:
        csv_input = pd.read_csv(data_path)
    except:
        csv_input = pd.DataFrame([])
    if 'ARMA_100_window_average_loss' not in csv_input:
        csv_input['ARMA_100_window_average_loss'] = running_average_loss
        csv_input.to_csv(data_path, index=False)

    if show_plot:
        plt.figure()
        plt.plot(results)
        plt.title("Autoregressive loss on " + past_sequence_label_feature_name)
        # plt.close()
        plt.figure()
        plt.plot(running_average_loss)
        plt.title("Autoregressive 100-window average loss on " +
                  past_sequence_label_feature_name)
        plt.show(block=True)
예제 #11
0
    def initialize(self, method_id, method_params, n = None, m = None, N=3, loss=mse, reg=0.0):
        """
        Description: Initializes autoregressive method parameters
        Args:
            method_id (string): id of weak learner method
            method_params (dict): dict of params to pass method
            N (int): default 3. Number of weak learners
            loss (function): loss function for boosting method
            reg (float): default 1.0. constant for regularization.
        """
        self.initialized = True

        # initialize proxy loss
        proxy_loss = lambda y_pred, v: np.dot(y_pred, v) + (reg/2) * np.sum(y_pred**2)

        # 1. Maintain N copies of the algorithm 
        assert N > 0
        self.N = N
        self.methods = []
        method_params['n'] = n
        method_params['m'] = m
        for _ in range(N):
            new_method = tigerforecast.method(method_id)
            new_method.initialize(**method_params)
            new_method.optimizer.set_loss(proxy_loss) # proxy loss
            self.methods.append(new_method)

        def _prev_predict(x):
            y = []
            cur_y = 0
            for i, method_i in enumerate(self.methods):
                eta_i = 2 / (i + 2)
                y_pred = method_i.predict(x)
                cur_y = (1 - eta_i) * cur_y + eta_i * y_pred
                y.append(cur_y)
            return [np.zeros(shape=y[0].shape)] + y
        self._prev_predict = _prev_predict

        def _get_grads(y_true, prev_predicts):
            g = jax.grad(loss)
            v_list = [g(y_prev, y_true) for y_prev in prev_predicts]
            return v_list
        self._get_grads = jax.jit(_get_grads)
예제 #12
0
    def _run_test(self, method_params, smoothing, min_steps, verbose=0):
        """ Run a single test with given method params, using median stopping rule """
        # initialize problem and method
        if verbose:
            print("Currently testing parameters: " + str(method_params))
        method = tigerforecast.method(self.method_id)
        method.initialize(**method_params)
        problem = tigerforecast.problem(self.problem_id)
        if problem.has_regressors:
            x, y_true = problem.initialize(**self.problem_params)
        else:
            x = problem.initialize(**self.problem_params)

        t = 0
        losses = []  # sorted losses, used to get median
        smooth_losses = np.zeros(
            smoothing)  # store previous losses to get smooth loss
        while True:  # run method until worse than median loss, ignoring first 100 steps
            t += 1
            y_pred = method.predict(x)
            if problem.has_regressors:
                method.update(y_true)
                loss = self.loss(y_pred, y_true)
            else:
                x = problem.step()
                method.update(x)
                loss = self.loss(y_pred, x)
            if t == 1:  # fill all of smooth_losses with the first loss
                for i in range(smoothing):
                    smooth_losses = self._update_smoothing(smooth_losses, loss)
            else:  # else replace only the oldest loss
                smooth_losses = self._update_smoothing(smooth_losses, loss)
            smooth_loss = np.mean(smooth_losses)
            if t % smoothing == 0:
                self._add_to_list(losses, smooth_loss)
                if self._halting_rule(losses, smooth_loss) and t >= min_steps:
                    break
        if verbose:
            print("Time taken: {}, final loss: {}".format(t, smooth_loss))
        return smooth_loss
예제 #13
0
def test_sgd_autoregressor(show=False):
    problem = tigerforecast.problem('ARMA-v0')
    x = problem.initialize(p=2, q=0)

    optimizer = SGD(learning_rate=0.0003)
    method = tigerforecast.method('AutoRegressor')
    method.initialize(p=3, optimizer=optimizer)  # reinitialize with instance

    loss = []
    for t in range(1000):
        y_pred = method.predict(x)
        y_true = problem.step()
        loss.append(mse(y_pred, y_true))
        method.update(y_true)
        x = y_true

    if show:
        plt.title("Test SGD on ARMA(3) with AutoRegressor method")
        plt.plot(loss)
        plt.show(block=False)
        plt.pause(3)
        plt.close()
예제 #14
0
def test_custom_problem(steps=1000, show=True):
    # initial preparation
    T = steps
    loss = lambda y_true, y_pred: (y_true - y_pred)**2
    method = tigerforecast.method("LastValue")
    method.initialize()

    # simple custom Problem that returns alternating +/- 1.0
    class Custom(tigerforecast.CustomProblem):
        def initialize(self):
            self.T = 0
            return -1

        def step(self):
            self.T += 1
            return 2 * (self.T % 2) - 1

    # try registering and calling the custom problem
    tigerforecast.register_custom_problem(Custom, "TestCustomProblem")
    custom_problem = tigerforecast.problem("TestCustomProblem")
    cur_x = custom_problem.initialize()

    results = []
    for i in range(T):
        cur_y_pred = method.predict(cur_x)
        cur_y_true = custom_problem.step()
        results.append(loss(cur_y_true, cur_y_pred))
        cur_x = cur_y_true

    if show:
        plt.plot(results)
        plt.title("LastValue method on custom alternating problem")
        plt.show(block=False)
        plt.pause(2)
        plt.close()
    print("test_custom_problem passed")
    return
예제 #15
0
def test_lstm(show_plot=True):
    n, m, l, h = 7, 1, 3, 10

    method = tigerforecast.method("LSTM")
    method.initialize(n, m, l, h, optimizer=Adam)
    loss = lambda pred, true: (pred - true)**2

    features = process_features()

    loss_series = []
    for i in range(len(features) - 1):
        x = features[i]
        y_pred = method.predict(x)[0]
        y_true = features[i + 1][-1]
        # print("y_pred: " + str(y_pred))
        # print("y_true: " + str(y_true))
        loss_series.append(loss(y_true, y_pred))
        method.update(y_true)
    # print(loss_series[-10:])

    # save results
    tigerforecast_dir = get_tigerforecast_dir()
    data_path = os.path.join(tigerforecast_dir, 'data/loss_site_2231000.csv')
    try:
        csv_input = pd.read_csv(data_path)
    except:
        csv_input = pd.DataFrame([])
    if 'LSTM_adam_loss' not in csv_input:
        csv_input['LSTM_adam_loss'] = loss_series
        csv_input.to_csv(data_path, index=False)

    if show_plot:
        plt.plot(loss_series)
        plt.title("LSTM-Adam loss on site 2231000")
        plt.show(block=True)
        plt.close()
예제 #16
0
def test_simple_boost_arma(steps=500, show=True):
    # method initialize
    T = steps
    method_id = "AutoRegressor"
    method_params = {'p': 18, 'optimizer': OGD}
    Ns = [64]
    timelines = [6, 9, 12]

    # regular AutoRegressor for comparison
    autoreg = tigerforecast.method("AutoRegressor")
    autoreg.initialize(p=18, optimizer=OGD)

    fig, ax = plt.subplots(nrows=1, ncols=3)
    cur = 0

    # run all boosting method
    for timeline in timelines:

        # problem initialize
        problem = tigerforecast.problem("ENSO-v0")
        x, y_true = problem.initialize(input_signals=['oni'],
                                       timeline=timeline)
        methods = []

        for n in Ns:  # number of weak learners
            method = tigerforecast.method("SimpleBoost")
            method.initialize(method_id, method_params, n,
                              reg=0.0)  # regularization
            methods.append(method)

        result_list = [[] for n in Ns]
        autoreg_loss = []

        for i in tqdm(range(T)):

            # predictions for every boosting method
            for result_i, method_i in zip(result_list, methods):
                y_pred = method_i.predict(x)
                result_i.append(mse(y_true, y_pred))
                method_i.update(y_true)

            # last value and autoregressor predictions
            autoreg_loss.append(mse(autoreg.predict(x), y_true))
            autoreg.update(y_true)
            x, y_true = problem.step()

        # plot performance
        if show:

            start = T // 2

            # plot every boosting method loss
            for n, results in zip(Ns, result_list):
                print("Mean loss for n={}: {}".format(
                    n, np.mean(np.array(results))))
                ax[cur].plot(avg_regret(results[-start:]),
                             label="SimpleBoost, n={}".format(n))

            # plot loss for last value and autoregressor methods
            print("Mean loss for AutoRegressor: {}".format(
                np.mean(np.array(autoreg_loss))))
            ax[cur].plot(avg_regret(autoreg_loss[-start:]),
                         label="AutoRegressor method")
            ax[cur].legend(loc="upper right", fontsize=8)

        cur += 1

    fig.tight_layout()
    plt.show()
예제 #17
0
def run_experiment(problem,
                   method,
                   metric='mse',
                   lr_tuning=True,
                   key=0,
                   timesteps=None,
                   verbose=0):
    '''
    Description: Initializes the experiment instance.
    
    Args:
        problem (tuple): problem id and parameters to initialize the specific problem instance with
        method (tuple): method id and parameters to initialize the specific method instance with
        metric (string): metric we are interesting in computing for current experiment
        key (int): for reproducibility
        timesteps(int): number of time steps to run experiment for
    Returns:
        loss (list): loss series for the specified metric over the entirety of the experiment
        time (float): time elapsed
        memory (float): memory used
    '''
    set_key(key)

    # extract specifications
    (problem_id, problem_params) = problem
    (method_id, method_params) = method

    loss_fn = metrics[metric]

    # initialize problem
    problem = tigerforecast.problem(problem_id)

    if (problem_params is None):
        init = problem.initialize()
    else:
        init = problem.initialize(**problem_params)

    # get first few x and y
    if (problem.has_regressors):
        x, y = init
    else:
        x, y = init, problem.step()

    # initialize method
    method = tigerforecast.method(method_id)

    if (method_params is None):
        method_params = {}
    try:
        method_params['n'] = x.shape[0]
    except:
        method_params['n'] = 1
    try:
        method_params['m'] = y.shape[0]
    except:
        method_params['m'] = 1

    if (lr_tuning):
        method_params = tune_lr(method_id, method_params, problem_id,
                                problem_params)

    method.initialize(**method_params)
    if (timesteps is None):
        if (problem.max_T == -1):
            print(
                "WARNING: On simulated problem, the number of timesteps should be specified. Will default to 5000."
            )
            timesteps = 5000
        else:
            timesteps = problem.max_T - method.p - 2
    elif (problem.max_T != -1):
        timesteps = min(timesteps, problem.max_T - method.p - 2)

    for i in range(method.p):
        method.predict(x)
        new = problem.step()
        #print('x:{0}, y:{1}'.format(x,y))
        if (problem.has_regressors):
            x, y = new
        else:
            x, y = y, new

    #print('history:{0}'.format(method.past))
    if (verbose and key == 0):
        print("Running %s on %s..." % (method_id, problem_id))

    loss = np.zeros(timesteps)
    time_start = time.time()
    memory = 0
    load_bar = False
    if (verbose == 2):
        load_bar = True
    # get loss series
    for i in tqdm(range(timesteps), disable=(not load_bar or key != 0)):
        # get loss and update method
        try:  #this avoids exceptions usually caused by ONS
            cur_loss = float(loss_fn(y, method.predict(x)))
            loss = jax.ops.index_update(loss, i, cur_loss)
            method.update(y)
            # get new pair of observation and label
            new = problem.step()
            if (problem.has_regressors):
                x, y = new
            else:
                x, y = y, new
        except:
            loss = jax.ops.index_update(loss, i, float('nan'))

    return loss, time.time() - time_start, memory
예제 #18
0
def test_flood_FL(steps=61, show_plot=True):
    T = steps
    n, m, l, d = 6, 1, 3, 10
    # problem = tigerforecast.problem("LDS-Control-v0")
    # y_true = problem.initialize(n, m, d)
    tigerforecast_dir = get_tigerforecast_dir()
    data_path = os.path.join(tigerforecast_dir, 'data/FL_train.csv')
    df = pd.read_csv(data_path).head()
    # print(df['static:drain_area_log2'].head())
    # arr_results = onp.load(data_path, encoding='bytes')
    method = tigerforecast.method("LSTM")
    method.initialize(n, m, l, d)
    loss = lambda pred, true: np.sum(((pred - true) / true)**2)
    # num_batches = len(arr_results)
    # for i in range(num_batches):
    #    print("num_measurements:" + str(len(arr_results[i][2])))
    static_features_to_use = [
        ('static:drain_area_log2', False),
        ('train:site:mean:USGS:discharge_mean', False),
        ('train:site:std:USGS:discharge_mean', False),
    ]

    sequence_features_to_use = [
        ('sequence:GSMAP_MERGED:hourlyPrecipRate', False),
        ('sequence:GLDAS21:Tair_f_inst', True), ('sequence:AQUA_VI:NDVI', True)
    ]

    label_feature_name = 'label:USGS:discharge_mean'
    past_sequence_label_feature_name = 'sequence:USGS:discharge_mean'

    # print("num_batches: " + str(num_batches))
    feature_list = []
    # sequence_length = df['sequence:USGS:discharge_mean_shape']
    # print("type(sequence_length):" + str(type(sequence_length)))
    # print("sequence length: " + sequence_length)
    sequence_length = 61
    label_list = []
    for i in range(5):
        for j in range(sequence_length):
            feature = []
            for (seq_feat, b) in sequence_features_to_use:
                # print("seq_feat: " + seq_feat)
                # print("df[seq_feat].shape: " + str(df[seq_feat].shape))
                # print(df[seq_feat])
                # print(type(df[seq_feat].iloc[i]))
                # print(ast.literal_eval(df[seq_feat].iloc[i]))
                feature.append(ast.literal_eval(df[seq_feat].iloc[i])[j])
            for (stat_feat, c) in static_features_to_use:
                feature.append(ast.literal_eval(df[stat_feat].iloc[0])[0])
            feature_list.append(feature)
            label_list.append(
                ast.literal_eval(df[label_feature_name].iloc[0])[0])
    print(feature_list[0])
    print(len(feature_list[0]))
    print(len(feature_list))
    print(len(label_list))
    print(label_list[:10])
    results = []
    for i in range(len(feature_list)):
        u = feature_list[i]
        y_pred = method.predict(u)
        y_true = label_list[i]
        print("y_pred: " + str(y_pred))
        print("y_true: " + str(y_true))
        results.append(loss(y_true, y_pred))
        method.update(y_true)
    print(results[-10:])

    if show_plot:
        plt.plot(results[25:])
        plt.title("LSTM method on FL_train data")
        plt.show(block=True)
        plt.close()
    '''
    for i in range(1000):
        print(i)
        for j in range(61):
            y_pred = method.predict(arr_results[i][2][j])
            y_true = arr_results[i][3][j]
            results.append(loss(y_true, y_pred))
            method.update(y_true)'''
    '''
    if show_plot:
        plt.plot(results)
        plt.title("LSTM method on LDS problem")
        plt.show(block=True)
        plt.close()
    print("test_lstm passed")'''
    return
예제 #19
0
def test_simple_boost_lstm(steps=500, show=True):
    # method initialize
    T = steps
    method_id = "LSTM"
    ogd = OGD(learning_rate=0.01)
    method_params = {'n': 1, 'm': 1, 'l': 5, 'h': 10, 'optimizer': ogd}
    methods = []
    Ns = [1, 3, 6]
    for n in Ns:  # number of weak learners
        method = tigerforecast.method("SimpleBoost")
        method.initialize(method_id, method_params, n,
                          reg=1.0)  # regularization
        methods.append(method)

    # regular AutoRegressor for comparison
    autoreg = tigerforecast.method("AutoRegressor")
    autoreg.initialize(p=4)  # regularization

    # problem initialize
    p, q = 4, 0
    problem = tigerforecast.problem("ARMA-v0")
    y_true = problem.initialize(p, q, noise_magnitude=0.1)

    # run all boosting method
    result_list = [[] for n in Ns]
    last_value = []
    autoreg_loss = []
    for i in range(T):
        y_next = problem.step()

        # predictions for every boosting method
        for result_i, method_i in zip(result_list, methods):
            y_pred = method_i.predict(y_true)
            result_i.append(mse(y_next, y_pred))
            method_i.update(y_next)

        # last value and autoregressor predictions
        last_value.append(mse(y_true, y_next))
        autoreg_loss.append(mse(autoreg.predict(y_true), y_next))
        autoreg.update(y_next)
        y_true = y_next

    # plot performance
    if show:
        start = 100
        x = np.arange(start, steps)
        plt.figure(figsize=(12, 8))

        # plot every boosting method loss
        for n, results in zip(Ns, result_list):
            print("Mean loss for n={}: {}".format(
                n, np.mean(np.array(results[start:]))))
            plt.plot(x,
                     avg_regret(results[start:]),
                     label="SimpleBoost, n={}".format(n))

        # plot loss for last value and autoregressor methods
        print("Mean loss for LastValue: {}".format(
            np.mean(np.array(last_value[start:]))))
        plt.plot(x, avg_regret(last_value[start:]), label="Last value method")
        print("Mean loss for AutoRegressor: {}".format(
            np.mean(np.array(autoreg_loss[start:]))))
        plt.plot(x,
                 avg_regret(autoreg_loss[start:]),
                 label="AutoRegressor method")

        plt.title("SimpleBoost method on ARMA problem")
        plt.legend()
        plt.show(block=False)
        plt.pause(10)
        plt.close()
예제 #20
0
def test_wave_filtering(show_plot=False):
    # state variables
    T, n, m = 1000, 10, 10

    # method variables
    k, eta = 20, 0.00002  # update_steps is an optional variable recommended by Yi
    R_Theta = 5
    R_M = 2 * R_Theta * R_Theta * k**0.5

    # generate random data (columns of Y)
    hidden_state_dim = 5
    h = rand.uniform(generate_key(),
                     minval=-1,
                     maxval=1,
                     shape=(hidden_state_dim, ))  # first hidden state h_0
    A = rand.normal(generate_key(), shape=(hidden_state_dim, hidden_state_dim))
    B = rand.normal(generate_key(), shape=(hidden_state_dim, n))
    C = rand.normal(generate_key(), shape=(m, hidden_state_dim))
    D = rand.normal(generate_key(), shape=(m, n))
    A = (A + A.T) / 2  # make A symmetric
    A = 0.99 * A / np.linalg.norm(A, ord=2)
    if (np.linalg.norm(B) > R_Theta):
        B = B * (R_Theta / np.linalg.norm(B))
    if (np.linalg.norm(C) > R_Theta):
        C = C * (R_Theta / np.linalg.norm(C))
    if (np.linalg.norm(D) > R_Theta):
        D = D * (R_Theta / np.linalg.norm(D))
    if (np.linalg.norm(h) > R_Theta):
        h = h * (R_Theta / np.linalg.norm(h))

    # input vectors are random data
    X = rand.normal(generate_key(), shape=(n, T))

    # generate Y according to predetermined matrices
    Y = []
    for t in range(T):
        Y.append(
            C.dot(h) + D.dot(X[:, t]) +
            rand.truncated_normal(generate_key(), 0, 0.1, shape=(m, )))
        h = A.dot(h) + B.dot(X[:, t]) + rand.truncated_normal(
            generate_key(), 0, 0.1, shape=(hidden_state_dim, ))
    Y = np.array(Y).T  # list to numpy matrix

    method = tigerforecast.method("WaveFiltering")
    method.initialize(n, m, k, T, eta, R_M)
    # loss = lambda y_true, y_pred: (y_true - y_pred)**2
    loss = lambda y_true, y_pred: (np.linalg.norm(y_true - y_pred))**2

    lastvalue_method = tigerforecast.method("LastValue")
    lastvalue_method.initialize()

    results = []
    lastvalue_results = []
    for i in range(T):
        # print(i)
        cur_y_pred = method.predict(X[:, i])
        #print(method.forecast(cur_x, 3))
        cur_y_true = Y[:, i]
        cur_loss = loss(cur_y_true, cur_y_pred)
        results.append(cur_loss)
        method.update(cur_y_true)

        lastvalue_cur_y_pred = lastvalue_method.predict(X[:, i])
        lastvalue_cur_y_true = Y[:, i]
        lastvalue_cur_loss = loss(lastvalue_cur_y_true, lastvalue_cur_y_pred)
        lastvalue_results.append(lastvalue_cur_loss)

    # print("test_wave_filtering passed")
    print(np.linalg.norm(X[:, -1]))
    print(np.linalg.norm(Y[:-1]))
    print(results[-10:-1])
    if show_plot:
        plt.plot(results)
        plt.title("WaveFiltering method on random data")
        plt.show(block=True)

        plt.plot(lastvalue_results)
        plt.title("LastValue method on random data")
        plt.show(block=True)