6.14 Neural Network Model
The ore.odmNN
class creates a Neural Network (NN) model for classification and regression. The Neural Network models can be used to capture intricate nonlinear relationships between inputs and outputs or to find patterns in data.
The ore.odmNN
class methods build a feed-forward neural network for regression and classification on OML4R proxy data frames. It supports multiple hidden layers, each with their own number of nodes and activation functions.
Each layer can have one of the following activation functions.
- NNET_ACTIVATIONS_ARCTAN
- NNET_ACTIVATIONS_BIPOLAR_SIG
- NNET_ACTIVATIONS_LINEAR
- NNET_ACTIVATIONS_LOG_SIG
- NNET_ACTIVATIONS_RELU
- NNET_ACTIVATIONS_TANH
The output layer is a single numeric or binary categorical target. The output layer can have any of the activation functions. It has the linear activation function by default.
Modeling with the ore.odmNN class is well-suited for noisy and complex data such as sensor data. Problems that such data might have are the following:
-
Potentially many (numeric) predictors, for example, pixel values
-
The target may be discrete-valued, real-valued, or a vector of such values
-
Training data may contain errors – robust to noise
-
Fast scoring
-
Model transparency is not required; models difficult to interpret
Typical steps in Neural Network modeling are the following:
-
Specifying the architecture
-
Preparing the data
-
Building the model
-
Specifying the stopping criteria: iterations, error on a validation set within tolerance
-
Viewing statistical results from the model
-
Improving the model
Settings for a Neural Network Model
The following table lists settings for NN models.
Table 6-13 Neural Network Model Settings
Setting Name | Setting Value | Description |
---|---|---|
NNET_HIDDEN_LAYERS |
|
Defines the topology by number of hidden layers. The default value is |
|
A list of positive integers |
Defines the topology by number of nodes per layer. Different layers can have different number of nodes. The value should be non-negative integers and comma separated. For example, '10, 20, 5'. The setting values must be consistent with |
|
A list of the following strings:
|
Defines the activation function for the hidden layers. For example, '''NNET_ACTIVATIONS_BIPOLAR_SIG '', ''NNET_ACTIVATIONS_TANH '''.
Different layers can have different activation functions. The default value is The number of activation functions must be consistent with Note: All quotes are single and two single quotes are used to escape a single quote in SQL statements. |
|
|
The setting specifies the lower bound of the region where weights are randomly initialized.
The value of |
|
|
This setting specifies the upper bound of the region where weights are initialized. It should be set in pairs with The default value is |
|
|
This setting specifies the maximum number of iterations in the Neural Network algorithm. The default value is |
|
|
Defines the convergence tolerance setting of the Neural Network algorithm. The default value is |
NNET_REGULARIZER |
|
Regularization setting for Neural Network algorithm. If the total number of training rows is greater than 50000, the default is If the total number of training rows is less than or equal to 50000, the default is |
|
|
Define the held ratio for the held-aside method. The default value is |
|
The value must be a positive integer. |
With The default value is |
NNET_REG_LAMBDA |
TO_CHAR(X >= 0) |
Defines the L2 regularization parameter lambda. This can not be set together with NNET_REGULARIZER_HELDASIDE .
The default value is |
Example 6-16 Building a Neural Network Model
This example creates an NN model and uses some of the methods of the ore.odmNN
class.
# Turn off row ordering warnings
options(ore.warn.order=FALSE)
# Data setup
set.seed(7654)
x <- seq(0.1, 5, by = 0.02)
weights <- round(rnorm(length(x),10,3))
y <- log(x) + rnorm(x, sd = 0.2)
# Create a temporary OML4R proxy object DAT.
DAT <- ore.push(data.frame(x=x, y=y, weights=weights))
# Create an NN regression model object. Fit the NN model according to the data and setting parameters.
mod.nn <- ore.odmNN(y~x, DAT,"regression",
odm.settings = list(nnet_hidden_layers = 1))
weight(mod.nn)
summary(mod.nn)
# Use the model to make predictions on the input data.
pred.nn <- predict(mod.nn, DAT, "y")
head(pred.nn, 10)
Listing for This Example
Table 6-14 A data.frame: 4 x 6
LAYER | IDX_FROM | IDX_TO | ATTRIBUTE_NAME | ATTRIBUTE_VALUE | WEIGHT |
---|---|---|---|---|---|
<dbl> | <dbl> | <dbl> | <chr> | <chr> | <dbl> |
0 | 0 | 0 | x | NA | -1.0663866 |
0 | NA | 0 | NA | NA | -7.4897304 |
1 | 0 | 0 | NA | NA | -1068.0117188 |
1 | NA | 0 | NA | NA | 0.9961451 |
Table 6-15 A data.frame: 10 x 2
y | PREDICTION |
---|---|
<dbl> | <dbl> |
-2.376195 | -1.648826 |
-1.906485 | -1.601597 |
-2.027240 | -1.555065 |
-1.541951 | -1.509221 |
-1.654645 | -1.464055 |
-1.742211 | -1.419556 |
-1.320646 | -1.375714 |
-1.357442 | -1.332520 |
-1.442755 | -1.289965 |
-1.192586 | -1.248039 |
Example 6-17 ore.odmNN
classification
This example creates an NN model and uses some of the methods of the ore.odmNN
classification class.
# Turn off row ordering warnings
options(ore.warn.order=FALSE)
# Data setup
m <- mtcars
m$gear <- as.factor(m$gear)
m$cyl <- as.factor(m$cyl)
m$vs <- as.factor(m$vs)
m$ID <- 1:nrow(m)
# Create a temporary OML4R proxy object for the MTCARS table.
MTCARS <- ore.push(m)
row.names(MTCARS) <- MTCARS$ID
# Create an NN classification model object. Fit the NN model according to the data and setting parameters.
mod.nn <- ore.odmNN(gear ~ ., MTCARS,"classification",
odm.settings = list(nnet_hidden_layers = 2,
nnet_activations = c("'NNET_ACTIVATIONS_LOG_SIG'", "'NNET_ACTIVATIONS_TANH'"),
nnet_nodes_per_layer = c(5, 2)))
head(weight(mod.nn), 10)
# Use the model to make predictions on the input data.
pred.nn <- predict(mod.nn, MTCARS, "gear")
# Generate a confusion matrix.
with(pred.nn, table(gear, PREDICTION))
Listing for This Example
Table 6-16 A data.frame: 10 x 6
LAYER | IDX_FROM | IDX_TO | ATTRIBUTE_NAME | ATTRIBUTE_VALUE | WEIGHT |
---|---|---|---|---|---|
<dbl> | <dbl> | <dbl> | <chr> | <chr> | <dbl> |
0 | 0 | 0 | ID | NA | 12.424586 |
0 | 0 | 1 | ID | NA | -9.953163 |
0 | 0 | 2 | ID | NA | -7.516252 |
0 | 0 | 3 | ID | NA | -1.100170 |
0 | 0 | 4 | ID | NA | -15.955383 |
0 | 1 | 0 | am | NA | 21.585514 |
0 | 1 | 1 | am | NA | -3.228476 |
0 | 1 | 2 | am | NA | -22.794853 |
0 | 1 | 3 | am | NA | 15.349457 |
0 | 1 | 4 | am | NA | -19.099138 |
PREDICTION
gear 3 4
3 14 1
4 0 12
5 2 3