4.1.5 Building a Neural Network Model

Neural network models can be used to capture intricate nonlinear relationships between inputs and outputs or to find patterns in data. The ore.neural function builds a feed-forward neural network for regression on ore.frame data. It supports multiple hidden layers with a specifiable number of nodes. Each layer can have one of several activation functions.

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.

The output of ore.neural is an object of type ore.neural.

For information about the arguments to the ore.neural function, invoke help(ore.neural).

Modeling with the ore.neural function 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:

  1. Specifying the architecture
  2. Preparing the data
  3. Building the model
  4. Specifying the stopping criteria: iterations, error on a validation set within tolerance
  5. Viewing statistical results from model
  6. Improving the model

Example 4-5 Building a Neural Network Model

This example builds a neural network with default values, including a hidden size of 1. The example pushes a subset of the longley data set to an ore.frame object in database memory as the object trainData. The example then pushes a different subset of longley to the database as the object testData. The example builds a neural network model with trainData and then predicts results using testData.

trainData <- ore.push(longley[1:11, ])
testData <- ore.push(longley[12:16, ])
fit <- ore.neural('Employed ~ GNP + Population + Year', data = trainData)
ans <- predict(fit, newdata = testData)
ans
Listing for Example 4-5
R> trainData <- ore.push(longley[1:11, ])
R> testData <- ore.push(longley[12:16, ])
R> fit <- ore.neural('Employed ~ GNP + Population + Year', data = trainData)
R> ans <- predict(fit, newdata = testData)
R> ans
  pred_Employed
1      67.97452
2      69.50893
3      70.28098
4      70.86127
5      72.31066
Warning message:
ORE object has no unique key - using random order 

Example 4-6 Using ore.neural and Specifying Activations

This example pushes the iris data set to a temporary database table that has the proxy ore.frame object IRIS. The example builds a neural network model using the ore.neural function and specifies a different activation function for each layer.

IRIS <- ore.push(iris)
fit <- ore.neural(Petal.Length ~ Petal.Width + Sepal.Length,
                  data = IRIS,
                  hiddenSizes = c(20, 5),
                  activations = c("bSigmoid", "tanh", "linear"))
ans <- predict(fit, newdata = IRIS,
               supplemental.cols = c("Petal.Length"))
options(ore.warn.order = FALSE)
head(ans, 3)
summary(ans)
Listing for Example 4-6
R> IRIS <- ore.push(iris)
R> fit <- ore.neural(Petal.Length ~ Petal.Width + Sepal.Length,
+                   data = IRIS,
+                   hiddenSizes = c(20, 5),
+                   activations = c("bSigmoid", "tanh", "linear"))
R> 
R> ans <- predict(fit, newdata = IRIS,
+                supplemental.cols = c("Petal.Length"))
R> options(ore.warn.order = FALSE)
R> head(ans, 3)
  Petal.Length pred_Petal.Length
1          1.4          1.416466
2          1.4          1.363385
3          1.3          1.310709
R> summary(ans)
  Petal.Length   pred_Petal.Length
 Min.   :1.000   Min.   :1.080    
 1st Qu.:1.600   1st Qu.:1.568    
 Median :4.350   Median :4.346    
 Mean   :3.758   Mean   :3.742    
 3rd Qu.:5.100   3rd Qu.:5.224    
 Max.   :6.900   Max.   :6.300