.Rhistory 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  2. tanh_3 <- mx.symbol.Activation(data = fc_1, act_type = "tanh")
  3. fc_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 40)
  4. # define whole model
  5. graph_model <- mx.symbol.SoftmaxOutput(data = fc_2)
  6. # plot our model
  7. #graph.viz(symbol = symbol)
  8. logger <- mx.metric.logger()
  9. epoch.end.callback <- mx.callback.log.train.metric(
  10. period = 1, # число батчей, после которого оценивается метрика
  11. logger = logger)
  12. # learning model
  13. model<- mx.model.FeedForward.create(graph_model, # our graph model
  14. X = train_array, # data
  15. y = train_y, # labels
  16. ctx = mx.cpu(), # engine
  17. num.round = 10, # num epoch
  18. array.batch.size = 64, # batch size
  19. learning.rate = 0.01,
  20. momentum = 0.9,
  21. eval.metric = mx.metric.accuracy, # our metrics
  22. epoch.end.callback = mx.callback.log.train.metric(1, logger)) # write learn stat
  23. graph.viz(model$symbol)
  24. # predict
  25. predicted <- predict(model, test_array)
  26. # predict
  27. predicted <- predict(model, test)
  28. # predict
  29. predicted <- predict(model, test_array)
  30. olivetti <- olivetti_faces()
  31. show_olivetti_face(olivetti, 2, 10)
  32. sample <- sample.int(n = nrow(olivetti), size = floor(.75*nrow(olivetti)), replace = F)
  33. train <- olivetti[sample, ]
  34. test <- olivetti[-sample, ]
  35. #transform and split train on x and y
  36. train <- data.matrix(train)
  37. train_x <- t(train[, -1])
  38. train_y <- train[, 1]
  39. train_array <- train_x
  40. dim(train_array) <- c(64, 64, 1, ncol(train_x))
  41. #transform and split test on x and y
  42. test <- data.matrix(test)
  43. test_x <- t(test[, -1])
  44. test_y <- test[, 1]
  45. test_array <- test_x
  46. dim(test_array) <- c(64, 64, 1, ncol(test_x))
  47. # define model
  48. data <- mx.symbol.Variable('data')
  49. # define conv layers
  50. conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20) # convolution
  51. tanh_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh") # activation function
  52. pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2)) # pooling
  53. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5, 5), num_filter = 50)
  54. tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
  55. pool_2 <- mx.symbol.Pooling(data=tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  56. # define fully connected layers
  57. flatten <- mx.symbol.Flatten(data = pool_2)
  58. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  59. tanh_3 <- mx.symbol.Activation(data = fc_1, act_type = "tanh")
  60. fc_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 40)
  61. # define whole model
  62. graph_model <- mx.symbol.SoftmaxOutput(data = fc_2)
  63. # plot our model
  64. #graph.viz(symbol = symbol)
  65. logger <- mx.metric.logger()
  66. epoch.end.callback <- mx.callback.log.train.metric(
  67. period = 1, # число батчей, после которого оценивается метрика
  68. logger = logger)
  69. # learning model
  70. model<- mx.model.FeedForward.create(graph_model, # our graph model
  71. X = train_array, # data
  72. y = train_y, # labels
  73. ctx = mx.cpu(), # engine
  74. num.round = 10, # num epoch
  75. array.batch.size = 64, # batch size
  76. learning.rate = 0.01,
  77. momentum = 0.9,
  78. eval.metric = mx.metric.accuracy, # our metrics
  79. epoch.end.callback = mx.callback.log.train.metric(1, logger)) # write learn stat
  80. # predict
  81. predicted <- predict(model, test_array)
  82. predicted
  83. predicted_labels <- max.col(t(predicted)) - 1
  84. # calculate accuracy
  85. sum(diag(table(test[, 1], predicted_labels)))/40
  86. # learning model
  87. model<- mx.model.FeedForward.create(graph_model, # our graph model
  88. X = train_array, # data
  89. y = train_y, # labels
  90. ctx = mx.cpu(), # engine
  91. num.round = 10, # num epoch
  92. array.batch.size = 64, # batch size
  93. learning.rate = 0.1,
  94. momentum = 0.9,
  95. eval.metric = mx.metric.accuracy, # our metrics
  96. epoch.end.callback = mx.callback.log.train.metric(1, logger)) # write learn stat
  97. predicted_labels <- max.col(t(predicted)) - 1
  98. # calculate accuracy
  99. sum(diag(table(test[, 1], predicted_lab
  100. els)))/40
  101. # calculate accuracy
  102. sum(diag(table(test[, 1], predicted_labels)))/40
  103. data <- mx.symbol.Variable('data')
  104. # define conv layers
  105. conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20) # convolution
  106. relu_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh") # activation function
  107. pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2)) # pooling
  108. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5, 5), num_filter = 50)
  109. relu_2 <- mx.symbol.Activation(data = conv_2, act_type = "relu")
  110. pool_2 <- mx.symbol.Pooling(data=tanh_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  111. # define fully connected layers
  112. flatten <- mx.symbol.Flatten(data = pool_2)
  113. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  114. relu_3 <- mx.symbol.Activation(data = fc_1, act_type = "relu")
  115. fc_2 <- mx.symbol.FullyConnected(data = relu_3, num_hidden = 40)
  116. # define whole model
  117. graph_model <- mx.symbol.SoftmaxOutput(data = fc_2)
  118. # plot our model
  119. #graph.viz(symbol = symbol)
  120. logger <- mx.metric.logger()
  121. epoch.end.callback <- mx.callback.log.train.metric(
  122. period = 1, # число батчей, после которого оценивается метрика
  123. logger = logger)
  124. # learning model
  125. model<- mx.model.FeedForward.create(graph_model, # our graph model
  126. X = train_array, # data
  127. y = train_y, # labels
  128. ctx = mx.cpu(), # engine
  129. num.round = 10, # num epoch
  130. array.batch.size = 64, # batch size
  131. learning.rate = 0.1,
  132. momentum = 0.9,
  133. eval.metric = mx.metric.accuracy, # our metrics
  134. epoch.end.callback = mx.callback.log.train.metric(1, logger)) # write learn stat
  135. # predict
  136. predicted <- predict(model, test_array)
  137. predicted_labels <- max.col(t(predicted)) - 1
  138. # calculate accuracy
  139. sum(diag(table(test[, 1], predicted_labels)))/40
  140. set.seed(100)
  141. # load data and split
  142. olivetti <- olivetti_faces()
  143. show_olivetti_face(olivetti, 2, 10)
  144. sample <- sample.int(n = nrow(olivetti), size = floor(.75*nrow(olivetti)), replace = F)
  145. train <- olivetti[sample, ]
  146. test <- olivetti[-sample, ]
  147. #transform and split train on x and y
  148. train <- data.matrix(train)
  149. train_x <- t(train[, -1])
  150. train_y <- train[, 1]
  151. train_array <- train_x
  152. dim(train_array) <- c(64, 64, 1, ncol(train_x))
  153. #transform and split test on x and y
  154. test <- data.matrix(test)
  155. test_x <- t(test[, -1])
  156. test_y <- test[, 1]
  157. test_array <- test_x
  158. dim(test_array) <- c(64, 64, 1, ncol(test_x))
  159. # define model
  160. data <- mx.symbol.Variable('data')
  161. # define conv layers
  162. conv_1 <- mx.symbol.Convolution(data = data, kernel = c(3, 3), num_filter = 20) # convolution
  163. relu_1 <- mx.symbol.Activation(data = conv_1, act_type = "tanh") # activation function
  164. pool_1 <- mx.symbol.Pooling(data = relu_1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2)) # pooling
  165. conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(3, 3), num_filter = 50)
  166. relu_2 <- mx.symbol.Activation(data = conv_2, act_type = "relu")
  167. pool_2 <- mx.symbol.Pooling(data=relu_2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
  168. # define fully connected layers
  169. flatten <- mx.symbol.Flatten(data = pool_2)
  170. fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
  171. relu_3 <- mx.symbol.Activation(data = fc_1, act_type = "relu")
  172. fc_2 <- mx.symbol.FullyConnected(data = relu_3, num_hidden = 40)
  173. # define whole model
  174. graph_model <- mx.symbol.SoftmaxOutput(data = fc_2)
  175. # plot our model
  176. #graph.viz(symbol = symbol)
  177. logger <- mx.metric.logger()
  178. epoch.end.callback <- mx.callback.log.train.metric(
  179. period = 1, # число батчей, после которого оценивается метрика
  180. logger = logger)
  181. # learning model
  182. model<- mx.model.FeedForward.create(graph_model, # our graph model
  183. X = train_array, # data
  184. y = train_y, # labels
  185. ctx = mx.cpu(), # engine
  186. num.round = 10, # num epoch
  187. array.batch.size = 64, # batch size
  188. learning.rate = 0.1,
  189. momentum = 0.9,
  190. eval.metric = mx.metric.accuracy, # our metrics
  191. epoch.end.callback = mx.callback.log.train.metric(1, logger)) # write learn stat
  192. # predict
  193. predicted <- predict(model, test_array)
  194. predicted_labels <- max.col(t(predicted)) - 1
  195. # calculate accuracy
  196. sum(diag(table(test[, 1], predicted_labels)))/40
  197. predicted <- predict(model, t(test_array))
  198. predicted_labels <- max.col(t(predicted)) - 1
  199. # calculate accuracy
  200. sum(diag(table(test[, 1], predicted_labels)))/40
  201. predicted <- predict(model, test_array)
  202. predicted_labels <- max.col(t(predicted)) - 1
  203. # calculate accuracy
  204. sum(diag(table(test[, 1], predicted_labels)))/40
  205. predicted
  206. dump("df", stdout())
  207. # plot our model
  208. graph.viz(linreg)
  209. # load data and split
  210. df <-
  211. structure(list(name = c("acebutolol", "acebutolol_ester", "acetylsalic_acid",
  212. "acyclovir", "alprenolol", "alprenolol ester", "aminopyrin",
  213. "artemisinin", "artesunate", "atenolol", "betazolol ester", "betazolol_",
  214. "bremazocine", "caffeine", "chloramphenicol", "chlorothiazide",
  215. "chlorpromazine", "cimetidine", "clonidine", "corticosterone",
  216. "desiprarnine", "dexamethas", "dexamethas_beta_D_glucoside",
  217. "dexamethas_beta_D_glucuronide", "diazepam", "dopamine", "doxorubici",
  218. "erythromycin", "estradiol", "felodipine", "ganciclovir", "griseofulvin",
  219. "hydrochlorothiazide", "hydrocortisone", "ibuprophen", "imipramine",
  220. "indomethacin", "labetalol", "mannitol", "meloxicam", "methanol",
  221. "methotrexate", "methylscopolamine", "metoprolol", "nadolol",
  222. "naproxen", "nevirapine", "nicotine", "olsalazine", "oxprenolol",
  223. "oxprenolol ester", "phencyclidine", "Phenytoin", "pindolol",
  224. "pirenzepine", "piroxicam", "pnu200603", "practolol", "prazocin",
  225. "progesterone", "propranolol", "propranolo_ester", "quinidine",
  226. "ranitidine", "salicylic acid", "scopolamine", "sucrose", "sulfasalazine",
  227. "telmisartan", "terbutaline", "tesosterone", "timolol", "timolol_ester",
  228. "uracil", "urea", "warfarine", "zidovudine"), log_P_eff_exp = c(-5.83,
  229. -4.61, -5.06, -6.15, -4.62, -4.47, -4.44, -4.52, -5.4, -6.44,
  230. -4.81, -4.52, -5.1, -4.41, -4.69, -6.72, -4.7, -5.89, -4.59,
  231. -4.47, -4.67, -4.75, -6.54, -6.12, -4.32, -5.03, -6.8, -5.43,
  232. -4.77, -4.64, -6.27, -4.44, -6.06, -4.66, -4.28, -4.85, -4.69,
  233. -5.03, -6.21, -4.71, -4.58, -5.92, -6.16, -4.59, -5.41, -4.83,
  234. -4.52, -4.71, -6.96, -4.68, -4.51, -4.61, -4.57, -4.78, -6.36,
  235. -4.45, -6.25, -6.05, -4.36, -4.37, -4.58, -4.48, -4.69, -6.31,
  236. -4.79, -4.93, -5.77, -6.33, -4.82, -6.38, -4.34, -4.85, -4.6,
  237. -5.37, -5.34, -4.68, -5.16), log_D = c(-0.09, 1.59, -2.25, -1.8,
  238. 1.38, 2.78, 0.63, 2.22, -0.88, -1.81, 0.28, 0.63, 1.66, 0.02,
  239. 1.14, -1.15, 1.86, -0.36, 0.78, 1.78, 1.57, 1.89, 0.58, -1.59,
  240. 2.58, -0.8, -0.16, 1.26, 2.24, 3.48, -0.87, 2.47, -0.12, 1.48,
  241. 0.68, 2.52, 1, 1.24, -2.65, 0.03, -0.7, -2.53, -1.14, 0.51, 0.68,
  242. 0.42, 1.81, 0.41, -4.5, 0.45, 1.98, 1.31, 2.26, 0.19, -0.46,
  243. -0.07, -4, -1.4, 1.88, 3.48, 1.55, 3.02, 2.04, -0.12, -1.44,
  244. 0.21, -3.34, -0.42, 2.41, -1.07, 3.11, 0.03, 1.74, -1.11, -1.64,
  245. 0.64, -0.58), rgyr = c(4.64, 5.12, 3.41, 3.37, 3.68, 3.84, 2.97,
  246. 2.75, 4.02, 4.58, 5.41, 5.64, 3.43, 2.47, 3.75, 3.11, 3.74, 4.26,
  247. 2.79, 3.68, 3.4, 3.6, 5.67, 5.75, 3.28, 2.67, 4.85, 4.99, 3.44,
  248. 3.39, 3.7, 3.37, 3.11, 3.72, 3.45, 3.44, 4.16, 4.61, 2.48, 3.34,
  249. 0.84, 5.33, 3.67, 4.59, 4.37, 3.38, 2.94, 2.5, 4.62, 3.63, 3.87,
  250. 2.91, 2.97, 3.71, 3.55, 3.17, 3.89, 4.02, 4.96, 3.58, 3.63, 4.13,
  251. 3.25, 5.13, 2.14, 3.63, 3.49, 5.68, 5.29, 3.15, 3.33, 4.02, 3.98,
  252. 1.84, 1.23, 3.45, 3.14), rgyr_d = c(4.51, 5.03, 3.24, 3.23, 3.69,
  253. 3.88, 2.97, 2.75, 3.62, 4.52, 5.27, 5.39, 3.38, 2.47, 3.73, 3.11,
  254. 3.69, 4.24, 2.79, 3.71, 3.42, 3.66, 5.28, 5.23, 3.28, 2.68, 4.9,
  255. 5.01, 3.44, 3.48, 3.48, 3.37, 3.11, 3.79, 3.36, 3.45, 3.16, 4.46,
  256. 2.59, 3.36, 0.84, 5.18, 3.74, 4.53, 4.1, 3.43, 2.94, 2.5, 4.37,
  257. 3.56, 3.9, 2.91, 2.97, 3.71, 3.4, 3.26, 3.79, 4.09, 4.99, 3.62,
  258. 3.53, 4.06, 3.3, 4.57, 2.14, 3.49, 3.54, 5.53, 5.01, 3.15, 3.33,
  259. 4.01, 4.13, 1.84, 1.23, 3.5, 3.13), HCPSA = c(82.88, 77.08, 79.38,
  260. 120.63, 38.92, 35.53, 20.81, 54.27, 102.05, 86.82, 43.02, 47.14,
  261. 49.56, 45.55, 113.73, 138.76, 4.6, 105.44, 30.03, 75.95, 13.8,
  262. 90.74, 163.95, 186.88, 25.93, 75.13, 186.78, 138.69, 44.34, 50.34,
  263. 139.45, 67.55, 142.85, 93.37, 39.86, 3.56, 67.13, 93.29, 127.46,
  264. 93.21, 25.64, 204.96, 51.29, 44.88, 86.73, 76.98, 36.68, 15.1,
  265. 144.08, 48.62, 49.58, 1.49, 65.63, 52.8, 59.71, 99.19, 69.89,
  266. 64.79, 86.76, 38.1, 40.42, 36.21, 43.77, 105.15, 61.71, 57.35,
  267. 187.69, 133.67, 55.48, 79.52, 42.35, 100.74, 96.25, 66.72, 82.72,
  268. 59.47, 96.33), TPSA = c(87.66, 93.73, 89.9, 114.76, 41.49, 47.56,
  269. 26.79, 53.99, 100.52, 84.58, 50.72, 56.79, 43.7, 58.44, 115.38,
  270. 118.69, 6.48, 88.89, 36.42, 74.6, 15.27, 94.83, 173.98, 191.05,
  271. 32.67, 66.48, 206.07, 193.91, 40.46, 64.63, 134.99, 71.06, 118.36,
  272. 94.83, 37.3, 6.48, 68.53, 95.58, 121.38, 99.6, 20.23, 210.54,
  273. 59.06, 50.72, 81.95, 46.53, 58.12, 16.13, 139.78, 50.72, 56.79,
  274. 3.24, 58.2, 57.28, 68.78, 99.6, 91.44, 70.59, 106.95, 34.14,
  275. 41.49, 47.56, 45.59, 86.26, 57.53, 62.3, 189.53, 141.31, 72.94,
  276. 72.72, 37.3, 79.74, 85.81, 58.2, 69.11, 63.6, 103.59), N_rotb = c(0.31,
  277. 0.29, 0.23, 0.21, 0.29, 0.27, 0.17, 0.07, 0.16, 0.29, 0.27, 0.26,
  278. 0.15, 0.12, 0.28, 0.08, 0.14, 0.33, 0.08, 0.1, 0.11, 0.13, 0.17,
  279. 0.17, 0.06, 0.23, 0.18, 0.21, 0.06, 0.22, 0.25, 0.16, 0.08, 0.12,
  280. 0.24, 0.13, 0.19, 0.24, 0.44, 0.16, 0.2, 0.26, 0.16, 0.3, 0.24,
  281. 0.19, 0.05, 0.07, 0.27, 0.31, 0.29, 0.04, 0.06, 0.23, 0.08, 0.13,
  282. 0.15, 0.29, 0.15, 0.07, 0.22, 0.22, 0.14, 0.33, 0.19, 0.15, 0.28,
  283. 0.2, 0.15, 0.29, 0.06, 0.24, 0.23, 0, 0.29, 0.15, 0.18), log_P_eff_calc = c(-5.3,
  284. -4.89, -5.77, -5.91, -4.58, -4.39, -4.63, -4.47, -5.64, -5.85,
  285. -5.2, -5.13, -4.57, -4.89, -5.11, -5.87, -4.38, -5.55, -4.69,
  286. -4.78, -4.46, -4.77, -5.83, -6.55, -4.45, -5.27, -6, -5.13, -4.57,
  287. -4.44, -5.79, -4.59, -5.62, -4.94, -4.78, -4.28, -5, -5.09, -5.87,
  288. -5.27, -4.67, -6.79, -5.37, -4.99, -5.15, -5.09, -4.49, -4.65,
  289. -6.97, -4.84, -4.45, -4.42, -4.6, -5.02, -5.3, -5.31, -6.37,
  290. -5.5, -5.05, -4.54, -4.57, -4.5, -4.46, -5.6, -5.29, -5.07, -6.56,
  291. -6.06, -4.85, -5.36, -4.53, -5.35, -4.82, -5.23, -5.29, -4.95,
  292. -5.43), residuals = c(-0.53, 0.28, 0.71, -0.24, -0.04, -0.08,
  293. 0.19, -0.05, 0.24, -0.59, 0.39, 0.61, -0.53, 0.48, 0.42, -0.85,
  294. -0.32, -0.34, 0.1, 0.31, -0.21, 0.02, -0.71, 0.43, 0.13, 0.24,
  295. -0.8, -0.3, -0.2, -0.2, -0.48, 0.15, -0.44, 0.28, 0.5, -0.57,
  296. 0.31, 0.06, -0.34, 0.56, 0.09, 0.87, -0.79, 0.4, -0.26, 0.26,
  297. -0.03, -0.06, 0.01, 0.16, -0.06, -0.19, 0.03, 0.24, -1.06, 0.86,
  298. 0.12, -0.55, 0.69, 0.17, -0.01, 0.02, -0.23, -0.71, 0.5, 0.14,
  299. 0.79, -0.27, 0.03, -1.02, 0.19, 0.5, 0.22, -0.14, -0.05, 0.27,
  300. 0.27)), row.names = c(NA, -77L), class = c("tbl_df", "tbl", "data.frame"
  301. ))
  302. set.seed(42)
  303. #transform and split train on x and y
  304. train_ind <- sample(1:77, 60)
  305. x_train <- as.matrix(df[train_ind, 2:8])
  306. y_train <- unlist(df[train_ind, 9])
  307. x_val <- as.matrix(df[-train_ind, 2:8])
  308. y_val <- unlist(df[-train_ind, 9])
  309. # define model
  310. data <- mx.symbol.Variable("data")
  311. fc1 <- mx.symbol.FullyConnected(data,
  312. num_hidden = 1)
  313. linreg <- mx.symbol.LinearRegressionOutput(fc1)
  314. initializer <- mx.init.normal(sd = 0.1)
  315. optimizer <- mx.opt.create("sgd",
  316. learning.rate = 1e-6,
  317. momentum = 0.9)
  318. logger <- mx.metric.logger()
  319. epoch.end.callback <- mx.callback.log.train.metric(
  320. period = 4, # число батчей, после которого оценивается метрика
  321. logger = logger)
  322. n_epoch <- 20
  323. # plot our model
  324. graph.viz(linreg)
  325. # learning model
  326. model <- mx.model.FeedForward.create(
  327. symbol = linreg,
  328. X = x_train,
  329. y = y_train,
  330. ctx = mx.cpu(),
  331. num.round = n_epoch,
  332. initializer = initializer,
  333. optimizer = optimizer,
  334. eval.data = list(data = x_val, label = y_val),
  335. eval.metric = mx.metric.rmse,
  336. array.batch.size = 15,
  337. epoch.end.callback = epoch.end.callback)
  338. logger$train
  339. logger$eval
  340. rmse_log <- data.frame(RMSE = c(logger$train, logger$eval),
  341. dataset = c(rep("train",
  342. length(logger$train)),
  343. rep("val",
  344. length(logger$eval))),
  345. epoch = 1:n_epoch)
  346. library(ggplot2)
  347. ggplot(rmse_log, aes(epoch, RMSE,
  348. group = dataset,
  349. colour = dataset)) +
  350. geom_point() +
  351. geom_line()
  352. xaringan:::inf_mr()
  353. install_keras()
  354. install.packages("keras")
  355. keras::install_keras(tensorflow = 'gpu')
  356. require(keras)
  357. model <- keras_model_sequential()
  358. model <- keras_model()
  359. keras::install_keras(tensorflow = 'gpu')
  360. detach(keras)
  361. keras::install_keras(tensorflow = 'gpu')
  362. model <- keras_model_sequential()
  363. require(keras)
  364. model <- keras_model_sequential()
  365. require(keras)
  366. model <- keras_model_sequential()
  367. require(dplyr)
  368. #install.packages("keras")
  369. #keras::install_keras(tensorflow = 'gpu')
  370. require(keras)
  371. require(dplyr)
  372. mnist <- dataset_mnist()
  373. x_train <- mnist$train$x
  374. y_train <- mnist$train$y
  375. x_test <- mnist$test$x
  376. y_test <- mnist$test$y
  377. x_train <- array_reshape(x_train, c(nrow(x_train), 784))
  378. x_test <- array_reshape(x_test, c(nrow(x_test), 784))
  379. # rescale
  380. x_train <- x_train / 255
  381. x_test <- x_test / 255
  382. y_train <- to_categorical(y_train, 10)
  383. y_test <- to_categorical(y_test, 10)
  384. model <- keras_model_sequential()
  385. model %>%
  386. layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>%
  387. layer_dropout(rate = 0.4) %>%
  388. layer_dense(units = 128, activation = 'relu') %>%
  389. layer_dropout(rate = 0.3) %>%
  390. layer_dense(units = 10, activation = 'softmax')
  391. summary(model)
  392. model %>% compile(
  393. loss = 'categorical_crossentropy',
  394. optimizer = optimizer_rmsprop(),
  395. metrics = c('accuracy')
  396. )
  397. history <- model %>% fit(
  398. x_train, y_train,
  399. epochs = 30, batch_size = 128,
  400. validation_split = 0.2
  401. )
  402. plot(history)
  403. model %>% evaluate(x_test, y_test)
  404. model %>% predict_classes(x_test)
  405. require(keras) # Neural Networks
  406. require(tidyverse) # Data cleaning / Visualization
  407. require(knitr) # Table printing
  408. require(rmarkdown) # Misc. output utilities
  409. require(ggridges) # Visualization
  410. install.packages("ggridges")
  411. require(keras) # Neural Networks
  412. require(tidyverse) # Data cleaning / Visualization
  413. require(knitr) # Table printing
  414. require(rmarkdown) # Misc. output utilities
  415. require(ggridges) # Visualization
  416. install.packages("tidyverse")
  417. require(keras) # Neural Networks
  418. require(tidyverse) # Data cleaning / Visualization
  419. require(knitr) # Table printing
  420. require(rmarkdown) # Misc. output utilities
  421. require(ggridges) # Visualization
  422. activityLabels <- read.table("Deep_Learning_in_R_files/HAPT Data Set/activity_labels.txt",
  423. col.names = c("number", "label"))
  424. activityLabels %>% kable(align = c("c", "l"))
  425. activityLabels <- read.table("Deep_Learning_in_R_files/HAPT Data Set/activity_labels.txt",
  426. col.names = c("number", "label"))
  427. activityLabels %>% kable(align = c("c", "l"))
  428. labels <- read.table("data/RawData/labels.txt",
  429. col.names = c("experiment", "userId", "activity", "startPos", "endPos"))
  430. labels %>%
  431. head(50) %>%
  432. paged_table()
  433. labels <- read.table("Deep_Learning_in_R_files/HAPT Data Set/RawData/labels.txt",
  434. col.names = c("experiment", "userId", "activity", "startPos", "endPos"))
  435. labels %>%
  436. head(50) %>%
  437. paged_table()
  438. dataFiles <- list.files("data/RawData")
  439. dataFiles %>% head()
  440. dataFiles <- list.files("Deep_Learning_in_R_files/HAPT Data Set/RawData")
  441. dataFiles %>% head()
  442. ## 3
  443. fileInfo <- data_frame(
  444. filePath = dataFiles
  445. ) %>%
  446. filter(filePath != "labels.txt") %>%
  447. separate(filePath, sep = '_',
  448. into = c("type", "experiment", "userId"),
  449. remove = FALSE) %>%
  450. mutate(
  451. experiment = str_remove(experiment, "exp"),
  452. userId = str_remove_all(userId, "user|\\.txt")
  453. ) %>%
  454. spread(type, filePath)
  455. fileInfo %>% head() %>% kable()
  456. ## 4
  457. # Read contents of single file to a dataframe with accelerometer and gyro data.
  458. readInData <- function(experiment, userId){
  459. genFilePath = function(type) {
  460. paste0("Deep_Learning_in_R_files/HAPT Data Set/RawData/", type, "_exp",
  461. experiment, "_user", userId, ".txt")
  462. }
  463. bind_cols(
  464. read.table(genFilePath("acc"), col.names = c("a_x", "a_y", "a_z")),
  465. read.table(genFilePath("gyro"), col.names = c("g_x", "g_y", "g_z"))
  466. )
  467. }
  468. # Function to read a given file and get the observations contained along
  469. # with their classes.
  470. loadFileData <- function(curExperiment, curUserId) {
  471. # load sensor data from file into dataframe
  472. allData <- readInData(curExperiment, curUserId)
  473. extractObservation <- function(startPos, endPos){
  474. allData[startPos:endPos,]
  475. }
  476. # get observation locations in this file from labels dataframe
  477. dataLabels <- labels %>%
  478. filter(userId == as.integer(curUserId),
  479. experiment == as.integer(curExperiment))
  480. # extract observations as dataframes and save as a column in dataframe.
  481. dataLabels %>%
  482. mutate(
  483. data = map2(startPos, endPos, extractObservation)
  484. ) %>%
  485. select(-startPos, -endPos)
  486. }
  487. # scan through all experiment and userId combos and gather data into a dataframe.
  488. allObservations <- map2_df(fileInfo$experiment, fileInfo$userId, loadFileData) %>%
  489. right_join(activityLabels, by = c("activity" = "number")) %>%
  490. rename(activityName = label)
  491. # cache work.
  492. write_rds(allObservations, "allObservations.rds")
  493. allObservations %>% dim()
  494. # exploring the data
  495. allObservations %>%
  496. mutate(recording_length = map_int(data,nrow)) %>%
  497. ggplot(aes(x = recording_length, y = activityName)) +
  498. geom_density_ridges(alpha = 0.8)
  499. # filtering
  500. desiredActivities <- c(
  501. "STAND_TO_SIT", "SIT_TO_STAND", "SIT_TO_LIE",
  502. "LIE_TO_SIT", "STAND_TO_LIE", "LIE_TO_STAND"
  503. )
  504. filteredObservations <- allObservations %>%
  505. filter(activityName %in% desiredActivities) %>%
  506. mutate(observationId = 1:n())
  507. filteredObservations %>% paged_table()
  508. ## get all users
  509. userIds <- allObservations$userId %>% unique()
  510. # split the data
  511. set.seed(42) # seed for reproducibility
  512. filteredObservations %>% paged_table()