---
title: "Neural Networks Module - Examples overview"
subtitle: "TMA4268 Statistical Learning V2018"
author: "Thiago G. Martins, Department of Mathematical Sciences, NTNU"
date: "Spring 2018"
bibliography: references.bib
output: #3rd letter intentation hierarchy
#  beamer_presentation:
###    incremental: true # or >* for one at a time
#  slidy_presentation:
#    font_adjustment: +1  
   prettydoc::html_pretty:
    theme: cayman
    highlight: github
    toc: true
    toc_depth: 4
   # prettydoc::html_pretty:
   #  theme: architect
   #  highlight: github
#  pdf_document:
  #  toc: true
  #  toc_depth: 2

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, include=FALSE}
library(keras)
```

This page provides a simple overview of the examples covered in the lectures.
This lecture note is based on [@chollet2018deep].

# MNIST dataset

## Objective

- classify the digit contained in a image.

## Data type

- We have images such as 

```{r, message=FALSE, warning=FALSE, echo=FALSE}
# Downloading raw data
mnist <- dataset_mnist()

# Training data
train_images <- mnist$train$x
train_labels <- mnist$train$y

# Test data
test_images <- mnist$test$x
test_labels <- mnist$test$y

# Plotting a digit
par(mfrow=c(2,3))
plot(as.raster(train_images[5,,], max = 255))
plot(as.raster(train_images[15,,], max = 255))
plot(as.raster(train_images[25,,], max = 255))
plot(as.raster(train_images[35,,], max = 255))
plot(as.raster(train_images[45,,], max = 255))
plot(as.raster(train_images[55,,], max = 255))
```

- with annotated labels

```{r, message=FALSE, warning=FALSE, echo=FALSE}
table(train_labels)
```

## Problem type

- Multiclass classification based on image data.

# IMDB dataset

## Objective

- classify a movie review as either positive or negative.

## Data type

```{r, message=FALSE, warning=FALSE, cache=TRUE, echo=FALSE}
imdb <- dataset_imdb(num_words = 100000)

train_data <- imdb$train$x
train_labels <- imdb$train$y
test_data <- imdb$test$x
test_labels <- imdb$test$y

word_index <- dataset_imdb_word_index()  
reverse_word_index <- names(word_index)                                    
names(reverse_word_index) <- word_index
decoded_review <- sapply(train_data[[1]], function(index) {                
  word <- if (index >= 3) reverse_word_index[[as.character(index - 3)]]
  if (!is.null(word)) word else "?"
})
```

- We have text data

```{r, message=FALSE, warning=FALSE, echo=FALSE}
paste(decoded_review, collapse = " ")
```

- with annotated labels where 0 stands for negative and 1 stands for positive.

```{r, message=FALSE, warning=FALSE, echo=FALSE}
table(train_labels)
```

## Problem type

- binary classification based on text data. 

# Reuters dataset

## Objective

- classify short news stories into one of 46 topics available.

## Data type

- We have text data

```{r, message=FALSE, warning=FALSE, cache=TRUE, echo=FALSE}
reuters <- dataset_reuters(num_words = 10000)
```

```{r, message=FALSE, warning=FALSE, echo=FALSE}
c(c(train_data, train_labels), c(test_data, test_labels)) %<-% reuters

word_index <- dataset_reuters_word_index()
reverse_word_index <- names(word_index)
names(reverse_word_index) <- word_index
decoded_newswire <- sapply(train_data[[1]], function(index) {
  word <- if (index >= 3) reverse_word_index[[as.character(index - 3)]]
  if (!is.null(word)) word else "?"
})

paste(decoded_newswire, collapse = " ")
```

- with annotated labels going from 0 to 45

```{r, message=FALSE, warning=FALSE, echo=FALSE}
table(train_labels)
```

## Problem type

- multiclass classification based on text data.

# The Boston housing price dataset

## Objective

- Predict the median price of homes in a given Boston suburb in the mid-1970s, given data points about the suburb at the time, such as the crime rate, the local property tax rate, and so on. 

## Data type

- We have a small dataset and 13 numerical features

```{r, message=FALSE, warning=FALSE, cache=TRUE, echo=FALSE}
dataset <- dataset_boston_housing()
```

```{r, message=FALSE, warning=FALSE, echo=FALSE}
c(c(train_data, train_targets), c(test_data, test_targets)) %<-% dataset
str(train_data)
```

- The targets are the median values of owner-occupied homes, in thousands of dollars:

```{r, message=FALSE, warning=FALSE, echo=FALSE}
str(train_targets)
```

## Problem type

- regression based on numerical features.

# References
