Code for Session 11

Author

Dr. Richard M. Crowley

Note that the directories used to store data are likely different on your computer, and such references will need to be changed before using any such code.

library(knitr)
library(kableExtra)

#' Pretty formatted HTML tables
#' 
#' @param df The dataframe to format as a table
#' @param names Names to use for columns if not using the df column names. Vector
#' @param highlight_cols Columns to highlight. Default is c(1) for highlighting the first row. Use c() to remove.
#' @param highlight_rows Rows to highlight. Default is c() to not highlight any rows.
#' @param color_rows Rows to highlight in color. Default is c() to not highlight any rows.
#' @param highlight_end_rows Rows to highlight. Uses a reverse indexing where 1 is the last row, 2 is second to last, etc.
#' @param full Use to span the full width of a slide. FALSE by default.
#' @param fixed_header Whether the first row should be treated as a header row. TRUE by default.
html_df <- function(df, names=NULL, highlight_cols=c(1), highlight_rows=c(), color_rows=c(), highlight_end_rows=c(), full=F, fixed_header=T) {
  if(!length(names)) {
    names=colnames(df)
  }
  kbl(df,"html", col.names = names, align = c("l",rep('c',length(cols)-1))) %>%
    kable_paper(c("striped","hover"), full_width=full, fixed_thead=F, html_font = "\"Source Sans Pro\", Helvetica, sans-serif") %>%
    {if(length(highlight_cols)) column_spec(., highlight_cols,bold=T, background = "#ffffff99") else .} %>%
    {if(length(highlight_rows)) row_spec(., highlight_rows,bold=T, background = "#ffffff99") else .} %>%
    {if(length(color_rows)) row_spec(., color_rows,bold=T, background = "#aaaaff99") else .} %>%
    {if(length(highlight_end_rows)) row_spec(., nrow(df) + 1 - highlight_end_rows,bold=T, background = "#ffffff99") else .}
}

library(tidyverse)
print("Hello world!")
[1] "Hello world!"
library(keras)
model <- load_model_hdf5("../../Data/Session_11-mnist_model.h5")
history <- readRDS('../../Data/Session_11-mnist_history.rds')
eval <- readRDS('../../Data/Session_11-mnist_eval.rds')
summary(model)
Model: "sequential_1"
________________________________________________________________________________
 Layer (type)                       Output Shape                    Param #     
================================================================================
 dense (Dense)                      (None, 256)                     200960      
 dropout (Dropout)                  (None, 256)                     0           
 dense_1 (Dense)                    (None, 128)                     32896       
 dropout_1 (Dropout)                (None, 128)                     0           
 dense_2 (Dense)                    (None, 10)                      1290        
================================================================================
Total params: 235,146
Trainable params: 235,146
Non-trainable params: 0
________________________________________________________________________________
plot(history)

eval
$loss
[1] 0.1117176

$accuracy
[1] 0.9812
model <- load_model_hdf5("../../Data/Session_11-tweet_model.h5")
history <- readRDS('../../Data/Session_11-tweet_history.rds')
eval <- readRDS('../../Data/Session_11-tweet_eval.rds')
summary(model)
Model: "sequential"
________________________________________________________________________________
 Layer (type)                  Output Shape               Param #    Trainable  
================================================================================
 conv2d (Conv2D)               (None, 254, 254, 32)       896        Y          
 re_lu (ReLU)                  (None, 254, 254, 32)       0          Y          
 conv2d_1 (Conv2D)             (None, 252, 252, 16)       4624       Y          
 leaky_re_lu (LeakyReLU)       (None, 252, 252, 16)       0          Y          
 batch_normalization (BatchNor  (None, 252, 252, 16)      64         Y          
 malization)                                                                    
 max_pooling2d (MaxPooling2D)  (None, 126, 126, 16)       0          Y          
 dropout (Dropout)             (None, 126, 126, 16)       0          Y          
 flatten (Flatten)             (None, 254016)             0          Y          
 dense (Dense)                 (None, 20)                 5080340    Y          
 activation (Activation)       (None, 20)                 0          Y          
 dropout_1 (Dropout)           (None, 20)                 0          Y          
 dense_1 (Dense)               (None, 2)                  42         Y          
 activation_1 (Activation)     (None, 2)                  0          Y          
================================================================================
Total params: 5,085,966
Trainable params: 5,085,934
Non-trainable params: 32
________________________________________________________________________________
plot(history)

eval
$loss
[1] 0.7535837

$accuracy
[1] 0.6572581
history <- readRDS('../../Data/Session_11-tweet_history-30epoch.rds')
plot(history)