tfdeploy
tfdeploy copied to clipboard
Predicting a tfestimators model after exporting without session restart fails
Run the following, notice that this is fixed if the r session is restarted after running export_savedmodel().
library(tfestimators)
mtcars_input_fn <- function(data, num_epochs = 1) {
input_fn(data,
features = c("disp", "cyl"),
response = "mpg",
batch_size = 32,
num_epochs = num_epochs)
}
cols <- feature_columns(column_numeric("disp"), column_numeric("cyl"))
model <- linear_regressor(feature_columns = cols)
indices <- sample(1:nrow(mtcars), size = 0.80 * nrow(mtcars))
train <- mtcars[indices, ]
test <- mtcars[-indices, ]
model %>% train(mtcars_input_fn(train, num_epochs = 10))
export_savedmodel(model, "savedmodel")
# transform data frame records into list of named lists
test_records <- mtcars[1:5, c("disp", "cyl")]
test_records <- lapply(1:nrow(test_records), function(i) as.list(test_records[i,]))
# generate predictions
library(tfdeploy)
predict_savedmodel(
test_records,
"savedmodel",
type = "export",
signature_name = "predict"
)
Error in py_call_impl(callable, dots$args, dots$keywords) :
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype float and shape [?,1]
[[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op u'Placeholder_2', defined at:
File "/Users/javierluraschi/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/saved_model/loader_impl.py", line 216, in load
saver = tf_saver.import_meta_graph(meta_graph_def_to_load, **saver_kwargs)
File "/Users/javierluraschi/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1810, in import_meta_graph
**kwargs)
File "/Users/javierluraschi/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 660, in import_scoped_meta_graph
producer_op_list=producer_op_list)
File "/Users/javierluraschi/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/
The problem is that under export_savedmodel, we create a placeholder which seems to affect subsequent use of the TF session...
See https://github.com/rstudio/tfestimators/compare/bugfix/export-savedmodel-default?expand=1