Deep Learning with : : CHEAT SHEET Keras TensorFlow Intro INSTALLATION Define Compile Fit Evaluate Predict The keras package uses the Python keras . Keras is a high-level neural networks API You can install all the prerequisites directly from R. developed with a focus on enabling fast • Model • Batch size https://keras.rstudio.com/reference/install_keras.html experimentation. It supports multiple back- • Sequential • Optimiser • Epochs • Evaluate • classes model • Loss • Validation • Plot • probability ends, including TensorFlow, CNTK and . library(keras) See ?install_keras • • Metrics split Multi-GPU install_keras() for GPU instructions TensorFlow is a lower level mathematical model library for building deep neural network https://keras.rstudio.com This installs the required libraries in an Anaconda architectures. The keras makes it The “Hello, World!” environment or virtual environment 'r-'. easy to use Keras and TensorFlow in R. https://www.manning.com/books/deep-learning-with-r of TRAINING AN IMAGE RECOGNIZER ON MNIST DATA Working with keras models # input : use MNIST images DEFINE A MODEL PREDICT CORE LAYERS mnist <- dataset_mnist() x_train <- mnist$train$x; y_train <- mnist$train$y keras_model() Keras Model predict() Generate predictions from a Keras model layer_input() Input layer x_test <- mnist$test$x; y_test <- mnist$test$y keras_model_sequential() Keras Model composed of a linear stack of layers predict_proba() and predict_classes() Generates probability or class probability predictions layer_dense() Add a densely- # reshape and rescale connected NN layer to an output multi_gpu_model() Replicates a model on different for the input samples x_train <- array_reshape(x_train, c(nrow(x_train), 784)) GPUs x_test <- array_reshape(x_test, c(nrow(x_test), 784)) predict_on_batch() Returns predictions for a single layer_activation() Apply an x_train <- x_train / 255; x_test <- x_test / 255 batch of samples activation function to an output COMPILE A MODEL y_train <- to_categorical(y_train, 10) predict_generator() Generates predictions for the layer_dropout() Applies Dropout y_test <- to_categorical(y_test, 10) compile(object, optimizer, loss, metrics = NULL) input samples from a data generator to the input Configure a Keras model for training layer_reshape() Reshapes an # defining the model and layers output to a certain shape model <- keras_model_sequential() FIT A MODEL OTHER MODEL OPERATIONS model %>% layer_dense(units = 256, activation = 'relu', fit(object, x = NULL, y = NULL, batch_size = NULL, summary() Print a summary of a Keras model layer_permute() Permute the input_shape = c(784)) %>% epochs = 10, verbose = 1, callbacks = NULL, …) dimensions of an input according Train a Keras model for a fixed number of epochs to a given pattern layer_dropout(rate = 0.4) %>% (iterations) export_savedmodel() Export a saved model layer_dense(units = 128, activation = 'relu') %>% n layer_repeat_vector() Repeats layer_dense(units = 10, activation = 'softmax’) fit_generator() Fits the model on data yielded batch- get_layer() Retrieves a layer based on either its the input n times name (unique) or index by-batch by a generator # compile (define loss and optimizer) pop_layer() Remove the last layer in a model x f(x) layer_lambda(object, f) Wraps model %>% compile( train_on_batch() test_on_batch() Single gradient arbitrary expression as a layer update or model evaluation over one batch of loss = 'categorical_crossentropy', samples save_model_hdf5(); load_model_hdf5() Save/ optimizer = optimizer_rmsprop(), L1 L2 layer_activity_regularization() Load models using HDF5 Layer that applies an update to metrics = c('accuracy’) the cost function based input ) EVALUATE A MODEL serialize_model(); unserialize_model() activity Serialize a model to an R object # train (fit) layer_masking() Masks a model %>% fit( evaluate(object, x = NULL, y = NULL, batch_size = sequence by using a mask value to clone_model() Clone a model instance x_train, y_train, NULL) Evaluate a Keras model skip timesteps epochs = 30, batch_size = 128, freeze_weights(); unfreeze_weights() evaluate_generator() Evaluates the model on a data validation_split = 0.2 Freeze and unfreeze weights layer_flatten() Flattens an input generator ) model %>% evaluate(x_test, y_test) model %>% predict_classes(x_test)

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at keras.rstudio.com • keras 2.1.2 • Updated: 2017-12 More layers Preprocessing

