H2o encoders starter

[This article was first published on koaning.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This document contains a startup script for H2O in R. It is a silly example (why would anybody want to train a deep encoder on the iris dataset) but it helps people get started. This setup is meant for local use, not for cluster setup.

Just copy the code in!

Setup

library(readr)
library(dplyr)
library(ggplot2)
library(GGally)
library(h2o)

h2o.init(ip = 'localhost', port = 54321, nthreads= -1, max_mem_size = '4g')
h2o.clusterInfo()

Autoencoder

h2o_df <- as.h2o(iris)

mod_nn <- h2o.deeplearning(
  x = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width'),
  training_frame = h2o_df,
  hidden = c(2),
  epochs = 100,
  activation = 'Tanh',
  autoencoder = TRUE
)

features <- h2o.deepfeatures(mod_nn, h2o_df, layer=1)
pltr_nn <- features %>% 
  as.data.frame %>% 
  mutate(species = iris$Species)

colnames(pltr_nn) <- c("x1", "x2", "species")

ggplot() + 
  geom_point(data=pltr_nn, aes(x1, x2, colour = species))

PCA

mod_pca <- h2o.prcomp(
  x = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width'),
  training_frame = h2o_df, k = 2, transform = 'STANDARDIZE'
)

pltr_pca <- h2o.predict(mod_pca, h2o_df, num_pc=2) %>% 
  as.data.frame %>% 
  mutate(species = iris$Species)

colnames(pltr_pca) <- c("x1", "x2", "species")

ggplot() + 
  geom_point(data=pltr_pca, aes(x1, x2, colour = species))

The syntax is very lovely R-like. One just has to remember that h2o assumes a slightly different internal datastructure to keep things performant (similarly to SparkR). It does offer some machine learning possiblilities not available to SparkR users. Features!

You should also be able to find this blog on: r-bloggers

To leave a comment for the author, please follow the link and comment on their blog: koaning.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)