--- title: "Stat. 654 Quiz brulee using torch" author: "Your Name Here" date: "" format: html: embed-resources: true --- ## Feed-Forward Neural Network for the Google Tensorflow Playground XOR Data ### Clone the [TFPlayground](https://github.com/hyounesy/TFPlaygroundPSA) Github repository into your R Project folder. To clone the repository you can use RStudio File > New Project > Version Control > Git and paste the URL of the repository into the Git Repository URL box. Then select a folder to clone the repository into. Click the Green button and copy the ulr: *https://github.com/hyounesy/TFPlaygroundPSA.git* Then paste the URL into the Git Repository URL box. Select a folder to clone the repository into. Click the Create Project button. Use the data in *../data/tiny/xor_25/input.txt* to create a feed-forward neural network to classify the data. Use the *keras* package to create the model. ### Load the required libraries ```{r} library(tidyverse) library(tidymodels) library(readr) library(janitor) library(brulee) library(keras) ``` ### Load the data ```{r} input <- read_delim("data/tiny/xor_25/input.txt", delim = "\t", escape_double = FALSE, trim_ws = TRUE) input <- input |> clean_names() |> select(-pid) |> mutate(label = as.factor(label)) |> tibble() head(input) ``` ### Split the data into training and testing sets ```{r} n <- nrow(input) input_parts <- input |> initial_split(prop = 0.8) train <- input_parts |> training() test <- input_parts |> testing() list(train, test) |> map_int(nrow) ``` ### Visualize the data ```{r} train |> ggplot(aes(x = x1, y = x2, color = factor(label))) + geom_point() ``` ```{r} test |> ggplot(aes(x = x1, y = x2, color = factor(label))) + geom_point() ``` ### brulee uses torch ```{r} mod_nn <- mlp(mode = "classification", hidden_units = 8) |> set_engine("brulee") |> fit(label ~ x1 + x2, data = train) mod_nn # train pred <- train |> bind_cols( predict(mod_nn, new_data = train, type = "class") ) |> rename(mod_nn = .pred_class) accuracy(pred, label, mod_nn) # test pred <- test |> bind_cols( predict(mod_nn, new_data = test, type = "class") ) |> rename(mod_nn = .pred_class) accuracy(pred, label, mod_nn) ``` ### brulee two layer ```{r} mod_nn <- mlp(mode = "classification", hidden_units = 8) |> set_engine("brulee_two_layer") |> fit(label ~ x1 + x2, data = train) mod_nn # train pred <- train |> bind_cols( predict(mod_nn, new_data = train, type = "class") ) |> rename(mod_nn = .pred_class) accuracy(pred, label, mod_nn) # test pred <- test |> bind_cols( predict(mod_nn, new_data = test, type = "class") ) |> rename(mod_nn = .pred_class) accuracy(pred, label, mod_nn) ```