Data pre-processing

Vectorize labels

To vectorize the labels, there are two possibilities:

  • We can use one-hot encoding:

Note that there is a built-in way to do this in Keras:

  • We can cast the label list as an integer tensor:

The only change is that we would have to use sparse_categorical_crossentropy loss function instead of the categorical_crossentropy loss function used for one-hot vector labels.

Handling missing values

Input missing values as number zero.

  • In general safe with neural networks, assuming 0 is not already a meaningful value.

Artificially generating training samples with missing entries

  • (Chollet and Allaire 2018) recommends to artificially generate training samples with missing entries
    • In case you expect missing values on the test set without having them on the training set
    • This is not statistically sound. Duplicating data would artificially reduce uncertainty.

Feature engeneering

  • Neural networks are capable of automatically extracting useful features from raw data.

  • Does this mean we do not have to worry about feature engineering as long as you’re using deep neural networks?

    No, for two reasons:

    • Good features still allow you to solve problems more elegantly while using fewer resources.
    • Good features let you solve a problem with far less data.

Training

We have now defined our model, configured the training scheme by specifying a loss function, an optimizer and metrics for evaluation. We can now train and validate our model.

Fit and evaluate

Training the model

  • train_data: Input tensor with the training features.
  • train_labels: Input tensor with the training labels.
  • epochs: Number of passes through the data.
  • batch_size: Number of samples to use for each iteration of mini-batch SGD.

Training and validating a model

  • validation_data: Data on which to evaluate the loss and any model metrics at the end of each epoch.

Different way to train and validate a model

Information leak

  • Be mindful of the following: every time you use feedback from your validation process to tune your model, you leak information about the validation process into the model.
  • This makes the evaluation process less reliable, if done systematically over many iterations.

Predicting

Reference material

This lecture note is based on (Chollet and Allaire 2018).

References

Chollet, F., and J. Allaire. 2018. Deep Learning with R. Manning Publications. https://books.google.no/books?id=xnIRtAEACAAJ.