sethjuarez / numl

Machine Learning for .NET
http://numl.net
MIT License
430 stars 104 forks source link

GRU example #60

Open BernhardGlueck opened 7 years ago

BernhardGlueck commented 7 years ago

First, hats off for this great library, its so much better designed than Encog or Accord.Net ... and DotnetCore support jay !!

On to my question:

You recently added GRU support to the library, and i was wondering how to use it to do time series prediction ( I now the theory behind it etc, i am just wondering about how to use descriptors and generally the library to achieve this. )

Lets assume we have these classes ( i am using a simple, artifical example here, i know that this is not a good model, but it shows the principle )

class SensorReading {
DateTime TimeStamp; double Temperature }

Let assume we have those readings in 1 minute intervals. So we get lets say a List[SensorReading] with 1.000.000 entries.

Now usually i would apply a sliding window to this input so i get the following timeseries entities out of it: (

class SensorTimeSeries { IList[SensorReading] Input; SensorReading Output; }

So i get a List[SensorTimeSeries] out of the List[SensorReading] with lets say 5 Input data points and the corresponding output ( the 6th reading ) to predict and a total of 1.000.000 - 6 entries.

My question is now, how do i create a GRU model for this ? How to train and evaluate this lateron ?

To reiterate my training data would be List[SensorTimeSeries] with 999.994 entries each containing 5 readings as input and one as the to predicted label...

bdschrisk commented 7 years ago

Thanks @BernhardGlueck and apologies for the delay.

The GRU model is still being refined and further developed, so hold off using it for now. When ready, it'll have the same usage as the other models in the library - which is describing the data and fitting a model to it. The sliding window is automagically handled by the internal state representation of the GRU thus predicting on a new sample acts as a sliding window and updates the state for future predictions.

A GRU representation could look like this (untested):

public class SensorReading
{
public DateTime Date { get; set; }
public double Value { get; set; }

public SensorReading Next(IModel timeseriesModel)
{
    return timeseriesModel.Predict(this);
}
}

public void Main()
{
DateTime now = DateTime.Now;
var dataset = new {
    new SensorReading { Date = now, Value = 1 },
    new SensorReading { Date = now.AddMinutes(1), Value = 2 },
    new SensorReading { Date = now.AddMinutes(2), Value = 3 },
    new SensorReading { Date = now.AddMinutes(3), Value = 4 },
};

var descriptor = Descriptor.For<SensorReading>()
               .WithDateTime(i => i.Date, DatePortion.Time)
               .Learn(i => i.Value);

IModel timeseries = new GatedRecurrentGenerator()
                                     .Generate(descriptor, dataset);

SensorReading current = dataset.First();
while (true)
{
    current = current.Next(timeseries);
    Console.WriteLine($"{current.Date}\t={current.Value}");
            Thread.Sleep(TimeSpan.FromMinutes(1)); // :)
}
}

I'll post an update here when it's ready.

BernhardGlueck commented 7 years ago

No apologies neccessary. Thank you for your detailed explanation