train_dnn: Train a deep neural network

Description Usage Arguments Value Examples

View source: R/train_dnn.R

Description

This function trains a deep neural network

Usage

1
2
3
4
5
6
train_dnn(darch, input, target, input_valid = NULL, target_valid = NULL,
  ..., learn_rate_weight = exp(-10), learn_rate_bias = exp(-10),
  learn_rate_gamma = 1, batch_size = 10, batch_normalization = TRUE,
  dropout_input = 0, dropout_hidden = 0, momentum_initial = 0.6,
  momentum_final = 0.9, momentum_switch = 100, num_epochs = 0,
  error_function = meanSquareErr, report_classification_error = FALSE)

Arguments

darch

a darch instance

input

input data for training

target

target data for training

input_valid

input data for validation

target_valid

target data for validation

...

additional input

learn_rate_weight

learning rate for the weight matrices

learn_rate_bias

learning rate for the biases

learn_rate_gamma

learning rate for the gamma

batch_size

batch size during training

batch_normalization

logical value that determines whether to turn on batch normalization during training. Recommneded value: T

dropout_input

dropout ratio at input layer. Recommneded value: 0.2

dropout_hidden

dropout ratio at hidden layers. Recommended value: 0.5

momentum_initial

momentum ratio during training. Recommended value: 0.6

momentum_final

final momentum during training. Recommended value: 0.9

momentum_switch

afther which epoch the final momentum ratio is used during training

num_epochs

number of iterations of the training

error_function

error function to minimize during training

report_classification_error

logical value. T to report the classification error during training

Value

a trained deep neural network (darch instance)

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Example of Regression

input <- matrix(runif(1000), 500, 2)
input_valid <- matrix(runif(100), 50, 2)
target <- rowSums(input + input^2)
target_valid <- rowSums(input_valid + input_valid^2)
# create a new deep neural network for classificaiton
dnn_regression <- new_dnn(
 c(2, 50, 50, 20, 1),  # The layer structure of the deep neural network.
 # The first element is the number of input variables.
 # The last element is the number of output variables.
 hidden_layer_default = rectified_linear_unit_function,
 # for hidden layers, use rectified_linear_unit_function
 output_layer_default = linearUnitDerivative
 # for regression, use linearUnitDerivative function
)

 dnn_regression <- train_dnn(
 dnn_regression,

 # training data
 input, # input variable for training
 target, # target variable for training
 input_valid, # input variable for validation
 target_valid, # target variable for validation

 # training parameters
 learn_rate_weight = exp(-8) * 10,
 # learning rate for weights, higher if use dropout
 learn_rate_bias = exp(-8) * 10,
 # learning rate for biases, hihger if use dropout
 learn_rate_gamma = exp(-8) * 10,
 # learning rate for the gamma factor used
 batch_size = 10,
 # number of observations in a batch during training.
 # Higher for faster training. Lower for faster convergence
 batch_normalization = TRUE,
 # logical value, T to use batch normalization
 dropout_input = 0.2,
  # dropout ratio in input.
 dropout_hidden = 0.5,
 # dropout ratio in hidden layers
 momentum_initial = 0.6,
 # initial momentum in Stochastic Gradient Descent training
 momentum_final = 0.9,
 # final momentum in Stochastic Gradient Descent training
 momentum_switch = 100,
 # after which the momentum is switched from initial to final momentum
 num_epochs = 5,
  # number of iterations in training
  # increase numbef of epochs to 100 for better model fit


 # Error function
 error_function = meanSquareErr,
 # error function to minimize during training. For regression, use meanSquareErr
 report_classification_error = FALSE
 # whether to print classification error during training
)


# the prediciton by dnn_regression
pred <- predict(dnn_regression)

# calculate the r-squared of the prediciton
rsq(dnn_regression)


# calcualte the r-squared of the prediciton in validation
rsq(dnn_regression, input = input_valid, target = target_valid)

# print the layer weights
# this function can print heatmap, histogram, or a surface
print_weight(dnn_regression, 1, type = "heatmap")

print_weight(dnn_regression, 2, type = "surface")

print_weight(dnn_regression, 3, type = "histogram")


# Examples of classification

input <- matrix(runif(1000), 500, 2)
input_valid <- matrix(runif(100), 50, 2)
target <- (cos(rowSums(input + input^2)) > 0.5) * 1
target_valid <- (cos(rowSums(input_valid + input_valid^2)) > 0.5) * 1

# create a new deep neural network for classificaiton
dnn_classification <- new_dnn(
 c(2, 50, 50, 20, 1),  # The layer structure of the deep neural network.
 # The first element is the number of input variables.
 # The last element is the number of output variables.
 hidden_layer_default = rectified_linear_unit_function,
 # for hidden layers, use rectified_linear_unit_function
 output_layer_default = sigmoidUnitDerivative
 # for classification, use sigmoidUnitDerivative function
)

dnn_classification <- train_dnn(
 dnn_classification,

 # training data
 input, # input variable for training
 target, # target variable for training
 input_valid, # input variable for validation
 target_valid, # target variable for validation

 # training parameters
 learn_rate_weight = exp(-8) * 10,
 # learning rate for weights, higher if use dropout
 learn_rate_bias = exp(-8) * 10,
 # learning rate for biases, hihger if use dropout
 learn_rate_gamma = exp(-8) * 10,
 # learning rate for the gamma factor used
 batch_size = 10,
 # number of observations in a batch during training.
 # Higher for faster training. Lower for faster convergence
 batch_normalization = TRUE,
 # logical value, T to use batch normalization
 dropout_input = 0.2,
 # dropout ratio in input.
 dropout_hidden = 0.5,
 # dropout ratio in hidden layers
 momentum_initial = 0.6,
 # initial momentum in Stochastic Gradient Descent training
 momentum_final = 0.9,
 # final momentum in Stochastic Gradient Descent training
 momentum_switch = 100,
 # after which the momentum is switched from initial to final momentum
 num_epochs = 5,
 # number of iterations in training
 # increase num_epochs to 100 for better model fit

 # Error function
 error_function = crossEntropyErr,
 # error function to minimize during training. For regression, use crossEntropyErr
 report_classification_error = TRUE
 # whether to print classification error during training
)

# the prediciton by dnn_regression
pred <- predict(dnn_classification)

hist(pred)

# calculate the r-squared of the prediciton
AR(dnn_classification)

# calcualte the r-squared of the prediciton in validation
AR(dnn_classification, input = input_valid, target = target_valid)

rz1988/deeplearning documentation built on May 28, 2019, 10:46 a.m.