We use a 1-dimensional convolutional function to apply the CNN model. We need Keras R interface to use the Keras neural network API in R. You need to install it if it is not available on your development environment. The tutorial covers:

- Preparing the data
- Defining and fitting the model
- Predicting and visualizing the results
- Source code listing

library(keras) library(caret)

**Preparing the data**

We use the Boston housing dataset as a target regression data in this tutorial. First, we'll load the dataset and split it into the train and test parts.

set.seed(123) boston = MASS::Boston indexes = createDataPartition(boston$medv, p = .85, list = F)

train = boston[indexes,] test = boston[-indexes,]

Next, we'll separate x input and y output parts of both train and test data and convert them into the matrix type. As you may know, the 'medv' is the output, y data in the Boston housing dataset and it is the final column (14) in it. The remaining columns are the x input data.

xtrain = as.matrix(train[,-14]) ytrain = as.matrix(train[,14]) xtest = as.matrix(test[,-14]) ytest = as.matrix(test[, 14])

We'll check the dimensions.

dim(xtrain) [1] 432 13

dim(ytrain) [1] 432 1

Next, we'll reshape the x input data by adding another one-dimension.

xtrain = array(xtrain, dim = c(nrow(xtrain), 13, 1)) xtest = array(xtest, dim = c(nrow(xtest), 13, 1))

dim(xtrain) [1] 432 13 1

dim(xtest) [1] 74 13 1

Here, we can extract the input dimension for the keras model.

in_dim = c(dim(xtrain)[2:3]) print(in_dim) [1] 13 1

**Defining and fitting the model**

We'll define the Keras sequential model and add a one-dimensional convolutional layer. Input shape becomes as it is defined above (13,1). We'll add Flatten and Dense layers and compile it with 'Adam' optimizer.

model = keras_model_sequential() %>% layer_conv_1d(filters = 64, kernel_size = 2, input_shape = in_dim, activation = "relu") %>% layer_flatten() %>% layer_dense(units = 32, activation = "relu") %>% layer_dense(units = 1, activation = "linear") model %>% compile( loss = "mse", optimizer = "adam") model %>% summary() ________________________________________________________________________ Layer (type) Output Shape Param # ======================================================================== conv1d_2 (Conv1D) (None, 12, 64) 192 ________________________________________________________________________ flatten_2 (Flatten) (None, 768) 0 ________________________________________________________________________ dense_3 (Dense) (None, 32) 24608 ________________________________________________________________________ dense_4 (Dense) (None, 1) 33 ======================================================================== Total params: 24,833 Trainable params: 24,833 Non-trainable params: 0 ________________________________________________________________________ |

Next, we 'll fit the model with train data.

model %>% fit(xtrain, ytrain, epochs = 100, batch_size=16, verbose = 0) scores = model %>% evaluate(xtrain, ytrain, verbose = 0) print(scores) loss 24.20518

**Predicting and visualizing the results**

Now we can predict the test data with the trained model.

`ypred = model %>% predict(xtest)`

We'll check the accuracy of prediction through the RMSE metrics.

cat("RMSE:", RMSE(ytest, ypred)) RMSE: 4.935908

Finally, we'll visualize the result in a plot to check the difference.

x_axes = seq(1:length(ypred)) plot(x_axes, ytest, ylim = c(min(ypred), max(ytest)), col = "burlywood", type = "l", lwd = 2, ylab = "medv") lines(x_axes, ypred, col = "red", type = "l", lwd = 2) legend("topleft", legend = c("y-test", "y-pred"), col = c("burlywood", "red"), lty=1, cex=0.7, lwd=2, bty='n')

In this tutorial, we've briefly learned how to fit and predict regression data with the keras CNN model in R. The full source code is listed below.

**Source code listing**

library(keras) library(caret) set.seed(123)

boston = MASS::Boston indexes = createDataPartition(boston$medv, p = .85, list = F) train = boston[indexes,] test = boston[-indexes,] xtrain = as.matrix(train[,-14]) ytrain = as.matrix(train[,14]) xtest = as.matrix(test[,-14]) ytest = as.matrix(test[, 14]) dim(xtrain) dim(ytrain)

xtrain = array(xtrain, dim = c(nrow(xtrain), 13, 1)) xtest = array(xtest, dim = c(nrow(xtest), 13, 1))

dim(xtrain) dim(xtest) in_dim = c(dim(xtrain)[2:3]) print(in_dim) model = keras_model_sequential() %>% layer_conv_1d(filters = 64, kernel_size = 2, input_shape = in_dim, activation = "relu") %>% layer_flatten() %>% layer_dense(units = 32, activation = "relu") %>% layer_dense(units = 1, activation = "linear") model %>% compile( loss = "mse", optimizer = "adam") model %>% summary()

` `

```
model %>% fit(xtrain, ytrain, epochs = 100, batch_size=16, verbose = 0)
scores = model %>% evaluate(xtrain, ytrain, verbose = 0)
print(scores)
ypred = model %>% predict(xtest)
cat("RMSE:", RMSE(ytest, ypred))
```

` `

```
x_axes = seq(1:length(ypred))
plot(x_axes, ytest, ylim = c(min(ypred), max(ytest)),
col = "burlywood", type = "l", lwd = 2, ylab = "medv")
lines(x_axes, ypred, col = "red", type = "l", lwd = 2)
legend("topleft", legend = c("y-test", "y-pred"),
col = c("burlywood", "red"), lty = 1, cex=0.7, lwd=2, bty='n')
```

## No comments:

## Post a Comment