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

# 1. Importer les données

```{r}
library(kernlab)
data(spam)
```

```{r,message=FALSE,warning=FALSE}
library(keras)
spamX <- as.matrix(spam[,-58])
spamY <- to_categorical(as.numeric(spam$type)-1, 2)
```

```{r,message=FALSE,warning=FALSE}
set.seed(5678)
perm <- sample(4601,3000)
appX <- spamX[perm,]
appY <- spamY[perm,]
validX <- spamX[-perm,]
validY <- spamY[-perm,]
```

# 2. Construire un réseau et optimiser les paramètres

```{r,message=FALSE,warning=FALSE}
use_session_with_seed(42)
mod.1couche <- keras_model_sequential() %>%
  layer_dense(units=2, activation ="softmax")
```

```{r,message=FALSE,warning=FALSE}
mod.1couche %>% compile(loss = "categorical_crossentropy",
                        optimizer=optimizer_rmsprop(),metrics=c("accuracy"))
hist.1couche <- mod.1couche %>%
  fit(appX,appY,epochs=30,validation_split=0.2)
```

```{r}
plot(hist.1couche)
```

```{r,message=FALSE,warning=FALSE}
hist.1couche <- mod.1couche %>%
  fit(appX,appY,epochs=30,validation_split=0)
```

```{r}
mod.2couches <- keras_model_sequential() %>%
  layer_dense(units=30, activation ="relu") %>%
  layer_dense(units=2, activation ="softmax")
```

```{r,message=FALSE,warning=FALSE}
mod.2couches %>% compile(loss = "categorical_crossentropy",
                         optimizer=optimizer_rmsprop(),metrics=c("accuracy"))
hist.2couches <- mod.2couches %>%
  fit(appX,appY,epochs=30,validation_split=0)
```

# 3. Faire de la prévision

```{r}
predict(mod.1couche, validX)[1:3,]
apply(predict(mod.1couche, validX), 1, which.max)[1:3]
predict(mod.2couches, validX)[1:3,]
apply(predict(mod.2couches, validX), 1, which.max)[1:3]
```

# 4. Sélectionner les caractéristiques d’un réseau

```{r,message=FALSE,warning=FALSE}
library(caret)
app <- spam[perm, ]
valid <- spam[-perm, ]
param_grid <- expand.grid(size = c(15, 30, 45), lambda = 0,
                          batch_size = 32, lr = 0.001, rho = 0.9,
                          decay = 0, activation = c("relu", "tanh"))
caret_mlp <- train(type ~ . , data = spam,
                   method = "mlpKerasDecay",
                   tuneGrid = param_grid,
                   epoch = 30, verbose = 0,
                   trControl = trainControl(method="cv",number=5))
```

```{r}
caret_mlp
```

```{r}
predict(caret_mlp, newdata = spam[1:3,])
```

# 5. Estimer les performances du réseau

```{r}
mean(predict(caret_mlp, newdata = valid)==valid[["type"]])
```

