楼主: igs816
3515 107

[其他] R Deep Learning Cookbook   [分享]

cebrau 发表于 2017-8-11 22:51:26 |显示全部楼层
感謝分享
回复

使用道具 举报

beijin2088 发表于 2017-8-12 07:39:29 |显示全部楼层
谢谢分享
回复

使用道具 举报

Reader's 发表于 2017-8-12 09:09:53 |显示全部楼层
  1. ######################## USING MXNET package (neural Network)
  2. library(mxnet)

  3. # load the data
  4. # Load the occupancy data
  5. occupancy_train <- read.csv("/occupation_detection/datatraining.txt",stringsAsFactors = T)
  6. occupancy_test <- read.csv("/occupation_detection/datatest.txt",stringsAsFactors = T)

  7. # Define input (x) and output (y) variables"
  8. x = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
  9. y = "Occupancy"

  10. # convert the train data into matrix
  11. occupancy_train.x <- data.matrix(occupancy_train[,x])
  12. occupancy_train.y <- occupancy_train$Occupancy

  13. # convert the test data into matrix
  14. occupancy_test.x <- data.matrix(occupancy_test[,x])
  15. occupancy_test.y <- occupancy_test$Occupancy

  16. # Train a multilayered perceptron

  17. #set seed to reproduce results
  18. mx.set.seed(1234567)

  19. # create NN structure
  20. smb.data <- mx.symbol.Variable("data")
  21. smb.fc <- mx.symbol.FullyConnected(smb.data, num_hidden=5)
  22. smb.soft <- mx.symbol.SoftmaxOutput(smb.fc)

  23. # Train the network
  24. nnmodel <- mx.model.FeedForward.create(symbol = smb.soft,
  25.                                        X = occupancy_train.x,
  26.                                        y = occupancy_train.y,
  27.                                        ctx = mx.cpu(),
  28.                                        num.round = 100,
  29.                                        eval.metric = mx.metric.accuracy,
  30.                                        array.batch.size = 100,
  31.                                        learning.rate = 0.01)

  32. # Train accuracy (AUC)
  33. train_pred <- predict(nnmodel,occupancy_train.x)
  34. train_yhat <- max.col(t(train_pred))-1
  35. roc_obj <- pROC::roc(c(occupancy_train.y), c(train_yhat))
  36. pROC::auc(roc_obj)

  37. # Predict on the test data
  38. test_pred <- predict(nnmodel,occupancy_test.x)
  39. test_yhat <- max.col(t(test_pred))-1

  40. #Test accuracy (AUC)
  41. roc_obj <- pROC::roc(c(occupancy_test.y), c(test_yhat))
  42. pROC::auc(roc_obj)
复制代码
回复

使用道具 举报

