官术网_书友最值得收藏!

How to do it...

  1. Load the occupancy train and test datasets in R:
# Load the occupancy data 
occupancy_train <-read.csv("C:/occupation_detection/datatraining.txt",stringsAsFactors = T)
occupancy_test <- read.csv("C:/occupation_detection/datatest.txt",stringsAsFactors = T)
  1. The following independent (x) and dependent (y) variables will be used to model GLM:
# Define input (x) and output (y) variables
x = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
y = "Occupancy"
  1. Based on the requirement by MXNet, convert the train and test datasets to a matrix and ensure that the class of the outcome variable is numeric (instead of factor as in the case of H2O):
# convert the train data into matrix
occupancy_train.x <- data.matrix(occupancy_train[,x])
occupancy_train.y <- occupancy_train$Occupancy

# convert the test data into matrix
occupancy_test.x <- data.matrix(occupancy_test[,x])
occupancy_test.y <- occupancy_test$Occupancy
  1. Now, let's configure a neural network manually. First, configure a symbolic variable with a specific name. Then configure a symbolic fully connected network with five neurons in a single hidden layer followed with the softmax activation function with logit loss (or cross entropy loss). One can also create additional (fully connected) hidden layers with different activation functions.
# Configure Neural Network structure 
smb.data <- mx.symbol.Variable("data")
smb.fc <- mx.symbol.FullyConnected(smb.data, num_hidden=5)
smb.soft <- mx.symbol.SoftmaxOutput(smb.fc)
  1. Once the neural network is configured, let's create (or train) the (Feedforward) neural network model using the mx.model.FeedForward.create function. The model is fine-tuned for parameters such as the number of iterations or epochs (100), the metric for evaluation (classification accuracy), the size of each iteration or epoch (100 observations), and the learning rate (0.01):
# Train the network
model.nn <- mx.model.FeedForward.create(symbol = smb.soft,
X = occupancy_train.x,
y = occupancy_train.y,
ctx = mx.cpu(),
num.round = 100,
eval.metric = mx.metric.accuracy,
array.batch.size = 100,
learning.rate = 0.01)
主站蜘蛛池模板: 丹巴县| 西昌市| 合肥市| 枣阳市| 都江堰市| 府谷县| 礼泉县| 安远县| 和顺县| 开远市| 惠州市| 翁源县| 德安县| 特克斯县| 盐亭县| 湖州市| 洛浦县| 德州市| 蒲城县| 原阳县| 大庆市| 马公市| 岢岚县| 昭觉县| 翁牛特旗| 甘肃省| 兴国县| 杂多县| 衡南县| 新民市| 固始县| 陇川县| 桓仁| 宁蒗| 望奎县| 福清市| 彭泽县| 锦屏县| 扎鲁特旗| 乌拉特前旗| 大田县|