Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Keras

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Deep Learning with Keras : : CHEAT SHEET Keras TensorFlow

Intro Define
INSTALLATION
The keras R package uses the Python keras library.
Compile Fit Evaluate Predict
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
• Sequential • Optimiser • Epochs • Evaluate • classes https://keras.rstudio.com/reference/install_keras.html
experimentation. It supports multiple back-
ends, including TensorFlow, CNTK and Theano. model • Loss • Validation • Plot • probability
library(keras) See ?keras_install
• Multi-GPU • Metrics split
install_keras() for GPU instructions
TensorFlow is a lower level mathematical model
library for building deep neural network This installs the required libraries in an Anaconda
architectures. The keras R package makes it https://keras.rstudio.com The “Hello, World!” environment or virtual environment 'r-tensorflow'.
easy to use Keras and TensorFlow in R. https://www.manning.com/books/deep-learning-with-r of deep learning

TRAINING AN IMAGE RECOGNIZER ON MNIST DATA


Working with keras models # input layer: use MNIST images
DEFINE A MODEL PREDICT CORE LAYERS mnist <- dataset_mnist()
keras_model() Keras Model x_train <- mnist$train$x; y_train <- mnist$train$y
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
for the input samples connected NN layer to an output x_train <- array_reshape(x_train, c(nrow(x_train), 784))
multi_gpu_model() Replicates a model on different
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 layer_dropout() Applies Dropout y_train <- to_categorical(y_train, 10)


predict_generator() Generates predictions for the
to the input y_test <- to_categorical(y_test, 10)
compile(object, optimizer, loss, metrics = NULL) input samples from a data generator
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
epochs = 10, verbose = 1, callbacks = NULL, …) input_shape = c(784)) %>%
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
by-batch by a generator name (unique) or index
# 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 files 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 = clone_model() Clone a model instance sequence by using a mask value to
NULL) Evaluate a Keras model x_train, y_train,
skip timesteps
epochs = 30, batch_size = 128,
freeze_weights(); unfreeze_weights()
evaluate_generator() Evaluates the model on a data layer_flatten() Flattens an input validation_split = 0.2
generator Freeze and unfreeze weights
)
model %>% evaluate(x_test, y_test)
model %>% predict_classes(x_test)

RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 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.
temporal convolution
layer_activation(object, activation)
Apply an activation function to an output
pad_sequences()
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()

layer_conv_3d() 3D, e.g. spatial Exponential linear unit inception_v3_preprocess_input()
text_tokenizer() Text tokenization utility Inception v3 model, with weights pre-trained
convolution over volumes
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() Inception-ResNet v2 model, with weights
Depthwise separable 2D Save a text tokenizer to an external file
Applies dropout to the input trained on ImageNet
layer_upsampling_1d() texts_to_sequences();
layer_spatial_dropout_1d() texts_to_sequences_generator() application_vgg16(); application_vgg19()
layer_upsampling_2d() layer_spatial_dropout_2d()
layer_upsampling_3d() Transforms each text in texts to sequence of integers VGG16 and VGG19 models
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() application_mobilenet()

layer_zero_padding_3d() RECURRENT LAYERS text_one_hot() One-hot encode text to word indices mobilenet_preprocess_input()

Zero-padding layer mobilenet_decode_predictions()

layer_simple_rnn() text_hashing_trick()
Fully-connected RNN where the output mobilenet_load_model_hdf5()
layer_cropping_1d() Converts a text to a sequence of indexes in a fixed-
layer_cropping_2d() is to be fed back to input MobileNet model architecture
size hashing space
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) ImageNet is a large database of images with
POOLING LAYERS
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()
layer_average_pooling_3d()
Average pooling for 1D to 3D
layer_cudnn_lstm()
Fast LSTM implementation backed
Generates batches of augmented/normalized data
from images and labels, or a directory Callbacks
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 data augmentation. given stages of the training procedure. You can
layer_global_max_pooling_2d() use callbacks to get a view on internal states
LOCALLY CONNECTED LAYERS
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 callback_learning_rate_scheduler() Learning
layer_global_average_pooling_3d() shared, i.e. different filters for each patch rate scheduler
Global average pooling 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 • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at keras.rstudio.com • keras 2.1.2 • Updated: 2017-12

You might also like