Reader's 发表于 2017-8-12 09:11:00 |显示全部楼层
  1. ######################## USING TENSORFLOW (neural network)
  2. library(tensorflow)

  3. np <- import("numpy")
  4. tf <- import("tensorflow")

  5. # Loading input and test data
  6. xFeatures = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
  7. yFeatures = "Occupancy"
  8. occupancy_train <-read.csv("/occupation_detection/datatraining.txt",stringsAsFactors = T)
  9. occupancy_test <- read.csv("/occupation_detection/datatest.txt",stringsAsFactors = T)

  10. # subset features for modeling and transform to numeric values
  11. occupancy_train<-apply(occupancy_train[, c(xFeatures, yFeatures)], 2, FUN=as.numeric)
  12. occupancy_test<-apply(occupancy_test[, c(xFeatures, yFeatures)], 2, FUN=as.numeric)

  13. # Data dimensions
  14. nFeatures<-length(xFeatures)
  15. nRow<-nrow(occupancy_train)

  16. # Reset the graph
  17. tf$reset_default_graph()
  18. # Starting session as interactive session
  19. sess<-tf$InteractiveSession()

  20. # Network Parameters
  21. n_hidden_1 = 5L # 1st layer number of features
  22. n_hidden_2 = 5L # 2nd layer number of features
  23. n_input = 5L    # 5 attributes
  24. n_classes = 1L  # Binary class

  25. # Model Parameters
  26. learning_rate = 0.001
  27. training_epochs = 10000

  28. # Graph input
  29. x = tf$constant(unlist(occupancy_train[,xFeatures]), shape=c(nRow, n_input), dtype=np$float32)
  30. y = tf$constant(unlist(occupancy_train[,yFeatures]), dtype="float32", shape=c(nRow, 1L))

  31. # Create model
  32. multilayer_perceptron <- function(x, weights, biases){
  33.   # Hidden layer with RELU activation
  34.   layer_1 = tf$add(tf$matmul(x, weights[["h1"]]), biases[["b1"]])
  35.   layer_1 = tf$nn$relu(layer_1)
  36.   # Hidden layer with RELU activation
  37.   layer_2 = tf$add(tf$matmul(layer_1, weights[["h2"]]), biases[["b2"]])
  38.   layer_2 = tf$nn$relu(layer_2)
  39.   # Output layer with linear activation
  40.   out_layer = tf$matmul(layer_2, weights[["out"]]) + biases[["out"]]
  41.   return(out_layer)
  42. }

  43. # Initialises and store hidden layer's weight & bias
  44. weights = list(
  45.   "h1" = tf$Variable(tf$random_normal(c(n_input, n_hidden_1))),
  46.   "h2" = tf$Variable(tf$random_normal(c(n_hidden_1, n_hidden_2))),
  47.   "out" = tf$Variable(tf$random_normal(c(n_hidden_2, n_classes)))
  48. )
  49. biases = list(
  50.   "b1" =  tf$Variable(tf$random_normal(c(1L,n_hidden_1))),
  51.   "b2" = tf$Variable(tf$random_normal(c(1L,n_hidden_2))),
  52.   "out" = tf$Variable(tf$random_normal(c(1L,n_classes)))
  53. )

  54. # Construct model
  55. pred = multilayer_perceptron(x, weights, biases)

  56. # Define loss and optimizer
  57. cost = tf$reduce_mean(tf$nn$sigmoid_cross_entropy_with_logits(logits=pred, labels=y))
  58. optimizer = tf$train$AdamOptimizer(learning_rate=learning_rate)$minimize(cost)

  59. # Initializing the global variables
  60. init = tf$global_variables_initializer()
  61. sess$run(init)

  62. # Training cycle
  63. for(epoch in 1:training_epochs){
  64.     sess$run(optimizer)
  65.   if (epoch %% 20== 0)
  66.     cat(epoch, "-", sess$run(cost), "\n")                                   
  67. }

  68. # Performance on Train
  69. library(pROC)
  70. ypred <- sess$run(tf$nn$sigmoid(multilayer_perceptron(x, weights, biases)))
  71. roc_obj <- roc(occupancy_train[, yFeatures], as.numeric(ypred))

  72. # Performance on Test
  73. nRowt<-nrow(occupancy_test)
  74. xt <- tf$constant(unlist(occupancy_test[, xFeatures]), shape=c(nRowt, nFeatures), dtype=np$float32) #
  75. ypredt <- sess$run(tf$nn$sigmoid(multilayer_perceptron(xt, weights, biases)))
  76. roc_objt <- roc(occupancy_test[, yFeatures], as.numeric(ypredt))

  77. plot.roc(roc_obj, col = "green", lty=2, lwd=2)
  78. plot.roc(roc_objt, add=T, col="red", lty=4, lwd=2)
复制代码
回复

使用道具 举报

lianqu 发表于 2017-8-15 09:15:35 |显示全部楼层
回复

使用道具 举报

