楼主: igs816
7954 107

[其他] R Deep Learning Cookbook   [推广有奖]

101
cebrau 发表于 2017-8-11 22:51:26 |只看作者 |坛友微信交流群
感謝分享

使用道具

102
beijin2088 发表于 2017-8-12 07:39:29 |只看作者 |坛友微信交流群
谢谢分享

使用道具

103
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)
复制代码

使用道具

104
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)
复制代码

使用道具

105
lianqu 发表于 2017-8-15 09:15:35 |只看作者 |坛友微信交流群

使用道具

106
zlhai 在职认证  发表于 2017-8-15 14:59:01 |只看作者 |坛友微信交流群
thx for sharing

使用道具

107
Nicolle 学生认证  发表于 2018-9-8 09:39:28 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

108
Nicolle 学生认证  发表于 2018-9-8 09:40:52 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

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

本版微信群
加好友,备注jr
拉您进交流群

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

GMT+8, 2024-4-26 17:21