Exemplo n.º 1
0
def test_breakTrainSeq(targetSeries, exogenousSeries, seqLength,
                       forecastHorizon):
    """ Tests Utility.breakTrainSeq """

    n = targetSeries.shape[0]

    trainSequences = Utility.breakTrainSeq(targetSeries, exogenousSeries,
                                           seqLength, forecastHorizon)

    # If exogenousSeries is none, breakTrainSeq behaves differently,
    # here we test that behaviour
    if exogenousSeries is None:
        # On concatenating the trainSequences, we should get back data
        assert np.array_equal(np.concatenate(trainSequences, axis=0),
                              targetSeries)

        # length of each seq except the last should be exactly seqLength
        for seq in trainSequences[:-1]:
            assert seq.shape[0] == seqLength

        return

    # Forecast horizon cannot be None
    assert forecastHorizon is not None

    # Target and Exogenous series must have same number of elements
    assert targetSeries.shape[0] == exogenousSeries.shape[0]

    # Check if the train sequences are correct
    startIdx = 0
    for (targetSeriesSeq, exogenousSeriesSeq) in trainSequences:
        lenTargetSeries = targetSeriesSeq.shape[0]
        lenExogenousSeries = exogenousSeriesSeq.shape[0]

        assert lenTargetSeries == lenExogenousSeries + forecastHorizon

        exoEndIdx = startIdx + lenExogenousSeries
        targetEndIdx = exoEndIdx + forecastHorizon

        # Check if the broken sequence matches the correct part of the
        # original sequence
        assert np.array_equal(targetSeriesSeq,
                              targetSeries[startIdx:targetEndIdx])
        assert np.array_equal(exogenousSeriesSeq,
                              exogenousSeries[startIdx:exoEndIdx])

        startIdx = exoEndIdx

    assert (startIdx + forecastHorizon
            == n) or (n - startIdx <= forecastHorizon)
Exemplo n.º 2
0
def main():

    n = 20200
    trainN = 20000
    seqLength = 500

    data = np.expand_dims(StandardGenerator('long_term').generate(n), axis=1)
    trainData, testData = Utility.trainTestSplit(data, trainN)

    trainSequences = Utility.breakTrainSeq(trainData, None, seqLength)

    forecastHorizon = 1
    lag = 30

    model = DeepNN(
        forecastHorizon=forecastHorizon,
        lag=lag,
        numUnitsPerLayer=10,
        numLayers=2,
        numTargetVariables=1,
        numExoVariables=0
    )

    loss = model.train(
        trainSequences=trainSequences,
        numIterations=20,
        optimizer=tf.keras.optimizers.Adam(
            learning_rate=tf.keras.optimizers.schedules.ExponentialDecay(
                0.1,
                25,
                0.97
            )
        ),
        verboseLevel=2,
        returnLosses=True
    )

    Plot.plotLoss(loss)

    evalLoss, Ypred = model.evaluate(
        testData,
        returnPred=True
    )

    Ytrue = testData[lag + forecastHorizon:, :]

    print(f'Eval Loss: {evalLoss}')
    Plot.plotPredTrue(Ypred, Ytrue)