zlhai 在职认证  发表于 2017-8-15 14:59:01 |显示全部楼层
thx for sharing
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-9-8 09:39:28 |显示全部楼层
  1. ###########################   LOGISTIC REGRESSION   #################################

  2. ##   H2o GLM ##
  3. ## Load packages
  4. library(h2o)
  5. library(caret)
  6. library(pROC)

  7. # Load the occupancy data
  8. occupancy_train <- read.csv("/occupation_detection/datatraining.txt",stringsAsFactors = T)
  9. occupancy_test <- read.csv("/occupation_detection/datatest.txt",stringsAsFactors = T)

  10. # Define input (x) and output (y) variables"
  11. x = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
  12. y = "Occupancy"

  13. # Convert the outcome variable into factor
  14. occupancy_train$Occupancy <- as.factor(occupancy_train$Occupancy)
  15. occupancy_test$Occupancy <- as.factor(occupancy_test$Occupancy)

  16. occupancy_train.hex <- as.h2o(x = occupancy_train, destination_frame = "occupancy_train.hex")
  17. occupancy_test.hex <- as.h2o(x = occupancy_test, destination_frame = "occupancy_test.hex")


  18. # Train the GLM model
  19. occupancy_train.glm <- h2o.glm(x = x,  # Vector of predictor variable names
  20.                              y = y,    # Name of response/dependent variable
  21.                              training_frame = occupancy_train.hex, # Training data set
  22.                              seed = 1234567,     # Seed for random numbers
  23.                              family = "binomial",  
  24.                              lambda_search = TRUE,
  25.                              alpha = 0.5,
  26.                              nfolds = 5    # N-fold cross validation
  27.                              )

  28. # Training accuracy (AUC)
  29. occupancy_train.glm@model$training_metrics@metrics$AUC

  30. # Cross validation accuracy (AUC)
  31. occupancy_train.glm@model$cross_validation_metrics@metrics$AUC

  32. # Predict on test data
  33. yhat <- h2o.predict(occupancy_train.glm, occupancy_test.hex)

  34. # Check the accuracy on test data using confusion matrix
  35. table(actual = as.matrix(occupancy_test.hex$Occupancy) , predicted = as.matrix(yhat$predict))

  36. # Test accuracy (AUC)
  37. yhat$pmax <- pmax(yhat$p0, yhat$p1, na.rm = TRUE)
  38. roc_obj <- pROC::roc(c(as.matrix(occupancy_test.hex$Occupancy)), c(as.matrix(yhat$pmax)))
  39. auc(roc_obj)

  40. #compute variable importance and performance
  41. h2o.varimp_plot(occupancy_train.glm,num_of_features = 5)
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-9-8 09:40:52 |显示全部楼层
  1. ##############################   USING TENSORFLOW  - Logistic Regression

  2. # Loading input and test data
  3. xFeatures = c("Temperature", "Humidity", "Light", "CO2", "HumidityRatio")
  4. yFeatures = "Occupancy"
  5. occupancy_train <-read.csv("C:/occupation_detection/datatraining.txt",stringsAsFactors = T)
  6. occupancy_test <- read.csv("C:/occupation_detection/datatest.txt",stringsAsFactors = T)

  7. # subset features for modeling and transform to numeric values
  8. occupancy_train<-apply(occupancy_train[, c(xFeatures, yFeatures)], 2, FUN=as.numeric)
  9. occupancy_test<-apply(occupancy_test[, c(xFeatures, yFeatures)], 2, FUN=as.numeric)

  10. # Data dimensions
  11. nFeatures<-length(xFeatures)
  12. nRow<-nrow(occupancy_train)

  13. # Reset the graph
  14. tf$reset_default_graph()
  15. # Starting session as interactive session
  16. sess<-tf$InteractiveSession()

  17. # Setting-up Logistic regression graph
  18. x <- tf$constant(unlist(occupancy_train[, xFeatures]), shape=c(nRow, nFeatures), dtype=np$float32) #
  19. W <- tf$Variable(tf$random_uniform(shape(nFeatures, 1L)))
  20. b <- tf$Variable(tf$zeros(shape(1L)))
  21. y <- tf$matmul(x, W) + b

  22. # Setting-up cost function and optimizer
  23. y_ <- tf$constant(unlist(occupancy_train[, yFeatures]), dtype="float32", shape=c(nRow, 1L))
  24. cross_entropy<-tf$reduce_mean(tf$nn$sigmoid_cross_entropy_with_logits(labels=y_, logits=y, name="cross_entropy"))
  25. optimizer <- tf$train$GradientDescentOptimizer(0.15)$minimize(cross_entropy)

  26. # Start a session
  27. init <- tf$global_variables_initializer()
  28. sess$run(init)

  29. # Running optimization
  30. for (step in 1:5000) {
  31.   sess$run(optimizer)
  32.   if (step %% 20== 0)
  33.     cat(step, "-", sess$run(W), sess$run(b), "==>", sess$run(cross_entropy), "\n")
  34. }

  35. # Performance on Train
  36. library(pROC)
  37. ypred <- sess$run(tf$nn$sigmoid(tf$matmul(x, W) + b))
  38. roc_obj <- roc(occupancy_train[, yFeatures], as.numeric(ypred))


  39. # Performance on test
  40. nRowt<-nrow(occupancy_test)
  41. xt <- tf$constant(unlist(occupancy_test[, xFeatures]), shape=c(nRowt, nFeatures), dtype=np$float32) #
  42. ypredt <- sess$run(tf$nn$sigmoid(tf$matmul(xt, W) + b))
  43. roc_objt <- roc(occupancy_test[, yFeatures], as.numeric(ypredt))

  44. plot.roc(roc_obj, col = "green", lty=2, lwd=2)
  45. plot.roc(roc_objt, add=T, col="red", lty=4, lwd=2)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 我要注册

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2019-11-15 01:44