Posit AI Blog: TensorFlow Estimators

WhatsApp Group Join Now
Telegram Group Join Now
Instagram Group Join Now

gave tfestimators package TensorFlow is an R interface for Estimators, a high-level API that provides implementations for many different types of models, including linear models and deep neural networks.

More models are coming soon such as state saving recurrent neural networks, dynamic recurrent neural networks, support vector machines, random forest, KMeans clustering etc. TensorFlow estimators also provide a flexible framework for custom inference to arbitrary new model types.

The framework balances the competing demands for flexibility and simplicity by offering APIs at different levels of abstraction, making common model architectures available out of the box, while providing utilities designed to accelerate experimentation with model architectures. provides a library of

These abstractions make it possible for developers to write models in production-friendly ways, as well as downstream infrastructure for parameter tuning independent of distributed training or model implementation.

To make box models flexible and applicable to a wide range of problems, tfestimators Provides canned estimators that are parameterized not only on traditional hyperparameters but also using feature columns, a declarative specification describing how to interpret the input data.

For more details on the architecture and design of TensorFlow Estimators, please see KDD'17 Paper: TensorFlow estimators: managing simplicity versus flexibility in a high-level machine learning framework.

Start immediately

Installation

For use tfestimatorsyou need to install both. tfestimators Along with the R package Tensor flow himself

First, install the tfestimators R package as follows:

devtools::install_github("rstudio/tfestimators")

Then, use it install_tensorflow() function to install TensorFlow (note that the current tfestimators package requires version 1.3.0 of TensorFlow so if you already have TensorFlow installed you should update if you are running an earlier version):

This will provide you with a default installation of TensorFlow suitable for getting started. See Essay on installation To learn about more advanced options, including installing a version of TensorFlow that takes advantage of NVIDIA GPUs if you have the correct CUDA libraries installed.

Linear regression

Let's construct a simple linear regression model with mtcars dataset To demonstrate the use of approximations. We will explain how Input functions An estimator can be built and used to feed data, how attribute columns can be used to define a set of transformations to be applied to the input data, and how these pieces come together in the estimator interface.

The input function

Estimators can obtain data through input functions. Input functions take an arbitrary data source (in-memory datasets, streaming data, custom data format, etc.) and produce Tensors that can be fed to TensorFlow models. gave tfestimators The package includes a input_fn() function that can create TensorFlow input functions from common R data sources (eg dataframes and matrices). It is also possible to write completely custom input functions.

Here, we define a helper function that will return an input function for our subset. mtcars data set.

library(tfestimators)

# return an input_fn for a given subset of data
mtcars_input_fn <- function(data) {
  input_fn(data, 
           features = c("disp", "cyl"), 
           response = "mpg")
}

Feature column

Next, we define the feature column for our model. Feature columns are used to specify how the tensors received from the input function should be combined and transformed before entering the model training, evaluation, and prediction phases. A feature column can be a simple mapping to some input column (eg column_numeric() for numeric data columns), or conversion of other feature columns (eg column_crossed() defining a new column as the cross of two other feature columns).

Here, we create a list of feature columns that contain two numeric variables − disp And cyl:

cols <- feature_columns(
  column_numeric("disp"),
  column_numeric("cyl")
)

You can also specify multiple feature columns at once:

cols <- feature_columns( 
  column_numeric("disp", "cyl")
)

Using a family of feature column functions we can define various transformations before using the data for modeling.

An estimator

Next, we create an estimator by calling linear_regressor() function and passing it a set of feature columns:

model <- linear_regressor(feature_columns = cols)

training

Now we are ready to train our model. train() The function we will divide. mtcars Set the data into separate training and validation datasets, and feed the training data into it. train(). We will keep 20% data aside for validation.

indices <- sample(1:nrow(mtcars), size = 0.80 * nrow(mtcars))
train <- mtcars[indices, ]
test  <- mtcars[-indices, ]

# train the model
model %>% train(mtcars_input_fn(train))

appraisal

Using , we can estimate the accuracy of the model. evaluate() function, using our 'test' data set for validation.

model %>% evaluate(mtcars_input_fn(test))

prediction

After we finish training the model, we can use it to make predictions from new data.

new_obs <- mtcars[1:3, ]
model %>% predict(mtcars_input_fn(new_obs))

Learn more

After familiarizing yourself with these concepts, these articles cover the basics and key components of using TensorFlow estimators in more detail:

These articles describe more advanced topics/usages:

One of the best ways to learn is to review examples and experiment. See Examples page for several examples to help you get started.

WhatsApp Group Join Now
Telegram Group Join Now
Instagram Group Join Now

Leave a Comment