# Dataset API

## Overview

We can access the TensorFlow Dataset API via the tfdatasets package, which enables us to create scalable input pipelines that can be used with tfestimators. In this vignette, we demonstrate the capability to stream datasets stored on disk for training by building a classifier on the iris dataset.

## Dataset Preparation

Let’s assume we’re given a dataset (which could be arbitrarily large) split into training and validation, and a small sample of the dataset. To simulate this scenario, we’ll create a few CSV files as follows:

set.seed(123)
train_idx <- sample(nrow(iris), nrow(iris) * 2/3)

iris_train <- iris[train_idx,]
iris_validation <- iris[-train_idx,]
iris_sample <- iris_train %>%

write.csv(iris_train, "iris_train.csv", row.names = FALSE)
write.csv(iris_validation, "iris_validation.csv", row.names = FALSE)
write.csv(iris_sample, "iris_sample.csv", row.names = FALSE)

## Estimator Construction

We construct the classifier as usual – see Estimator Basics for details on feature columns and creating estimators.

library(tfestimators)
response <- "Species"
features <- setdiff(names(iris), response)
feature_columns <- feature_columns(
column_numeric(features)
)

classifier <- dnn_classifier(
feature_columns = feature_columns,
hidden_units = c(16, 32, 16),
n_classes = 3,
label_vocabulary = c("setosa", "virginica", "versicolor")
)

## Input Function

The creation of the input function is similar to the in-memory case. However, instead of passing data frames or matrices to iris_input_fn(), we pass TensorFlow dataset objects which are internally iterators of the dataset files.

iris_input_fn <- function(data) {
input_fn(data, features = features, response = response)
}

iris_spec <- csv_record_spec("iris_sample.csv")
iris_train <- text_line_dataset(
"iris_train.csv", record_spec = iris_spec) %>%
dataset_batch(10) %>%
dataset_repeat(10)
iris_validation <- text_line_dataset(
"iris_validation.csv", record_spec = iris_spec) %>%
dataset_batch(10) %>%
dataset_repeat(1)

The csv_record_spec() function is a helper function that creates a specification from a sample file; the returned specification is required by the text_line_dataset() function to parse the files. There are many transformations available for dataset objects, but here we just demonstrate dataset_batch() and dataset_repeat() which control the batch size and how many times we iterate through the dataset files, respectively.

## Training and Evaluation

Once the input functions and datasets are defined, the training and evaluation interface is exactly the same as in the in-memory case.

history <- train(classifier, input_fn = iris_input_fn(iris_train))
plot(history)
predictions <- predict(classifier, input_fn = iris_input_fn(iris_validation))
predictions
evaluation <- evaluate(classifier, input_fn = iris_input_fn(iris_validation))
evaluation

## Learning More

See the documetnation for the tfdatasets package for additional details on using TensorFlow datasets.
{"ft_lang_label":"__label__en","ft_lang_prob":0.63373244,"math_prob":0.9271685,"size":2702,"snap":"2019-13-2019-22","text_gpt3_token_len":584,"char_repetition_ratio":0.13750927,"word_repetition_ratio":0.011560693,"special_character_ratio":0.23131014,"punctuation_ratio":0.13349515,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9819785,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-21T06:40:17Z\",\"WARC-Record-ID\":\"<urn:uuid:5affc2e1-e5fe-4f6f-9c65-0f014cee2f7d>\",\"Content-Length\":\"37290\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5dda5ded-942d-461a-9717-6904bac213fe>\",\"WARC-Concurrent-To\":\"<urn:uuid:f896f064-f7c9-4d3e-a193-e8047912851a>\",\"WARC-IP-Address\":\"99.84.181.119\",\"WARC-Target-URI\":\"https://tensorflow.rstudio.com/tfestimators/articles/dataset_api.html\",\"WARC-Payload-Digest\":\"sha1:NYXMW3K7LYF6CCEM3Y7F7WEYLZYEI3UO\",\"WARC-Block-Digest\":\"sha1:6YZG6VILPWVTHPQNWBPDEMYMA7UFAHAA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202496.35_warc_CC-MAIN-20190321051516-20190321073516-00457.warc.gz\"}"}