TensorFlow Model APIs#
TensorFlow saved models can be turned into APIs that evaluate the model when called.
Publishing#
You can deploy saved models from R using the instructions in the Publishing from R chapter.
Or you can deploy from the command line using the instructions in the Creating a Manifest File section of the Publishing R or Other Content chapter.
Using the Deployed Model#
The home page on your new TensorFlow Model API will explain how it can be used. Much like Plumber, you can use RStudio Connect access controls and RStudio Connect API Keys to secure your Model API, or to allow everyone to use it.
All TensorFlow Model API requests are mostly the same. For the following examples,
assume that your TensorFlow Model API is running open to the public at:
https://localhost:3939/content/12
. Assume also that your TensorFlow model accepts
as input a 2-tensor (matrix) of floating point values with dimensions Infinity by 2.
You could call your TensorFlow Model API like so:
curl https://localhost:3939/content/12/serving_default/predict -XPOST --data-binary='\
{ \
"instances": [\
[[5.4, 3.2]]\
]\
}'
Note that the TensorFlow Model API is strict about the number of dimensions passed
in instances. The instances
array does not count towards tensor dimensions:
"instances": [[[2.4]]]
is one instance of a 2-tensor (matrix) with dimensions 1x1"instances": [[[[2.3, 4.5],[5.6, 7.8]]]]
is one instance of a 3-tensor with dimensions 1x2x2
Your TensorFlow Model API will return the predicted values as you configured it. For example, if your model was configured to respond with a 0-tensor (scalar) of floats, you might get the following:
{
"predictions": [1.2]
}
The rstudio/tfdeploy repository contains some example scripts for building and exporting simple models, so you can try them before you upload some of your own. For example:
account <- "replace me with your username"
server <- "replace me with your server host"
# the `keras` package must be installed for models/keras-mnist.R
install.packages("keras")
devtools::install_github("rstudio/tfdeploy")
source(system.file("models","keras-mnist.R", package="tfdeploy"))
rsconnect::deployTFModel("keras-mnist", account = account, server = server)