# Custom R Script for H2O Autoencoder

**URL:** <https://community.exploratory.io/t/custom-r-script-for-h2o-autoencoder/1947>\
**Category:** HowTo\
**Created:** [November 20, 2020, 1:21pm UTC](https://community.exploratory.io/t/custom-r-script-for-h2o-autoencoder/1947 "2020-11-20T13:21:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Wilcox](https://community.exploratory.io/user_avatar/community.exploratory.io/andrew_wilcox/32/1041_2.png) [@Andrew\_Wilcox](https://community.exploratory.io/u/Andrew_Wilcox)\
**Post date:** [November 20, 2020, 1:21pm UTC](https://community.exploratory.io/t/custom-r-script-for-h2o-autoencoder/1947/1 "2020-11-20T13:21:41Z")

</div>

I am using the Turbofan Engine Degradation Simulation Data Set from NASA located here:

> **[NASA Ames Intelligent Systems Division home](https://www.nasa.gov/intelligent-systems-division/)**
>
> We provide leadership in information technologies by conducting mission-driven, user-centric research and development in computational sciences for NASA applications. We demonstrate and infuse innovative technologies for autonomy, robotics,...

I am trying to remove noise from the sensor data by applying the H2O autoencoder, as recommended here:

> **[Machine Learning Techniques for Predictive Maintenance](https://www.infoq.com/articles/machine-learning-techniques-predictive-maintenance/)**
>
> In this article, the authors explore how we can build a machine learning model to do predictive maintenance of systems. They discuss a sample application using NASA engine failure dataset to predict the Remaining Useful Time (RUL) with regression...

I have followed these instructions to get H2o installed and setup in Exploratory:

[https://blog.exploratory.io/using-h2o-powered-machine-learning-algorithms-in-r-exploratory-9c514fddca8d](https://blog.exploratory.io/using-h2o-powered-machine-learning-algorithms-in-r-exploratory-9c514fddca8d)

I created a custom script, which runs, but the data I return is not in the correct format for Exploratory, and I get the resulting error:

> Please define tidy.Problem with `mutate()` input `model`. ✖ cannot coerce class ‘structure(H2OAutoEncoderModel, package = h2o)’ ℹ Input `model` is `purrr::map2(...)`..

Below is my script and the output from H2O service.

Thanks in advance.

> # library(h2o)
> 
> h2o.init(max\_mem\_size=“60g”, nthreads=-1)
> 
> # Utility function that converts regular data.frame into H2OFrame.
> 
> as\_h2o ← function(df) {
> 
> # Convert character columns into factor.
> 
> for (colname in colnames(df)) {  
> if (class(df[[colname]]) == “character”) {  
> df[[colname]] ← as.factor(df[[colname]])  
> }  
> }  
> df ← as.h2o(df)  
> df  
> }
> 
> build\_h2o\_autoencoder ← function(data) {
> 
> ```
> i <- 1:200
> h2o_data.train <- data[i, -1]
> h2o_data.train <- as_h2o(h2o_data.train)
> 
> itest <- 201:295
> h2o_data.test <- data[itest, -1]
> h2o_data.test <- as_h2o(h2o_data.test)
> 
> #xnames <- colnames(h2o_data.train)
> xnames = c(4:25)
> 
> h2o_data <- as_h2o(data) # convert data into H2OFrame.
> 
> # Train a denoise autoencoder
> denoise_ae <- h2o.deeplearning(
> x = xnames,
> training_frame = h2o_data.train,
> validation_frame = h2o_data.test,
> autoencoder = TRUE,
> hidden = 3,
> activation = 'Tanh',
> sparse = TRUE
> )
> # convert predicted data back to data.frame.
> denoise_ae <- as.data.frame(denoise_ae)
> denoise_ae
> 
> ```
> 
> }

Partial output from the H2O service

> 11-20 09:07:43.577 192.168.2.16:54321 93144 FJ-1-3 INFO: Finished training the Deep Learning model.  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Model Metrics Type: AutoEncoder  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Description: N/A  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: model id: DeepLearning\_model\_R\_1605876768431\_4  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: frame id: df.temporary.sample.16.49%  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: MSE: 0.13167608  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: RMSE: 0.36287197  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Model Metrics Type: AutoEncoder  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Description: N/A  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: model id: DeepLearning\_model\_R\_1605876768431\_4  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: frame id: df  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: MSE: 0.13092372  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: RMSE: 0.36183384  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Variable Importances:  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Variable Relative Importance Scaled Importance Percentage  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 5.70 1.000000 1.000000 0.078082  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 1.01 0.895548 0.895548 0.069926  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 41.69 0.747241 0.747241 0.058346  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 445.00 0.709292 0.709292 0.055383  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 137.36 0.703748 0.703748 0.054950  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 8311.32 0.687593 0.687593 0.053689  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 100.0 0.667932 0.667932 0.052154  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 3.91 0.643408 0.643408 0.050239  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 1112.93 0.603242 0.603242 0.047103  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 8074.83 0.593626 0.593626 0.046352  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: —  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2211.86 0.508927 0.508927 0.039738  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 100.00 0.482159 0.482159 0.037648  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 549.68 0.439516 0.439516 0.034318  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 10.62 0.429863 0.429863 0.033565  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 0.02 0.411523 0.411523 0.032133  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 129.78 0.407064 0.407064 0.031784  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2212 0.390483 0.390483 0.030490  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 1343.43 0.387120 0.387120 0.030227  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 6.3670 0.272774 0.272774 0.021299  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 1 0.207339 0.207339 0.016190  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Status of Neuron Layers (auto-encoder, gaussian distribution, Quadratic loss, 164 weights/biases, 8.1 KB, 606,360 training samples, mini-batch size 1):  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Layer Units Type Dropout L1 L2 Mean Rate Rate RMS Momentum Mean Weight Weight RMS Mean Bias Bias RMS  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 1 23 Input 0.00 %  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2 3 Tanh 0 0.000000 0.000000 1.004224 0.000000 0.000000 0.016175 0.254712 0.000000 0.000000  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 3 23 Tanh 0.000000 0.000000 1.004224 0.000000 0.000000 -0.022812 0.253095 0.000000 0.000000  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Scoring History:  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: Timestamp Duration Training Speed Epochs Iterations Samples Training RMSE Training MSE Validation RMSE Validation MSE  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2020-11-20 09:07:40 0.294 sec 0.00000 obs/sec 0.00000 0 0.000000 0.36287 0.13168 0.36183 0.13092  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2020-11-20 09:07:43 3.340 sec 210468 obs/sec 10.00000 10 606360.000000 0.78124 0.61034 0.78188 0.61133  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: 2020-11-20 09:07:43 3.442 sec 210249 obs/sec 10.00000 10 606360.000000 0.36287 0.13168 0.36183 0.13092  
> 11-20 09:07:43.579 192.168.2.16:54321 93144 FJ-1-3 INFO: ==============================================================================================================================================================================  
> 11-20 09:07:44.369 192.168.2.16:54321 93144 #22755-14 INFO: GET /3/Models/DeepLearning\_model\_R\_1605876768431\_4, parms: {}
