Path: blob/main/C4/W2/assignment/C4W2_Assignment.ipynb
3492 views
Week 2: Predicting time series
Welcome! In the previous assignment you got some exposure to working with time series data, but you didn't use machine learning techniques for your forecasts. This week you will be using a deep neural network to create one step forecasts to see how this technique compares with the ones you already tried out. Once again all of the data is going to be generated.
TIPS FOR SUCCESSFUL GRADING OF YOUR ASSIGNMENT:
All cells are frozen except for the ones where you need to submit your solutions or when explicitly mentioned you can interact with it.
You can add new cells to experiment but these will be omitted by the grader, so don't rely on newly created cells to host your solution code, use the provided places for this.
You can add the comment # grade-up-to-here in any graded cell to signal the grader that it must only evaluate up to that point. This is helpful if you want to check if you are on the right track even if you are not done with the whole assignment. Be sure to remember to delete the comment afterwards!
Avoid using global variables unless you absolutely have to. The grader tests your code in an isolated environment without running all cells from the top. As a result, global variables may be unavailable when scoring your submission. Global variables that are meant to be used will be defined in UPPERCASE.
To submit your notebook, save it and then click on the blue submit button at the beginning of the page.
Let's get started!
Generating the data
First things first, you will need to generate your time series data.
The next cell includes a bunch of helper functions to generate and plot the time series. These are very similar to those you saw on Week 1.
Now, define a function to generate the time series, using the functions from the previous cell. This function should return a time series that has trend, seasonality and noise.
Defining some useful global variables
Next, you will define some global variables that will be used throughout the assignment. Feel free to reference them in the upcoming exercises:
SPLIT_TIME: time index to split between train and validation sets
WINDOW_SIZE: length of the window to use for smoothing the series
BATCH_SIZE: batch size for training the model
SHUFFLE_BUFFER_SIZE: number of elements from the dataset used to sample for a new shuffle of the dataset. For more information about the use of this variable you can take a look at the docs.
A note about grading:
When you submit this assignment for grading these same values for these globals will be used so make sure that all your code works well with these values. After submitting and passing this assignment, you are encouraged to come back here and play with these parameters to see the impact they have in the classification process. Since this next cell is frozen, you will need to copy the contents into a new cell and run it to overwrite the values for these globals.
Finally, put everything together and create the times series you will use for this assignment.
Splitting the data
As usual, you will need a function to split the data between train and validation sets. Since you already coded the train_val_split function during last week's assignment, this time it is provided for you:
Processing the data
Exercise 1: windowed_dataset
As you saw on the lectures, you can feed the data for training by creating a TF Dataset with the appropriate processing steps such as windowing, flattening, batching and shuffling. Remember you can do all these using the different methods of the tf.data.Dataset object. Next, complete the windowed_dataset function below that effectively pre-processes your time series and returns a TF Dataset.
This function receives a series and a window_size, and returns a TF Dataset. You should already be familiar with tf.data.Dataset objects from the this week's lectures, but be sure to check out the docs if you need any help.
To test your function you will be using a window_size of 10 which means that you will use 10 consecutive values to predict the next one. You will also set the parameter shuffle=False. Given this, the first element of the batch of features should be identical to the first 15 elements of the series_train, and the batch of labels should be equal to elements 10 through 42 of the series_train.
Expected Output:
Now plot the first item in the batch. You will be displayng the 20 features, followed by the label, which is the value you want to predict.
Now that you have tested your windowed_dataset function, use it to create your train dataset. For that, just run the cell below
Defining the model architecture
Exercise 2: create_model
Now that you have a function that will process the data before it is fed into your neural network for training, it is time to define you model architecture.
Complete the create_model function below. Notice that this function receives the window_size since this will be an important parameter for the first layer of your network.
Remember that this time you are predicting the values of a time series, so use an appropriate loss for this task. There are many you can choose for, but for grading purposes, please stick to 'mse'.
Hint:
You will only need
Denselayers.The training should be really quick so if you notice that each epoch is taking more than a few seconds, consider trying a different architecture.
The next cell allows you to check the number of total and trainable parameters of your model and prompts a warning in case these exceeds those of a reference solution, this serves the following 3 purposes listed in order of priority:
Helps you prevent crashing the kernel during training.
Helps you avoid longer-than-necessary training times.
Provides a reasonable estimate of the size of your model. In general you will usually prefer smaller models given that they accomplish their goal successfully.
Notice that this is just informative and may be very well below the actual limit for size of the model necessary to crash the kernel. So even if you exceed this reference you are probably fine. However, if the kernel crashes during training or it is taking a very long time and your model is larger than the reference, come back here and try to get the number of parameters closer to the reference.
Expected output:
Before going any further, check that the input and output dimensions of your model are correct. Do this by running the cell below:
You can also print a summary of your model to see what the architecture looks like.
Now go ahead and plot the training loss so you can monitor the learning process.
Evaluating the forecast
Now it is time to evaluate the performance of the forecast. For this you can use the compute_metrics function that you coded in the previous assignment:
You will also be generating predict_forecast function, that simply computes predictions for all values in the validation data.
Now, go ahead and make the predictions. This run should take no more time than the actual training.
You can now plot the true series, and the predicted series in order to get a visual estimate of how good your model is doing.
Expected Output:
A series similar to this one:
Finally, go ahead and compute the MSE and MAE metrics using the compute_metrics function you defined earlier.
To pass this assignment your forecast should achieve an MSE of 30 or less.
If your forecast didn't achieve this threshold try re-training your model with a different architecture or tweaking the optimizer's parameters.
If your forecast did achieve this threshold run the following cell to save the MSE in a binary file which will be used for grading and after doing so, submit your assigment for grading.
Congratulations on finishing this week's assignment!
You have successfully implemented a neural network capable of forecasting time series while also learning how to leverage Tensorflow's Dataset class to process time series data!
Keep it up!