- R Deep Learning Cookbook
- Dr. PKS Prakash Achyutuni Sri Krishna Rao
- 276字
- 2021-07-02 20:49:16
How to do it...
- 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)
- 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"
- 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
- 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)
- 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)
推薦閱讀
- 自己動(dòng)手寫搜索引擎
- OpenCV實(shí)例精解
- Highcharts Cookbook
- Java EE 7 Performance Tuning and Optimization
- 青少年學(xué)Python(第1冊(cè))
- Java Web開發(fā)就該這樣學(xué)
- 持續(xù)輕量級(jí)Java EE開發(fā):編寫可測(cè)試的代碼
- HTML+CSS+JavaScript網(wǎng)頁設(shè)計(jì)從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- JavaScript程序設(shè)計(jì)(第2版)
- 從零開始學(xué)Android開發(fā)
- Docker:容器與容器云(第2版)
- Building a Media Center with Raspberry Pi
- SaaS攻略:入門、實(shí)戰(zhàn)與進(jìn)階
- Python趣味創(chuàng)意編程
- 程序員的英語