CONVOLUTIONAL LAYERS ACTIVATION LAYERS SEQUENCE PREPROCESSING Keras TensorFlow layer_conv_1d() 1D, e.g. layer_activation(object, activation) pad_sequences() temporal Apply an to an output Pads each sequence to the same length (length of Pre-trained models the longest sequence) layer_activation_leaky_relu() Keras applications are deep learning models layer_conv_2d_transpose() Leaky version of a rectified linear unit skipgrams() that are made available alongside pre-trained Transposed 2D (deconvolution) Generates skipgram word pairs weights. These models can be used for α layer_activation_parametric_relu() prediction, feature extraction, and fine-tuning. layer_conv_2d() 2D, e.g. spatial Parametric rectified linear unit make_sampling_table() application_xception() convolution over images Generates word rank-based probabilistic sampling xception_preprocess_input() layer_activation_thresholded_relu() table Xception v1 model Thresholded rectified linear unit layer_conv_3d_transpose() Transposed 3D (deconvolution) layer_activation_elu() TEXT PREPROCESSING application_inception_v3() inception_v3_preprocess_input() layer_conv_3d() 3D, e.g. spatial Exponential linear unit text_tokenizer() Text tokenization utility convolution over volumes Inception v3 model, with weights pre-trained on ImageNet fit_text_tokenizer() Update tokenizer internal layer_conv_lstm_2d() vocabulary Convolutional LSTM DROPOUT LAYERS application_inception_resnet_v2() save_text_tokenizer(); load_text_tokenizer() inception_resnet_v2_preprocess_input() layer_separable_conv_2d() layer_dropout() Save a text tokenizer to an external file Inception-ResNet v2 model, with weights Depthwise separable 2D Applies dropout to the input trained on ImageNet texts_to_sequences(); layer_upsampling_1d() layer_spatial_dropout_1d() layer_upsampling_2d() texts_to_sequences_generator() application_vgg16(); application_vgg19() layer_spatial_dropout_2d() Transforms each text in texts to sequence of integers VGG16 and VGG19 models layer_upsampling_3d() layer_spatial_dropout_3d() Upsampling layer Spatial 1D to 3D version of dropout texts_to_matrix(); sequences_to_matrix() application_resnet50() ResNet50 model layer_zero_padding_1d() Convert a list of sequences into a matrix layer_zero_padding_2d() RECURRENT LAYERS application_mobilenet() layer_zero_padding_3d() text_one_hot() One-hot encode text to word indices mobilenet_preprocess_input() Zero-padding layer layer_simple_rnn() mobilenet_decode_predictions() text_hashing_trick() mobilenet_load_model_hdf5() layer_cropping_1d() Fully-connected where the output Converts a text to a sequence of indexes in a fixed- layer_cropping_2d() is to be fed back to input size hashing space MobileNet model architecture layer_cropping_3d() Cropping layer layer_gru() text_to_word_sequence() Gated recurrent unit - Cho et al Convert text to a sequence of words (or tokens) POOLING LAYERS ImageNet is a large database of images with layer_cudnn_gru() labels, extensively used for deep learning layer_max_pooling_1d() Fast GRU implementation backed IMAGE PREPROCESSING layer_max_pooling_2d() by CuDNN imagenet_preprocess_input() layer_max_pooling_3d() image_load() Loads an image into PIL format. imagenet_decode_predictions() Maximum pooling for 1D to 3D layer_lstm() Preprocesses a tensor encoding a batch of Long-Short Term Memory unit - flow_images_from_data() images for ImageNet, and decodes predictions layer_average_pooling_1d() Hochreiter 1997 flow_images_from_directory() layer_average_pooling_2d() Generates batches of augmented/normalized data layer_average_pooling_3d() layer_cudnn_lstm() from images and labels, or a directory Callbacks Average pooling for 1D to 3D Fast LSTM implementation backed by CuDNN A callback is a set of functions to be applied at layer_global_max_pooling_1d() image_data_generator() Generate minibatches of image data with real-time . given stages of the training procedure. You can layer_global_max_pooling_2d() LOCALLY CONNECTED LAYERS use callbacks to get a view on internal states layer_global_max_pooling_3d() and statistics of the model during training. Global maximum pooling fit_image_data_generator() Fit image data layer_locally_connected_1d() generator internal statistics to some sample data callback_early_stopping() Stop training when layer_global_average_pooling_1d() layer_locally_connected_2d() a monitored quantity has stopped improving layer_global_average_pooling_2d() Similar to convolution, but weights are not generator_next() Retrieve the next item layer_global_average_pooling_3d() shared, i.e. different filters for each patch callback_learning_rate_scheduler() Learning Global average pooling rate scheduler image_to_array(); image_array_resize() callback_tensorboard() TensorBoard basic image_array_save() 3D array representation visualizations RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at keras.rstudio.com • keras 2.1.2 • Updated: 2017-12