Esempio n. 1
0
def test_breakSeq(data: np.ndarray, seqLength: int):
    """ Tests Utility.breakSeq """

    dataSeq = Utility.breakSeq(data, seqLength)

    # On concatenating the dataSeq, we should get back data
    assert np.array_equal(np.concatenate(dataSeq, axis=0), data)

    # length of each seq except the last should be exactly seqLength
    for seq in dataSeq[:-1]:
        assert seq.shape[0] == seqLength
Esempio n. 2
0
def main():

    n = 21500
    trainN = 21000
    seqLength = 500
    numSeqPlot = 5

    trainData, testData = Utility.trainTestSplit(
        StandardGenerator('long_term').generate(n),
        trainN
    )

    trainSequences = Utility.breakSeq(trainData, seqLength=seqLength)

    # for i in range(numSeqPlot):
    #     Plot.plotDataCols(trainSequences[
    #         np.random.randint(0, len(trainSequences))
    #     ])

    model = LstmForecast(
        forecastHorizon=1,
        stateSize=50,
        activation='tanh',
        numRnnLayers=3
    )

    model.model.summary()

    loss = model.train(
        trainSequences=trainSequences,
        numIterations=15,
        optimizer=tf.keras.optimizers.Adam(
            learning_rate=tf.keras.optimizers.schedules.ExponentialDecay(
                0.01,
                20,
                0.96
            )
        )
    )

    Plot.plotLoss(loss)

    for i in range(numSeqPlot):
        idx = np.random.randint(0, len(trainSequences))
        evalLoss, Ypred = model.evaluate(trainSequences[idx], returnPred=True)
        Ytrue = trainSequences[idx][1:]

        Plot.plotPredTrue(Ypred, Ytrue, 'On Train')

    evalLoss, Ypred = model.evaluate(testData, returnPred=True)
    Ytrue = testData[1:]

    Plot.plotPredTrue(Ypred, Ytrue, 'On Test')
Esempio n. 3
0
def main():
    # The data generator
    dataGenerator = StandardGenerator('long_term')

    # Data for single-sequence methods
    n = 21500
    trainN = 21000
    trainData, testData = Utility.trainTestSplit(
        dataGenerator.generate(n),
        train=trainN
    )

    # Method 1 - train on mutually exclusive sequences
    seqLength = 500
    trainSequences = Utility.breakSeq(trainData, seqLength)
    tryModelOneSeq(trainSequences, testData, 'method1', PLOT_DIR)

    # Method 2 - train on randomly sampled contiguous sequences
    seqLength = 500
    numSeq = 42
    trainSequences = [
        trainData[startIdx: startIdx + seqLength]
        for startIdx in list(np.random.randint(
            0,
            trainN - seqLength,
            size=(numSeq,)
        ))
    ]
    tryModelOneSeq(trainSequences, testData, 'method2', PLOT_DIR)

    # Method 3 - train on the single long sequence
    trainSequences = [trainData]
    tryModelOneSeq(trainSequences, testData, 'method3', PLOT_DIR)

    # Multiple Independent Train Sequences
    seqLength = 500
    numSeq = 42
    trainSequences = Utility.generateMultipleSequence(
        dataGenerator=dataGenerator,
        numSequences=numSeq,
        minSequenceLength=seqLength,
        maxSequenceLength=seqLength
    )
    testData = dataGenerator.generate(seqLength)
    tryModelMultiSeq(trainSequences, testData, 'multiseq', PLOT_DIR)