├── .Rbuildignore ├── .gitignore ├── .travis.yml ├── DESCRIPTION ├── NAMESPACE ├── NEWS.md ├── R ├── boosted_trees_estimators.R ├── dnn_estimators.R ├── dnn_linear_combined_estimators.R ├── estimator_keys.R ├── feature_columns.R ├── formulas.R ├── generic_methods.R ├── history.R ├── imports.R ├── input_fn.R ├── keras_estimator.R ├── linear_estimators.R ├── package.R ├── pad.R ├── parsing_utils.R ├── preconditions.R ├── print_methods.R ├── reexport-tidyselect.R ├── reexports.R ├── run_config.R ├── run_context.R ├── session_run_hooks_builtin_wrappers.R ├── session_run_hooks_custom.R ├── simplify_fn.R ├── tf_custom_estimator.R ├── tf_estimator.R ├── tidyselect.R ├── train_and_evaluate.R ├── utils_miscs.R └── utils_tf.R ├── README.md ├── cran-comments.md ├── docs ├── articles │ ├── creating_estimators.html │ ├── deployment.html │ ├── estimator_basics.html │ ├── examples │ │ ├── .gitignore │ │ ├── creating_estimators_tutorial.R │ │ ├── custom_estimator.R │ │ ├── custom_estimator.html │ │ ├── index.html │ │ ├── iris_custom_decay_dnn.R │ │ ├── iris_custom_decay_dnn.html │ │ ├── iris_dnn_classifier.R │ │ ├── iris_dnn_classifier.html │ │ ├── layers_tutorial.R │ │ ├── mnist.R │ │ ├── mnist.html │ │ ├── tensorflow_layers.R │ │ ├── tensorflow_layers.html │ │ ├── wide_and_deep.R │ │ └── wide_and_deep.html │ ├── experiments.html │ ├── feature_columns.html │ ├── images │ │ ├── estimator-apis.png │ │ ├── estimator.png │ │ ├── experiment.png │ │ ├── tensorboard-graph-details.png │ │ ├── tensorboard-graph.png │ │ ├── tensorboard-loss.png │ │ ├── tensorflow-2-100709406-large.jpg │ │ └── tensorflow-architecture.png │ ├── index.html │ ├── input_functions.html │ ├── layers.html │ ├── linear_dnn_estimators.html │ ├── linear_estimators.html │ ├── parsing_spec.html │ ├── recurrent_neural_networks.html │ ├── run_configuration.html │ ├── run_hooks.html │ ├── simplify_function.html │ ├── support_vector_machines.html │ ├── tensorboard.html │ └── tensorflow_layers.html ├── authors.html ├── extra.css ├── extra.js ├── images │ └── .gitigore ├── index.html ├── jquery.sticky-kit.min.js ├── link.svg ├── pkgdown.css ├── pkgdown.js └── reference │ ├── EstimatorSessionRunHook.html │ ├── classifier_parse_example_spec.html │ ├── coef.html │ ├── coef.tf_estimator.html │ ├── column-scope.html │ ├── column_base.html │ ├── column_bucketized.html │ ├── column_categorical_weighted.html │ ├── column_categorical_with_hash_bucket.html │ ├── column_categorical_with_identity.html │ ├── column_categorical_with_vocabulary_file.html │ ├── column_categorical_with_vocabulary_list.html │ ├── column_crossed.html │ ├── column_embedding.html │ ├── column_indicator.html │ ├── column_numeric.html │ ├── column_weighted_categorical.html │ ├── dnn_classifier.html │ ├── dnn_estimators.html │ ├── dnn_linear_combined_classifier.html │ ├── dnn_linear_combined_estimators.html │ ├── dnn_linear_combined_regressor.html │ ├── dnn_regressor.html │ ├── dynamic_rnn.html │ ├── estimator.html │ ├── estimator_spec.html │ ├── estimators.html │ ├── eval_spec.html │ ├── evaluate.html │ ├── evaluate.tf_estimator.html │ ├── evaluate.tf_experiment.html │ ├── experiment.html │ ├── experiment.tf_estimator.html │ ├── export_savedmodel.html │ ├── export_savedmodel.tf_estimator.html │ ├── feature_columns.html │ ├── get_latest_checkpoint.html │ ├── get_saved_history.html │ ├── graph_keys.html │ ├── hook_checkpoint_saver.html │ ├── hook_global_step_waiter.html │ ├── hook_history_saver.html │ ├── hook_logging_tensor.html │ ├── hook_nan_tensor.html │ ├── hook_progress_bar.html │ ├── hook_step_counter.html │ ├── hook_stop_at_step.html │ ├── hook_summary_saver.html │ ├── index.html │ ├── input_fn.html │ ├── input_layer.html │ ├── latest_checkpoint.html │ ├── linear_classifier.html │ ├── linear_estimators.html │ ├── linear_regressor.html │ ├── metric_keys.html │ ├── mode_keys.html │ ├── model_dir.html │ ├── numpy_input_fn.html │ ├── pipe.html │ ├── predict.html │ ├── predict.tf_estimator.html │ ├── prediction_keys.html │ ├── reexports.html │ ├── regressor_parse_example_spec.html │ ├── run_config.html │ ├── session_run_args.html │ ├── session_run_hook.html │ ├── set_column_names.html │ ├── state_saving_rnn.html │ ├── svm_classifier.html │ ├── task_type.html │ ├── tfestimators.html │ ├── train-evaluate-predict.html │ ├── train.html │ ├── train.tf_estimator.html │ ├── train.tf_experiment.html │ ├── train_and_evaluate.html │ ├── train_and_evaluate.tf_estimator.html │ ├── train_and_evaluate.tf_experiment.html │ └── train_spec.html ├── images ├── index.Rmd ├── inst ├── CITATION └── python │ └── estimatortools │ ├── .gitignore │ ├── __init__.py │ └── session_run_hooks.py ├── man-roxygen ├── roxlate-activation-fn.R ├── roxlate-object-estimator.R └── roxlate-object-experiment.R ├── man ├── boosted_trees_estimators.Rd ├── classifier_parse_example_spec.Rd ├── column-scope.Rd ├── column_base.Rd ├── column_bucketized.Rd ├── column_categorical_weighted.Rd ├── column_categorical_with_hash_bucket.Rd ├── column_categorical_with_identity.Rd ├── column_categorical_with_vocabulary_file.Rd ├── column_categorical_with_vocabulary_list.Rd ├── column_crossed.Rd ├── column_embedding.Rd ├── column_indicator.Rd ├── column_numeric.Rd ├── dnn_estimators.Rd ├── dnn_linear_combined_estimators.Rd ├── estimator.Rd ├── estimator_spec.Rd ├── estimators.Rd ├── eval_spec.Rd ├── evaluate.tf_estimator.Rd ├── experiment.Rd ├── export_savedmodel.tf_estimator.Rd ├── feature_columns.Rd ├── graph_keys.Rd ├── hook_checkpoint_saver.Rd ├── hook_global_step_waiter.Rd ├── hook_history_saver.Rd ├── hook_logging_tensor.Rd ├── hook_nan_tensor.Rd ├── hook_progress_bar.Rd ├── hook_step_counter.Rd ├── hook_stop_at_step.Rd ├── hook_summary_saver.Rd ├── input_fn.Rd ├── input_layer.Rd ├── keras_model_to_estimator.Rd ├── latest_checkpoint.Rd ├── linear_estimators.Rd ├── metric_keys.Rd ├── mode_keys.Rd ├── model_dir.Rd ├── numpy_input_fn.Rd ├── plot.tf_estimator_history.Rd ├── predict.tf_estimator.Rd ├── prediction_keys.Rd ├── reexports.Rd ├── regressor_parse_example_spec.Rd ├── run_config.Rd ├── session_run_args.Rd ├── session_run_hook.Rd ├── task_type.Rd ├── tfestimators.Rd ├── train-evaluate-predict.Rd ├── train.tf_estimator.Rd ├── train_and_evaluate.tf_estimator.Rd ├── train_spec.Rd └── variable_names_values.Rd ├── pkgdown ├── _pkgdown.yml ├── extra.css └── extra.js ├── scripts └── travis_install.sh ├── tests ├── testthat.R └── testthat │ ├── helper-utils.R │ ├── test-boosted-trees-estimators.R │ ├── test-dnn_linear_combined-estimators.R │ ├── test-estimator-utils.R │ ├── test-examples.R │ ├── test-feature-columns.R │ ├── test-formulas.R │ ├── test-hooks.R │ ├── test-input-fn.R │ ├── test-linear-estimators.R │ ├── test-save.R │ ├── test-tf-custom-models.R │ ├── test-tf-models.R │ └── test-training.R ├── tfestimators.Rproj └── vignettes ├── creating_estimators.Rmd ├── dataset_api.Rmd ├── estimator_basics.Rmd ├── examples ├── .gitignore ├── custom_estimator.R ├── custom_estimator.Rmd ├── index.Rmd ├── iris_custom_decay_dnn.R ├── iris_custom_decay_dnn.Rmd ├── iris_dnn_classifier.R ├── iris_dnn_classifier.Rmd ├── mnist.R ├── mnist.Rmd ├── tensorflow_layers.R ├── tensorflow_layers.Rmd ├── wide_and_deep.R └── wide_and_deep.Rmd ├── feature_columns.Rmd ├── images ├── estimator-apis.png ├── estimator.png ├── experiment.png ├── tensorboard-graph-details.png ├── tensorboard-graph.png ├── tensorboard-loss.png └── tensorflow-architecture.png ├── input_functions.Rmd ├── parsing_spec.Rmd ├── run_hooks.Rmd ├── tensorboard.Rmd └── tensorflow_layers.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^CRAN-RELEASE$ 2 | ^cran-comments\.md$ 3 | ^.*\.Rproj$ 4 | ^\.Rproj\.user$ 5 | ^docs/ 6 | ^images/ 7 | ^internal/ 8 | ^man-roxygen/ 9 | ^mnist-data/ 10 | ^vignettes/examples/mnist-data/ 11 | ^scripts/ 12 | ^travis/ 13 | ^.*\.csv$ 14 | ^\.travis\.yml$ 15 | ^index.*$ 16 | ^pkgdown$ 17 | ^inst/python/estimatortools/__pycache__/ 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/.DS_store 2 | .Rproj.user 3 | .Rhistory 4 | .RData 5 | .Ruserdata 6 | internal/ 7 | .!* 8 | *.csv 9 | MNIST-data 10 | runs/ 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: r 4 | 5 | warnings_are_errors: true 6 | 7 | env: 8 | global: 9 | - TENSORFLOW_TEST_EXAMPLES="1" 10 | matrix: 11 | - TF_VERSION="1.3" 12 | - TF_VERSION="1.4" 13 | - TF_VERSION="1.5" 14 | - TF_VERSION="1.6" 15 | - TF_VERSION="1.8" 16 | - TF_VERSION="1.9" 17 | - TF_VERSION="1.10" 18 | - TF_VERSION="1.11" 19 | - TF_VERSION="1.12" 20 | - TF_VERSION="nightly" 21 | 22 | os: 23 | - linux 24 | 25 | cache: 26 | packages: true 27 | directories: 28 | - $HOME/.cache/pip 29 | 30 | before_script: 31 | - source scripts/travis_install.sh 32 | 33 | addons: 34 | apt: 35 | packages: 36 | - openmpi-bin 37 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: tfestimators 2 | Type: Package 3 | Title: Interface to 'TensorFlow' Estimators 4 | Version: 1.9.2 5 | Authors@R: c( 6 | person("JJ", "Allaire", role = c("aut"), 7 | email = "jj@rstudio.com"), 8 | person("Yuan", "Tang", role = c("aut"), 9 | email = "terrytangyuan@gmail.com", 10 | comment = c(ORCID = "0000-0001-5243-233X")), 11 | person("Kevin", "Ushey", role = c("aut"), 12 | email = "kevin@rstudio.com"), 13 | person("Kevin", "Kuo", role = c("aut"), 14 | email = "kevin.kuo@rstudio.com", 15 | comment = c(ORCID = "0000-0001-7803-7901")), 16 | person("Tomasz", "Kalinowski", role = c("cre"), 17 | email = "tomasz.kalinowski@rstudio.com"), 18 | person("Daniel", "Falbel", role = c("ctb", "cph")), 19 | person("RStudio", role = c("cph", "fnd")), 20 | person(family = "Google Inc.", role = c("cph")) 21 | ) 22 | Description: Interface to 'TensorFlow' Estimators 23 | , a high-level 24 | API that provides implementations of many different model types 25 | including linear models and deep neural networks. 26 | License: Apache License 2.0 27 | URL: https://github.com/rstudio/tfestimators 28 | BugReports: https://github.com/rstudio/tfestimators/issues 29 | SystemRequirements: TensorFlow (https://www.tensorflow.org/) 30 | Encoding: UTF-8 31 | Depends: 32 | R (>= 3.1) 33 | Imports: 34 | forge, 35 | magrittr, 36 | progress, 37 | reticulate (>= 1.10), 38 | rlang (>= 0.3), 39 | tensorflow (>= 1.9), 40 | tfruns (>= 1.1), 41 | tidyselect, 42 | utils, 43 | purrr, 44 | tibble, 45 | tidyr 46 | Roxygen: list(markdown = TRUE) 47 | RoxygenNote: 7.1.1 48 | Suggests: 49 | ggplot2, 50 | modelr (>= 0.1.1), 51 | testthat, 52 | rmarkdown, 53 | knitr 54 | VignetteBuilder: knitr 55 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # tfestimators 1.9.2 2 | 3 | * Added rmarkdown package to suggests at request of CRAN 4 | 5 | # tfestimators 1.9.1 6 | 7 | * Fixes for rlang 0.3 compatibility (#156, #159). 8 | 9 | # tfestimators 1.9.0 10 | 11 | * Add input checking to exported functions (#150). 12 | 13 | * `input_fn.list()` no longer performs type coercion on numpy arrays. 14 | 15 | * Added `boosted_trees_regressors()` and `boosted_trees_classifier()` (#146). 16 | -------------------------------------------------------------------------------- /R/dnn_estimators.R: -------------------------------------------------------------------------------- 1 | #' Deep Neural Networks 2 | #' 3 | #' Create a deep neural network (DNN) estimator. 4 | #' 5 | #' @inheritParams estimators 6 | #' 7 | #' @template roxlate-activation-fn 8 | #' @templateVar name activation_fn 9 | #' @templateVar default relu 10 | #' 11 | #' @param hidden_units An integer vector, indicating the number of hidden 12 | #' units in each layer. All layers are fully connected. For example, 13 | #' `c(64, 32)` means the first layer has 64 nodes, and the second layer 14 | #' has 32 nodes. 15 | #' @param optimizer Either the name of the optimizer to be used when training 16 | #' the model, or a TensorFlow optimizer instance. Defaults to the Adagrad 17 | #' optimizer. 18 | #' @param dropout When not `NULL`, the probability we will drop out a given 19 | #' coordinate. 20 | #' 21 | #' @family canned estimators 22 | #' @name dnn_estimators 23 | NULL 24 | 25 | #' @inheritParams dnn_estimators 26 | #' @name dnn_estimators 27 | #' @export 28 | dnn_regressor <- function(hidden_units, 29 | feature_columns, 30 | model_dir = NULL, 31 | label_dimension = 1L, 32 | weight_column = NULL, 33 | optimizer = "Adagrad", 34 | activation_fn = "relu", 35 | dropout = NULL, 36 | input_layer_partitioner = NULL, 37 | config = NULL) 38 | { 39 | args <- as.list(environment(), all.names = TRUE) 40 | 41 | estimator <- py_suppress_warnings( 42 | tf$estimator$DNNRegressor( 43 | hidden_units = cast_integer_list(hidden_units), 44 | feature_columns = ensure_nullable_list(feature_columns), 45 | model_dir = resolve_model_dir(model_dir), 46 | label_dimension = cast_scalar_integer(label_dimension), 47 | weight_column = cast_nullable_string(weight_column), 48 | optimizer = optimizer, 49 | activation_fn = resolve_activation_fn(activation_fn), 50 | dropout = cast_nullable_scalar_double(dropout), 51 | input_layer_partitioner = input_layer_partitioner, 52 | config = config 53 | ) 54 | ) 55 | 56 | new_tf_regressor(estimator, args = args, 57 | subclass = "tf_estimator_regressor_dnn_regressor") 58 | } 59 | 60 | #' @inheritParams dnn_estimators 61 | #' @name dnn_estimators 62 | #' @export 63 | dnn_classifier <- function(hidden_units, 64 | feature_columns, 65 | model_dir = NULL, 66 | n_classes = 2L, 67 | weight_column = NULL, 68 | label_vocabulary = NULL, 69 | optimizer = "Adagrad", 70 | activation_fn = "relu", 71 | dropout = NULL, 72 | input_layer_partitioner = NULL, 73 | config = NULL) 74 | { 75 | args <- as.list(environment(), all.names = TRUE) 76 | 77 | estimator <- py_suppress_warnings( 78 | tf$estimator$DNNClassifier( 79 | hidden_units = cast_integer_list(hidden_units), 80 | feature_columns = ensure_nullable_list(feature_columns), 81 | model_dir = resolve_model_dir(model_dir), 82 | n_classes = cast_scalar_integer(n_classes), 83 | weight_column = cast_nullable_string(weight_column), 84 | label_vocabulary = label_vocabulary, 85 | optimizer = optimizer, 86 | activation_fn = resolve_activation_fn(activation_fn), 87 | dropout = cast_nullable_scalar_double(dropout), 88 | input_layer_partitioner = input_layer_partitioner, 89 | config = config 90 | ) 91 | ) 92 | 93 | new_tf_classifier(estimator, args = args, 94 | subclass = "tf_estimator_classifier_dnn_classifier") 95 | } 96 | 97 | -------------------------------------------------------------------------------- /R/formulas.R: -------------------------------------------------------------------------------- 1 | is.formula <- function(x) { 2 | inherits(x, "formula") 3 | } 4 | 5 | parse_formula <- function(formula, data = NULL) { 6 | formula <- validate_formula(formula) 7 | n <- length(formula) 8 | 9 | # extract response 10 | response <- if (n == 3) { 11 | lhs <- formula[[2]] 12 | if (!(is.symbol(lhs) || is.character(lhs))) 13 | stop("expected symbolic response; got '", lhs, "'") 14 | as.character(lhs) 15 | } 16 | 17 | # extract features 18 | terms <- stats::terms(formula, data = data) 19 | features <- attr(terms, "term.labels") 20 | intercept <- as.logical(attr(terms, "intercept")) 21 | 22 | list(features = features, 23 | response = response, 24 | intercept = intercept) 25 | } 26 | 27 | validate_formula <- function(formula) { 28 | formula <- stats::as.formula(formula) 29 | for (i in 2:length(formula)) 30 | validate_formula_operators(formula[[i]]) 31 | formula 32 | } 33 | 34 | validate_formula_operators <- function(object) { 35 | n <- length(object) 36 | if (is.call(object) && n > 0) { 37 | 38 | # check that this is a call to a known operator 39 | op <- object[[1]] 40 | if (!is.symbol(op) && !is.function(eval(op))) 41 | stop("expected a symbol or function for call; got '", deparse(op), "'") 42 | 43 | ch <- as.character(op) 44 | if (!any(ch %in% c("+", "-", "(", "$"))) 45 | stop("unhandled formula operator: expected '+' or '-'; got '", ch, "'") 46 | 47 | # validate the rest of the calls 48 | for (i in 1:n) 49 | validate_formula_operators(object[[i]]) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /R/generic_methods.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' @importFrom tensorflow evaluate train train_and_evaluate export_savedmodel 4 | NULL 5 | 6 | 7 | #' Construct an Experiment 8 | #' 9 | #' Construct an experiment object. 10 | #' 11 | #' @param object An \R object. 12 | #' @param ... Optional arguments passed on to implementing methods. 13 | experiment <- function(object, ...) { 14 | UseMethod("experiment") 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /R/imports.R: -------------------------------------------------------------------------------- 1 | #' @import reticulate 2 | #' @import rlang 3 | #' @import tensorflow 4 | #' @import tidyselect 5 | #' @import forge 6 | #' 7 | #' @importFrom magrittr %>% 8 | #' @importFrom stats coef 9 | #' @importFrom purrr reduce 10 | #' @importFrom tibble as_tibble type_sum 11 | #' @importFrom utils capture.output str 12 | NULL 13 | -------------------------------------------------------------------------------- /R/keras_estimator.R: -------------------------------------------------------------------------------- 1 | new_tf_keras_estimator <- function(estimator, args = NULL, ..., 2 | subclass = NULL) { 3 | new_tf_estimator(estimator, args, ..., 4 | subclass = c(subclass, "tf_keras_estimator")) 5 | } 6 | 7 | #' Keras Estimators 8 | #' 9 | #' Create an Estimator from a compiled Keras model 10 | #' 11 | #' @param keras_model A keras model. 12 | #' @param keras_model_path Directory to a keras model on disk. 13 | #' @param custom_objects Dictionary for custom objects. 14 | #' @param model_dir Directory to save Estimator model parameters, graph and etc. 15 | #' @param config Configuration object. 16 | #' 17 | #' @export 18 | keras_model_to_estimator <- function( 19 | keras_model = NULL, keras_model_path = NULL, custom_objects = NULL, 20 | model_dir = NULL, config = NULL) { 21 | 22 | if (is.null(keras_model) && is.null(keras_model_path)) 23 | stop("Either keras_model or keras_model_path needs to be provided.") 24 | 25 | if (!is.null(keras_model_path)) { 26 | if (!is.null(keras_model)) 27 | stop("Please specity either keras_model or keras_model_path but not both.") 28 | if (grepl("^(gs://|storage\\.googleapis\\.com)", keras_model_path)) 29 | stop("'keras_model_path' is not a local path. Please copy the model locally first.") 30 | keras_model <- tf$keras$models$load_model(keras_model_path) 31 | } 32 | 33 | tryCatch(reticulate::py_get_attr(keras_model, "optimizer"), 34 | error = function(e) stop( 35 | "Given keras model has not been compiled yet. Please compile first 36 | before creating the estimator.") 37 | ) 38 | 39 | args <- as.list(environment(), all.names = TRUE) 40 | 41 | estimator <- py_suppress_warnings( 42 | tf$keras$estimator$model_to_estimator( 43 | keras_model = keras_model, 44 | keras_model_path = keras_model_path, 45 | custom_objects = custom_objects, 46 | model_dir = model_dir, 47 | config = config 48 | )) 49 | 50 | new_tf_keras_estimator(estimator, args = args) 51 | } 52 | -------------------------------------------------------------------------------- /R/linear_estimators.R: -------------------------------------------------------------------------------- 1 | #' Construct a Linear Estimator 2 | #' 3 | #' Construct a linear model, which can be used to predict a continuous outcome 4 | #' (in the case of `linear_regressor()`) or a categorical outcome (in the case 5 | #' of `linear_classifier()`). 6 | #' 7 | #' @inheritParams estimators 8 | #' 9 | #' @param optimizer Either the name of the optimizer to be used when training 10 | #' the model, or a TensorFlow optimizer instance. Defaults to the FTRL 11 | #' optimizer. 12 | #' 13 | #' @family canned estimators 14 | #' @name linear_estimators 15 | NULL 16 | 17 | #' @inheritParams linear_estimators 18 | #' @name linear_estimators 19 | #' @export 20 | linear_regressor <- function(feature_columns, 21 | model_dir = NULL, 22 | label_dimension = 1L, 23 | weight_column = NULL, 24 | optimizer = "Ftrl", 25 | config = NULL, 26 | partitioner = NULL) 27 | { 28 | args <- as.list(environment(), all.names = TRUE) 29 | 30 | estimator <- py_suppress_warnings( 31 | tf$estimator$LinearRegressor( 32 | feature_columns = ensure_nullable_list(feature_columns), 33 | model_dir = resolve_model_dir(model_dir), 34 | weight_column = cast_nullable_string(weight_column), 35 | optimizer = optimizer, 36 | config = config, 37 | partitioner = partitioner, 38 | label_dimension = cast_scalar_integer(label_dimension) 39 | ) 40 | ) 41 | 42 | new_tf_regressor(estimator, args = args, 43 | subclass = "tf_estimator_regressor_linear_regressor") 44 | } 45 | 46 | #' @inheritParams linear_estimators 47 | #' @name linear_estimators 48 | #' @export 49 | linear_classifier <- function(feature_columns, 50 | model_dir = NULL, 51 | n_classes = 2L, 52 | weight_column = NULL, 53 | label_vocabulary = NULL, 54 | optimizer = "Ftrl", 55 | config = NULL, 56 | partitioner = NULL) 57 | { 58 | args <- as.list(environment(), all.names = TRUE) 59 | 60 | estimator <- py_suppress_warnings( 61 | tf$estimator$LinearClassifier( 62 | feature_columns = ensure_nullable_list(feature_columns), 63 | model_dir = resolve_model_dir(model_dir), 64 | n_classes = cast_scalar_integer(n_classes), 65 | weight_column = cast_nullable_string(weight_column), 66 | label_vocabulary = label_vocabulary, 67 | optimizer = optimizer, 68 | config = config, 69 | partitioner = partitioner 70 | ) 71 | ) 72 | 73 | new_tf_classifier(estimator, args = args, 74 | subclass = "tf_estimator_classifier_linear_classifier") 75 | } 76 | -------------------------------------------------------------------------------- /R/package.R: -------------------------------------------------------------------------------- 1 | #' High-level Estimator API in TensorFlow for R 2 | #' 3 | #' This library provides an R interface to the 4 | #' \href{https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/estimator}{Estimator} 5 | #' API inside TensorFlow that's designed to streamline the process of creating, 6 | #' evaluating, and deploying general machine learning and deep learning models. 7 | #' 8 | #' \href{https://www.tensorflow.org}{TensorFlow} is an open source software library 9 | #' for numerical computation using data flow graphs. Nodes in the graph 10 | #' represent mathematical operations, while the graph edges represent the 11 | #' multidimensional data arrays (tensors) communicated between them. The 12 | #' flexible architecture allows you to deploy computation to one or more CPUs or 13 | #' GPUs in a desktop, server, or mobile device with a single API. 14 | #' 15 | #' The \href{https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/all_symbols}{TensorFlow 16 | #' API} is composed of a set of Python modules that enable constructing and 17 | #' executing TensorFlow graphs. The tensorflow package provides access to the 18 | #' complete TensorFlow API from within R. 19 | #' 20 | #' For additional documentation on the tensorflow package see 21 | #' \href{https://tensorflow.rstudio.com}{https://tensorflow.rstudio.com} 22 | #' 23 | #' @docType package 24 | #' @name tfestimators 25 | NULL 26 | 27 | estimator_lib <- NULL 28 | feature_column_lib <- NULL 29 | canned_estimator_lib <- NULL 30 | 31 | np <- NULL 32 | 33 | .globals <- new.env(parent = emptyenv()) 34 | .globals$active_column_names <- NULL 35 | .globals$history <- NULL 36 | 37 | .onLoad <- function(libname, pkgname) { 38 | 39 | # delay load handler 40 | displayed_warning <- FALSE 41 | delay_load <- list( 42 | 43 | priority = 5, 44 | 45 | environment = "r-tensorflow", 46 | 47 | on_load = function() { 48 | check_tensorflow_version(displayed_warning) 49 | }, 50 | 51 | on_error = function(e) { 52 | stop(tf_config()$error_message, call. = FALSE) 53 | } 54 | ) 55 | 56 | 57 | # core modules 58 | if (package_version(Sys.getenv("TENSORFLOW_VERSION", "1.15")) <= "1.12") { 59 | estimator_lib <<- import("tensorflow.python.estimator.estimator", delay_load = delay_load) 60 | feature_column_lib <<- import("tensorflow.python.feature_column.feature_column", delay_load = delay_load) 61 | canned_estimator_lib <<- import("tensorflow.python.estimator.canned", delay_load = delay_load) 62 | } else { 63 | estimator_lib <<- import("tensorflow_estimator.python.estimator.estimator", delay_load = delay_load) 64 | feature_column_lib <<- import("tensorflow.python.feature_column.feature_column_v2", delay_load = delay_load) 65 | canned_estimator_lib <<- import("tensorflow_estimator.python.estimator.canned", delay_load = delay_load) 66 | } 67 | 68 | 69 | # other modules 70 | np <<- import("numpy", convert = FALSE, delay_load = TRUE) 71 | } 72 | 73 | check_tensorflow_version <- function(displayed_warning) { 74 | current_tf_ver <- tf_version() 75 | required_least_ver <- "1.3" 76 | if (current_tf_ver < required_least_ver) { 77 | if (!displayed_warning) { 78 | message("tfestimators requires TensorFlow version >= ", required_least_ver, " ", 79 | "(you are currently running version ", current_tf_ver, ").\n") 80 | displayed_warning <<- TRUE 81 | } 82 | } 83 | } 84 | 85 | .onUnload <- function(libpath) { 86 | 87 | } 88 | 89 | .onAttach <- function(libname, pkgname) { 90 | msg <- "tfestimators is not recomended for new code. It is only compatible with Tensorflow version 1, and is not compatable with Tensorflow version 2." 91 | packageStartupMessage(msg) 92 | } 93 | 94 | .onDetach <- function(libpath) { 95 | 96 | } 97 | -------------------------------------------------------------------------------- /R/pad.R: -------------------------------------------------------------------------------- 1 | pad <- function(object, n) { 2 | UseMethod("pad") 3 | } 4 | 5 | #' @export 6 | pad.default <- function(object, n) { 7 | if (n - length(object) <= 0) 8 | return(object) 9 | 10 | c(object, rep.int(NA, n - length(object))) 11 | } 12 | 13 | #' @export 14 | pad.data.frame <- function(object, n) { 15 | 16 | # compute padding (bail if nothing to do) 17 | padding <- n - nrow(object) 18 | if (padding <= 0) 19 | return(object) 20 | 21 | # reset row names to new length 22 | nrow <- nrow(object) 23 | attr(object, "row.names") <- c(NA_integer_, -as.integer(n)) 24 | 25 | # extend vectors in object 26 | for (i in seq_along(object)) 27 | object[[i]] <- c(object[[i]], rep(NA, times = padding)) 28 | 29 | # and we're done! 30 | object 31 | } 32 | -------------------------------------------------------------------------------- /R/preconditions.R: -------------------------------------------------------------------------------- 1 | ensure_valid_column_names <- function(x, columns) { 2 | existed_cols <- object_names(x) 3 | invalid_columns <- !(columns %in% existed_cols) 4 | if (any(invalid_columns)) { 5 | stop(paste0("The following columns are not in the dataset: ", 6 | paste(columns[invalid_columns], collapse = ","))) 7 | } 8 | } 9 | 10 | object_names <- function(x) { 11 | 12 | # return character vectors as-is 13 | if (is.character(x)) 14 | return(x) 15 | 16 | # if the object has column names, use those 17 | if (!is.null(colnames(x))) 18 | return(colnames(x)) 19 | 20 | # otherwise, default to names 21 | names(x) 22 | } 23 | 24 | ensure_not_na <- function(object) { 25 | if (any(is.na(object))) { 26 | stopf( 27 | "'%s' %s", 28 | deparse(substitute(object)), 29 | if (length(object) > 1) "contains NA values" else "is NA" 30 | ) 31 | } 32 | 33 | object 34 | } 35 | 36 | ensure_not_null <- function(object) { 37 | object %||% stop(sprintf("'%s' is NULL", deparse(substitute(object)))) 38 | } 39 | 40 | ensure_dict <- function(x, named = FALSE) { 41 | if (is.list(x)) { 42 | if (named && is.null(names(x))) { 43 | stop("x must be a named list") 44 | } 45 | dict(x) 46 | } else if (inherits(x, "python.builtin.dict")) { 47 | x 48 | } else { 49 | stop("x needs to be a list or dict") 50 | } 51 | } 52 | 53 | ensure_nullable_list <- function(x) { 54 | result <- if (!is.null(x) && !is.list(x)) 55 | list(x) 56 | else 57 | x 58 | unname(result) 59 | } 60 | 61 | require_file_exists <- function(path, fmt = NULL) { 62 | fmt <- fmt %||% "no file at path '%s'" 63 | if (!file.exists(path)) stopf(fmt, path) 64 | path 65 | } 66 | 67 | require_directory_exists <- function(path, fmt = NULL) { 68 | fmt <- fmt %||% "no file at path '%s'" 69 | require_file_exists(path) 70 | info <- file.info(path) 71 | if (!isTRUE(info$isdir)) stopf(fmt, path) 72 | path 73 | } 74 | 75 | ensure_directory <- function(path) { 76 | if (file.exists(path)) { 77 | info <- file.info(path) 78 | if (isTRUE(info$isdir)) return(path) 79 | stopf("path '%s' exists but is not a directory", path) 80 | } 81 | 82 | if (!dir.create(path, recursive = TRUE)) 83 | stopf("failed to create directory at path '%s'", path) 84 | 85 | invisible(path) 86 | } 87 | -------------------------------------------------------------------------------- /R/print_methods.R: -------------------------------------------------------------------------------- 1 | tf_estimator_type <- function(estimator) { 2 | if (inherits(estimator, "tf_estimator_regressor")) 3 | "regressor" 4 | else if (inherits(estimator, "tf_estimator_classifier")) 5 | "classifier" 6 | else 7 | "estimator" 8 | } 9 | 10 | #' @export 11 | print.tf_estimator <- function(x, ...) { 12 | 13 | if (is.null(x$estimator) || py_is_null_xptr(x$estimator)) 14 | return(cat("\n")) 15 | 16 | header <- sprintf( 17 | "A TensorFlow %s [%s]", 18 | tf_estimator_type(x), 19 | as.character(x$estimator) 20 | ) 21 | 22 | model_dir <- x$estimator$model_dir 23 | 24 | fields <- list( 25 | "Model Directory" = model_dir 26 | ) 27 | 28 | body <- enumerate(fields, function(key, val) { 29 | sprintf("%s: %s", key, val) 30 | }) 31 | 32 | # Model checkpoint only exists when it's been trained 33 | if (!is.null(latest_checkpoint(model_dir))) { 34 | global_step <- variable_value(x)[[graph_keys()$GLOBAL_STEP]] 35 | model_trained_info <- sprintf( 36 | "Model has been trained for %i %s.", 37 | as.integer(global_step), 38 | if (global_step > 1) "steps" else "step" 39 | ) 40 | } else { 41 | model_trained_info <- sprintf("Model has not yet been trained.") 42 | } 43 | 44 | output <- paste( 45 | header, 46 | body, 47 | model_trained_info, 48 | sep = "\n", 49 | collapse = "\n" 50 | ) 51 | 52 | cat(output, sep = "\n") 53 | } 54 | 55 | #' @export 56 | str.tf_estimator <- function(object, ...) { 57 | paste0(capture.output(print(object)), collapse = "\n") 58 | } 59 | 60 | -------------------------------------------------------------------------------- /R/reexport-tidyselect.R: -------------------------------------------------------------------------------- 1 | # Alias required for help links in downstream packages 2 | #' @aliases select_helpers 3 | #' @importFrom tidyselect contains 4 | #' @export 5 | tidyselect::contains 6 | #' @importFrom tidyselect ends_with 7 | #' @export 8 | tidyselect::ends_with 9 | #' @importFrom tidyselect everything 10 | #' @export 11 | tidyselect::everything 12 | #' @importFrom tidyselect matches 13 | #' @export 14 | tidyselect::matches 15 | #' @importFrom tidyselect num_range 16 | #' @export 17 | tidyselect::num_range 18 | #' @importFrom tidyselect one_of 19 | #' @export 20 | tidyselect::one_of 21 | #' @importFrom tidyselect starts_with 22 | #' @export 23 | tidyselect::starts_with 24 | #' @importFrom tidyselect last_col 25 | #' @export 26 | tidyselect::last_col 27 | -------------------------------------------------------------------------------- /R/reexports.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | magrittr::`%>%` 3 | 4 | #' @export 5 | reticulate::use_python 6 | 7 | #' @export 8 | reticulate::use_virtualenv 9 | 10 | #' @export 11 | reticulate::use_condaenv 12 | 13 | #' @export 14 | reticulate::array_reshape 15 | 16 | #' @export 17 | tensorflow::tf 18 | 19 | #' @export 20 | tensorflow::shape 21 | 22 | #' @export 23 | tensorflow::install_tensorflow 24 | 25 | #' @export 26 | tensorflow::tf_config 27 | 28 | #' @export 29 | tensorflow::tensorboard 30 | 31 | #' @export 32 | tensorflow::evaluate 33 | 34 | #' @export 35 | tensorflow::train 36 | 37 | #' @export 38 | tensorflow::train_and_evaluate 39 | 40 | #' @export 41 | tensorflow::export_savedmodel 42 | 43 | #' @importFrom tfruns flags 44 | #' @export 45 | tfruns::flags 46 | 47 | #' @importFrom tfruns flag_numeric 48 | #' @export 49 | tfruns::flag_numeric 50 | 51 | #' @importFrom tfruns flag_integer 52 | #' @export 53 | tfruns::flag_integer 54 | 55 | #' @importFrom tfruns flag_string 56 | #' @export 57 | tfruns::flag_string 58 | 59 | #' @importFrom tfruns flag_boolean 60 | #' @export 61 | tfruns::flag_boolean 62 | 63 | #' @importFrom tfruns run_dir 64 | #' @export 65 | tfruns::run_dir 66 | 67 | #' @importFrom tibble glimpse 68 | #' @export 69 | tibble::glimpse 70 | 71 | -------------------------------------------------------------------------------- /R/run_config.R: -------------------------------------------------------------------------------- 1 | #' Run Configuration 2 | #' 3 | #' This class specifies the configurations for an `Estimator` run. 4 | #' 5 | #' @examples \dontrun{ 6 | #' config <- run_config() 7 | #' 8 | #' # Get the properties of the config 9 | #' names(config) 10 | #' 11 | #' # Change the mutable properties of the config 12 | #' config <- config$replace(tf_random_seed = 11L, save_summary_steps = 12L) 13 | #' 14 | #' # Print config as key value pairs 15 | #' print(config) 16 | #' } 17 | #' 18 | #' @family run_config methods 19 | #' 20 | #' @export 21 | run_config <- function() { 22 | estimator_lib$run_config$RunConfig() 23 | } 24 | 25 | #' @export 26 | print.tensorflow.python.estimator.run_config.RunConfig <- function(x, ...) { 27 | config <- x 28 | 29 | config_names <- names(config) 30 | config_items <- unlist(lapply(config_names, function(item) { 31 | if (is.null(config[[item]])) { 32 | item_value <- "NULL" 33 | } else if (config[[item]] == "") { 34 | item_value <- '""' 35 | } else { 36 | item_value <- config[[item]] 37 | } 38 | paste(item, 39 | item_value, 40 | collapse = "", sep = " = ") 41 | })) 42 | cat(paste(config_items, collapse = ", ")) 43 | } 44 | 45 | 46 | #' Task Types 47 | #' 48 | #' This constant class gives the constant strings for available task types 49 | #' used in `run_config`. 50 | #' 51 | #' @examples \dontrun{ 52 | #' task_type()$MASTER 53 | #' } 54 | #' 55 | #' @export 56 | #' @family run_config methods 57 | task_type <- function() { 58 | estimator_lib$run_config$TaskType 59 | } 60 | -------------------------------------------------------------------------------- /R/run_context.R: -------------------------------------------------------------------------------- 1 | # Tools for introspecting the run context of a TensorFlow session. 2 | run_context_losses <- function(context) { 3 | context$session$graph$get_collection("losses") 4 | } 5 | 6 | run_context_global_step <- function(context) { 7 | context$session$graph$get_collection("global_step") 8 | } 9 | -------------------------------------------------------------------------------- /R/simplify_fn.R: -------------------------------------------------------------------------------- 1 | as_tf_prediction <- function(x) { 2 | structure(x, class = c("tf_prediction", class(x))) 3 | } 4 | 5 | #' @export 6 | type_sum.tf_prediction <- function(x) { 7 | if (is.numeric(x)) 8 | paste0(signif(x, digits = 3), collapse = ", ") 9 | else 10 | paste0(x) 11 | } 12 | 13 | simple_simplify_predictions_fn <- function(results) { 14 | results %>% 15 | purrr::transpose() %>% 16 | purrr::map(~ purrr::map(.x, as_tf_prediction)) %>% 17 | as_tibble() 18 | } 19 | 20 | simple_simplify_evaluations_fn <- function(results) { 21 | results %>% 22 | rlang::flatten() %>% 23 | tibble::as_tibble() 24 | } 25 | 26 | simplify_results <- function(results, simplify) { 27 | if (simplify) { 28 | mode <- resolve_mode() 29 | switch(mode, 30 | infer = simple_simplify_predictions_fn, 31 | eval = simple_simplify_evaluations_fn, 32 | identity)(results) 33 | } else results 34 | } 35 | -------------------------------------------------------------------------------- /R/tidyselect.R: -------------------------------------------------------------------------------- 1 | #' Establish a Feature Columns Selection Scope 2 | #' 3 | #' This helper function provides a set of names to be 4 | #' used by `tidyselect` helpers in e.g. [feature_columns()]. 5 | #' 6 | #' @param columns Either a named \R object (whose names will be 7 | #' used to provide a selection context), or a character vector 8 | #' of such names. 9 | #' 10 | #' @param expr An \R expression, to be evaluated with the selection 11 | #' context active. 12 | #' 13 | #' @name column-scope 14 | NULL 15 | 16 | #' @rdname column-scope 17 | #' @export 18 | set_columns <- function(columns) { 19 | tidyselect::poke_vars(object_names(columns)) 20 | } 21 | 22 | #' @rdname column-scope 23 | #' @export 24 | with_columns <- function(columns, expr) { 25 | tidyselect::with_vars(object_names(columns), expr) 26 | } 27 | 28 | #' @rdname column-scope 29 | #' @export 30 | scoped_columns <- function(columns) { 31 | tidyselect::scoped_vars(object_names(columns), parent.frame()) 32 | } 33 | -------------------------------------------------------------------------------- /R/utils_miscs.R: -------------------------------------------------------------------------------- 1 | "%||%" <- function(x, y) if (is.null(x)) y else x 2 | 3 | stopf <- function(fmt, ..., call. = TRUE, domain = NULL) { 4 | stop(simpleError( 5 | sprintf(fmt, ...), 6 | if (call.) sys.call(sys.parent()) 7 | )) 8 | } 9 | 10 | warnf <- function(fmt, ..., call. = TRUE, immediate. = FALSE) { 11 | warning(sprintf(fmt, ...), call. = call., immediate. = immediate.) 12 | } 13 | 14 | enumerate <- function(object, f, ...) { 15 | nm <- names(object) 16 | result <- lapply(seq_along(object), function(i) { 17 | f(nm[[i]], object[[i]], ...) 18 | }) 19 | names(result) <- names(object) 20 | result 21 | } 22 | 23 | # make sure an object is a function 24 | resolve_fn <- function(object) { 25 | if (is.function(object)) 26 | object() 27 | else 28 | object 29 | } 30 | 31 | 32 | # utility function for importing python modules defined in the 33 | # inst/python directory of the package 34 | import_package_module <- function(module, convert = TRUE) { 35 | python_path <- system.file("python", package = "tfestimators") 36 | import_from_path(module, python_path, convert = convert) 37 | } 38 | 39 | resolve_mode <- function() { 40 | calls <- sys.calls() 41 | 42 | mode <- list( 43 | infer = ~ identical(.x[[1]], quote(predict.tf_estimator)) || 44 | identical(.x[[1]], quote(object$estimator$predict)), 45 | eval = ~ identical(.x[[1]], quote(evaluate.tf_estimator)) || 46 | identical(.x[[1]], quote(object$estimator$evaluate)) 47 | ) %>% 48 | purrr::map(~ purrr::detect(calls, .x)) %>% 49 | purrr::compact() %>% 50 | names() 51 | 52 | if (!length(mode)) 53 | stop("no train() or evaluate() function detected in call stack") 54 | if (length(mode) > 1) 55 | stop("both train() and evaluate() detected in call stack") 56 | 57 | mode 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## tfestimators - R Interface to TensorFlow Estimator API 2 | 3 | [![Travis-CI Build Status](https://travis-ci.org/rstudio/tfestimators.svg?branch=main)](https://travis-ci.org/rstudio/tfestimators) 4 | [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/tfestimators)](https://cran.r-project.org/package=tfestimators) 5 | 6 | 7 | 8 | The **tfestimators** package is an R interface to TensorFlow Estimators, a high-level API that provides: 9 | 10 | - Implementations of many different model types 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. 11 | 12 | - A flexible framework for defining arbitrary new model types as custom estimators. 13 | 14 | - Standalone deployment of models (no R runtime required) in a wide variety of environments. 15 | 16 | - An Experiment API that provides distributed training and hyperparameter tuning for both canned and custom estimators. 17 | 18 | For documentation on using tfestimators, see the package website at 19 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | Added 'rmarkdown' to Suggests as requested by CRAN 2 | 3 | ## R CMD check results 4 | 1 NOTE: New Maintainer 5 | -------------------------------------------------------------------------------- /docs/articles/examples/.gitignore: -------------------------------------------------------------------------------- 1 | data/ 2 | -------------------------------------------------------------------------------- /docs/articles/examples/creating_estimators_tutorial.R: -------------------------------------------------------------------------------- 1 | library(tfestimators) 2 | 3 | maybe_download_abalone <- function(train_data_path, test_data_path, predict_data_path, column_names_to_assign) { 4 | if (!file.exists(train_data_path) || !file.exists(test_data_path) || !file.exists(predict_data_path)) { 5 | cat("Downloading abalone data ...") 6 | train_data <- read.csv("http://download.tensorflow.org/data/abalone_train.csv", header = FALSE) 7 | test_data <- read.csv("http://download.tensorflow.org/data/abalone_test.csv", header = FALSE) 8 | predict_data <- read.csv("http://download.tensorflow.org/data/abalone_predict.csv", header = FALSE) 9 | colnames(train_data) <- column_names_to_assign 10 | colnames(test_data) <- column_names_to_assign 11 | colnames(predict_data) <- column_names_to_assign 12 | write.csv(train_data, train_data_path, row.names = FALSE) 13 | write.csv(test_data, test_data_path, row.names = FALSE) 14 | write.csv(predict_data, predict_data_path, row.names = FALSE) 15 | } else { 16 | train_data <- read.csv(train_data_path, header = TRUE) 17 | test_data <- read.csv(test_data_path, header = TRUE) 18 | predict_data <- read.csv(predict_data_path, header = TRUE) 19 | } 20 | return(list(train_data = train_data, test_data = test_data, predict_data = predict_data)) 21 | } 22 | 23 | COLNAMES <- c("length", "diameter", "height", "whole_weight", "shucked_weight", "viscera_weight", "shell_weight", "num_rings") 24 | 25 | downloaded_data <- maybe_download_abalone( 26 | file.path(getwd(), "train_abalone.csv"), 27 | file.path(getwd(), "test_abalone.csv"), 28 | file.path(getwd(), "predict_abalone.csv"), 29 | COLNAMES 30 | ) 31 | train_data <- downloaded_data$train_data 32 | test_data <- downloaded_data$test_data 33 | predict_data <- downloaded_data$predict_data 34 | 35 | constructed_input_fn <- function(dataset) { 36 | input_fn( 37 | dataset, 38 | features = -num_rings, 39 | response = num_rings, 40 | num_epochs = NULL, 41 | shuffle = TRUE 42 | ) 43 | } 44 | 45 | train_input_fn <- constructed_input_fn(train_data) 46 | test_input_fn <- constructed_input_fn(test_data) 47 | predict_input_fn <- constructed_input_fn(predict_data) 48 | 49 | diameter <- column_numeric("diameter") 50 | height <- column_numeric("height") 51 | 52 | model <- dnn_linear_combined_classifier( 53 | linear_feature_columns = feature_columns(diameter), 54 | dnn_feature_columns = feature_columns(height), 55 | dnn_hidden_units = c(100L, 50L) 56 | ) 57 | 58 | model_fn <- function(features, labels, mode, params, config) { 59 | # Connect the first hidden layer to input layer 60 | first_hidden_layer <- tf$layers$dense(features, 10L, activation = tf$nn$relu) 61 | 62 | # Connect the second hidden layer to first hidden layer with relu 63 | second_hidden_layer <- tf$layers$dense(first_hidden_layer, 10L, activation = tf$nn$relu) 64 | 65 | # Connect the output layer to second hidden layer (no activation fn) 66 | output_layer <- tf$layers$dense(second_hidden_layer, 1L) 67 | 68 | # Reshape output layer to 1-dim Tensor to return predictions 69 | # TODO: This failed if it's c(-1L) - check in reticulate for single element vector conversion 70 | predictions <- tf$reshape(output_layer, list(-1L)) 71 | predictions_list <- list(ages = predictions) 72 | 73 | # Calculate loss using mean squared error 74 | loss <- tf$losses$mean_squared_error(labels, predictions) 75 | 76 | eval_metric_ops <- list(rmse = tf$metrics$root_mean_squared_error( 77 | tf$cast(labels, tf$float64), predictions 78 | )) 79 | 80 | optimizer <- tf$train$GradientDescentOptimizer(learning_rate = params$learning_rate) 81 | train_op <- optimizer$minimize(loss = loss, global_step = tf$train$get_global_step()) 82 | 83 | return(estimator_spec( 84 | mode = mode, 85 | predictions = predictions_list, 86 | loss = loss, 87 | train_op = train_op, 88 | eval_metric_ops = eval_metric_ops 89 | )) 90 | } 91 | 92 | # Set model params 93 | model_params <- list(learning_rate = 0.001) 94 | 95 | # Instantiate Estimator 96 | model <- estimator(model_fn, params = model_params) 97 | 98 | train(model, input_fn = train_input_fn, steps = 2) 99 | 100 | evaluate(model, input_fn = test_input_fn, steps = 2) 101 | -------------------------------------------------------------------------------- /docs/articles/examples/iris_custom_decay_dnn.R: -------------------------------------------------------------------------------- 1 | library(tfestimators) 2 | 3 | # Construct the input 4 | inputs <- input_fn( 5 | iris, 6 | response = "Species", 7 | features = c( 8 | "Sepal.Length", 9 | "Sepal.Width", 10 | "Petal.Length", 11 | "Petal.Width"), 12 | batch_size = 10 13 | ) 14 | 15 | custom_model_fn <- function(features, labels, mode, params, config) { 16 | 17 | # Create three fully connected layers respectively of size 10, 20, and 10 with 18 | # each layer having a dropout probability of 0.1. 19 | logits <- features %>% 20 | tf$contrib$layers$stack( 21 | tf$contrib$layers$fully_connected, c(10L, 20L, 10L), 22 | normalizer_fn = tf$contrib$layers$dropout, 23 | normalizer_params = list(keep_prob = 0.9)) %>% 24 | tf$contrib$layers$fully_connected(3L, activation_fn = NULL) # Compute logits (1 per class) and compute loss. 25 | 26 | # Compute predictions. 27 | predicted_classes <- tf$argmax(logits, 1L) 28 | if (mode == "infer") { 29 | predictions <- list( 30 | class = predicted_classes, 31 | prob = tf$nn$softmax(logits)) 32 | return(estimator_spec(mode = mode, predictions = predictions)) 33 | } 34 | 35 | # Convert the labels to a one-hot tensor of shape (length of features, 3) and 36 | # with a on-value of 1 for each one-hot vector of length 3. 37 | onehot_labels <- tf$one_hot(labels, 3L, 1L, 0L) 38 | # Compute loss. 39 | loss <- tf$losses$softmax_cross_entropy(onehot_labels, logits) 40 | 41 | if (mode == "train") { 42 | global_step <- tf$train$get_global_step() 43 | learning_rate <- tf$train$exponential_decay( 44 | learning_rate = 0.1, 45 | global_step = global_step, 46 | decay_steps = 100L, 47 | decay_rate = 0.001) 48 | optimizer <- tf$train$AdagradOptimizer(learning_rate = learning_rate) 49 | train_op <- optimizer$minimize(loss, global_step = global_step) 50 | return(estimator_spec(mode = mode, loss = loss, train_op = train_op)) 51 | } 52 | 53 | # Compute evaluation metrics. 54 | eval_metric_ops <- list( 55 | accuracy = tf$metrics$accuracy( 56 | labels = labels, predictions = predicted_classes 57 | )) 58 | 59 | return(estimator_spec(mode = mode, loss = loss, eval_metric_ops = eval_metric_ops)) 60 | } 61 | 62 | model_dir <- "/tmp/iris-custom-decay-cnn-model" 63 | 64 | # Intialize classifer and training 65 | classifier <- estimator( 66 | model_fn = custom_model_fn, model_dir = model_dir) 67 | 68 | classifier %>% train(input_fn = inputs, steps = 100) 69 | 70 | # Genearate predictions 71 | predictions <- predict(classifier, input_fn = inputs) 72 | 73 | -------------------------------------------------------------------------------- /docs/articles/examples/iris_dnn_classifier.R: -------------------------------------------------------------------------------- 1 | library(tfestimators) 2 | 3 | response <- function() "Species" 4 | features <- function() setdiff(names(iris), response()) 5 | 6 | # split into train, test datasets 7 | set.seed(123) 8 | partitions <- modelr::resample_partition(iris, c(test = 0.2, train = 0.8)) 9 | iris_train <- as.data.frame(partitions$train) 10 | iris_test <- as.data.frame(partitions$test) 11 | 12 | # construct feature columns 13 | feature_columns <- feature_columns( 14 | column_numeric(features()) 15 | ) 16 | 17 | # construct classifier 18 | classifier <- dnn_classifier( 19 | feature_columns = feature_columns, 20 | hidden_units = c(10, 20, 10), 21 | n_classes = 3 22 | ) 23 | 24 | # construct input function 25 | iris_input_fn <- function(data) { 26 | input_fn(data, features = features(), response = response()) 27 | } 28 | 29 | # train classifier with training dataset 30 | train(classifier, input_fn = iris_input_fn(iris_train)) 31 | 32 | # valuate with test dataset 33 | predictions <- predict(classifier, input_fn = iris_input_fn(iris_test)) 34 | evaluation <- evaluate(classifier, input_fn = iris_input_fn(iris_test)) 35 | -------------------------------------------------------------------------------- /docs/articles/images/estimator-apis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/estimator-apis.png -------------------------------------------------------------------------------- /docs/articles/images/estimator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/estimator.png -------------------------------------------------------------------------------- /docs/articles/images/experiment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/experiment.png -------------------------------------------------------------------------------- /docs/articles/images/tensorboard-graph-details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/tensorboard-graph-details.png -------------------------------------------------------------------------------- /docs/articles/images/tensorboard-graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/tensorboard-graph.png -------------------------------------------------------------------------------- /docs/articles/images/tensorboard-loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/tensorboard-loss.png -------------------------------------------------------------------------------- /docs/articles/images/tensorflow-2-100709406-large.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/tensorflow-2-100709406-large.jpg -------------------------------------------------------------------------------- /docs/articles/images/tensorflow-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/articles/images/tensorflow-architecture.png -------------------------------------------------------------------------------- /docs/extra.css: -------------------------------------------------------------------------------- 1 | 2 | h4.date, 3 | h4.author { 4 | display: none; 5 | } 6 | 7 | h2.hasAnchor { 8 | font-weight: 350; 9 | } 10 | 11 | .ref-index tbody { 12 | margin-bottom: 60px; 13 | } 14 | 15 | pre:not([class]) { 16 | background-color: white; 17 | } 18 | 19 | .contents a { 20 | text-decoration: none; 21 | } 22 | 23 | blockquote { 24 | font-size: inherit; 25 | } 26 | 27 | .examples .page-header { 28 | border-bottom: none; 29 | margin: 0; 30 | padding-bottom: 0; 31 | } 32 | 33 | .examples .sourceCode { 34 | margin-top: 25px; 35 | } 36 | 37 | #sidebar .nav>li>a { 38 | padding-top: 1px; 39 | padding-bottom: 2px; 40 | } 41 | 42 | #installation .sourceCode { 43 | font-size: 13px; 44 | } 45 | 46 | .r-plot { 47 | margin-bottom: 25px; 48 | } 49 | 50 | .screenshot { 51 | margin-bottom: 20px; 52 | } 53 | 54 | .source-ref { 55 | margin-bottom: 20px; 56 | } 57 | 58 | .source-ref .caption { 59 | display: none; 60 | } 61 | 62 | 63 | 64 | .alert-warning { 65 | color: #8a6d3b; 66 | background-color: #fcf8e3; 67 | padding: 15px; 68 | margin-top: 20px; 69 | margin-bottom: 20px; 70 | background-image: linear-gradient(to bottom,#fcf8e3 0,#f8efc0 100%); 71 | filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffcf8e3', endColorstr='#fff8efc0', GradientType=0); 72 | background-repeat: repeat-x; 73 | border: 1px solid #f5e79e; 74 | border-radius: 4px; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /docs/extra.js: -------------------------------------------------------------------------------- 1 | 2 | $(document).ready(function() { 3 | 4 | // turn functions section into ref-table 5 | $('#functions').find('table').attr('class', 'ref-index'); 6 | 7 | // are we in examples? 8 | var examples = window.location.href.match("/articles/examples/") !== null; 9 | if (examples) { 10 | $('.template-vignette').addClass('examples'); 11 | 12 | // remove right column 13 | $(".col-md-9").removeClass("col-md-9").addClass('col-md-10'); 14 | $(".col-md-3").remove(); 15 | 16 | } 17 | }); 18 | -------------------------------------------------------------------------------- /docs/images/.gitigore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/docs/images/.gitigore -------------------------------------------------------------------------------- /docs/jquery.sticky-kit.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | http://leafo.net 3 | */ 4 | (function(){var b,f;b=this.jQuery||window.jQuery;f=b(window);b.fn.stick_in_parent=function(d){var A,w,J,n,B,K,p,q,k,E,t;null==d&&(d={});t=d.sticky_class;B=d.inner_scrolling;E=d.recalc_every;k=d.parent;q=d.offset_top;p=d.spacer;w=d.bottoming;null==q&&(q=0);null==k&&(k=void 0);null==B&&(B=!0);null==t&&(t="is_stuck");A=b(document);null==w&&(w=!0);J=function(a,d,n,C,F,u,r,G){var v,H,m,D,I,c,g,x,y,z,h,l;if(!a.data("sticky_kit")){a.data("sticky_kit",!0);I=A.height();g=a.parent();null!=k&&(g=g.closest(k)); 5 | if(!g.length)throw"failed to find stick parent";v=m=!1;(h=null!=p?p&&a.closest(p):b("
"))&&h.css("position",a.css("position"));x=function(){var c,f,e;if(!G&&(I=A.height(),c=parseInt(g.css("border-top-width"),10),f=parseInt(g.css("padding-top"),10),d=parseInt(g.css("padding-bottom"),10),n=g.offset().top+c+f,C=g.height(),m&&(v=m=!1,null==p&&(a.insertAfter(h),h.detach()),a.css({position:"",top:"",width:"",bottom:""}).removeClass(t),e=!0),F=a.offset().top-(parseInt(a.css("margin-top"),10)||0)-q, 6 | u=a.outerHeight(!0),r=a.css("float"),h&&h.css({width:a.outerWidth(!0),height:u,display:a.css("display"),"vertical-align":a.css("vertical-align"),"float":r}),e))return l()};x();if(u!==C)return D=void 0,c=q,z=E,l=function(){var b,l,e,k;if(!G&&(e=!1,null!=z&&(--z,0>=z&&(z=E,x(),e=!0)),e||A.height()===I||x(),e=f.scrollTop(),null!=D&&(l=e-D),D=e,m?(w&&(k=e+u+c>C+n,v&&!k&&(v=!1,a.css({position:"fixed",bottom:"",top:c}).trigger("sticky_kit:unbottom"))),eb&&!v&&(c-=l,c=Math.max(b-u,c),c=Math.min(q,c),m&&a.css({top:c+"px"})))):e>F&&(m=!0,b={position:"fixed",top:c},b.width="border-box"===a.css("box-sizing")?a.outerWidth()+"px":a.width()+"px",a.css(b).addClass(t),null==p&&(a.after(h),"left"!==r&&"right"!==r||h.append(a)),a.trigger("sticky_kit:stick")),m&&w&&(null==k&&(k=e+u+c>C+n),!v&&k)))return v=!0,"static"===g.css("position")&&g.css({position:"relative"}), 8 | a.css({position:"absolute",bottom:d,top:"auto"}).trigger("sticky_kit:bottom")},y=function(){x();return l()},H=function(){G=!0;f.off("touchmove",l);f.off("scroll",l);f.off("resize",y);b(document.body).off("sticky_kit:recalc",y);a.off("sticky_kit:detach",H);a.removeData("sticky_kit");a.css({position:"",bottom:"",top:"",width:""});g.position("position","");if(m)return null==p&&("left"!==r&&"right"!==r||a.insertAfter(h),h.remove()),a.removeClass(t)},f.on("touchmove",l),f.on("scroll",l),f.on("resize", 9 | y),b(document.body).on("sticky_kit:recalc",y),a.on("sticky_kit:detach",H),setTimeout(l,0)}};n=0;for(K=this.length;n 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/pkgdown.css: -------------------------------------------------------------------------------- 1 | /* Sticker footer */ 2 | body > .container { 3 | display: flex; 4 | padding-top: 60px; 5 | min-height: calc(100vh); 6 | flex-direction: column; 7 | } 8 | 9 | body > .container .row { 10 | flex: 1; 11 | } 12 | 13 | footer { 14 | margin-top: 45px; 15 | padding: 35px 0 36px; 16 | border-top: 1px solid #e5e5e5; 17 | color: #666; 18 | display: flex; 19 | } 20 | footer p { 21 | margin-bottom: 0; 22 | } 23 | footer div { 24 | flex: 1; 25 | } 26 | footer .pkgdown { 27 | text-align: right; 28 | } 29 | footer p { 30 | margin-bottom: 0; 31 | } 32 | 33 | img.icon { 34 | float: right; 35 | } 36 | 37 | img { 38 | max-width: 100%; 39 | } 40 | 41 | /* Section anchors ---------------------------------*/ 42 | 43 | a.anchor { 44 | margin-left: -30px; 45 | display:inline-block; 46 | width: 30px; 47 | height: 30px; 48 | visibility: hidden; 49 | 50 | background-image: url(./link.svg); 51 | background-repeat: no-repeat; 52 | background-size: 20px 20px; 53 | background-position: center center; 54 | } 55 | 56 | .hasAnchor:hover a.anchor { 57 | visibility: visible; 58 | } 59 | 60 | @media (max-width: 767px) { 61 | .hasAnchor:hover a.anchor { 62 | visibility: hidden; 63 | } 64 | } 65 | 66 | 67 | /* Fixes for fixed navbar --------------------------*/ 68 | 69 | .contents h1, .contents h2, .contents h3, .contents h4 { 70 | padding-top: 60px; 71 | margin-top: -60px; 72 | } 73 | 74 | /* Static header placement on mobile devices */ 75 | @media (max-width: 767px) { 76 | .navbar-fixed-top { 77 | position: absolute; 78 | } 79 | .navbar { 80 | padding: 0; 81 | } 82 | } 83 | 84 | 85 | /* Sidebar --------------------------*/ 86 | 87 | #sidebar { 88 | margin-top: 30px; 89 | } 90 | #sidebar h2 { 91 | font-size: 1.5em; 92 | margin-top: 1em; 93 | } 94 | 95 | #sidebar h2:first-child { 96 | margin-top: 0; 97 | } 98 | 99 | #sidebar .list-unstyled li { 100 | margin-bottom: 0.5em; 101 | } 102 | 103 | /* Reference index & topics ----------------------------------------------- */ 104 | 105 | .ref-index th {font-weight: normal;} 106 | .ref-index h2 {font-size: 20px;} 107 | 108 | .ref-index td {vertical-align: top;} 109 | .ref-index .alias {width: 40%;} 110 | .ref-index .title {width: 60%;} 111 | 112 | .ref-index .alias {width: 40%;} 113 | .ref-index .title {width: 60%;} 114 | 115 | .ref-arguments th {text-align: right; padding-right: 10px;} 116 | .ref-arguments th, .ref-arguments td {vertical-align: top;} 117 | .ref-arguments .name {width: 20%;} 118 | .ref-arguments .desc {width: 80%;} 119 | 120 | /* Nice scrolling for wide elements --------------------------------------- */ 121 | 122 | table { 123 | display: block; 124 | overflow: auto; 125 | } 126 | 127 | /* Syntax highlighting ---------------------------------------------------- */ 128 | 129 | pre { 130 | word-wrap: normal; 131 | word-break: normal; 132 | border: 1px solid #eee; 133 | } 134 | 135 | pre, code { 136 | background-color: #f8f8f8; 137 | color: #333; 138 | } 139 | 140 | pre .img { 141 | margin: 5px 0; 142 | } 143 | 144 | pre .img img { 145 | background-color: #fff; 146 | display: block; 147 | height: auto; 148 | } 149 | 150 | code a, pre a { 151 | color: #375f84; 152 | } 153 | 154 | .fl {color: #1514b5;} 155 | .fu {color: #000000;} /* function */ 156 | .ch,.st {color: #036a07;} /* string */ 157 | .kw {color: #264D66;} /* keyword */ 158 | .co {color: #888888;} /* comment */ 159 | 160 | .message { color: black; font-weight: bolder;} 161 | .error { color: orange; font-weight: bolder;} 162 | .warning { color: #6A0366; font-weight: bolder;} 163 | 164 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | $("#sidebar").stick_in_parent({offset_top: 40}); 3 | $('body').scrollspy({ 4 | target: '#sidebar', 5 | offset: 60 6 | }); 7 | 8 | var cur_path = paths(location.pathname); 9 | $("#navbar ul li a").each(function(index, value) { 10 | if (value.text == "Home") 11 | return; 12 | if (value.getAttribute("href") === "#") 13 | return; 14 | 15 | var path = paths(value.pathname); 16 | if (is_prefix(cur_path, path)) { 17 | // Add class to parent
  • , and enclosing
  • if in dropdown 18 | var menu_anchor = $(value); 19 | menu_anchor.parent().addClass("active"); 20 | menu_anchor.closest("li.dropdown").addClass("active"); 21 | } 22 | }); 23 | }); 24 | 25 | function paths(pathname) { 26 | var pieces = pathname.split("/"); 27 | pieces.shift(); // always starts with / 28 | 29 | var end = pieces[pieces.length - 1]; 30 | if (end === "index.html" || end === "") 31 | pieces.pop(); 32 | return(pieces); 33 | } 34 | 35 | function is_prefix(needle, haystack) { 36 | if (needle.length > haystack.lengh) 37 | return(false); 38 | 39 | for (var i = 0; i < haystack.length; i++) { 40 | if (needle[i] != haystack[i]) 41 | return(false); 42 | } 43 | 44 | return(true); 45 | } 46 | -------------------------------------------------------------------------------- /images: -------------------------------------------------------------------------------- 1 | docs/images -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | citHeader("To cite tfestimators in publications, please use:") 2 | 3 | bibentry(bibtype = "InProceedings", 4 | author = "Heng-Tze Cheng, Lichan Hong, Mustafa Ispir, Clemens Mewald, Zakaria Haque, Illia Polosukhin, Georgios Roumpos, D Sculley, Jamie Smith, David Soergel, Yuan Tang, Philipp Tucker, Martin Wicke, Cassandra Xia, and Jianwei Xie", 5 | title = "TensorFlow Estimators: Managing Simplicity vs. Flexibility in High-Level Machine Learning Frameworks", 6 | booktitle = "Proceedings of the 23rd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining", 7 | year = "2017", 8 | isbn = "978-1-4503-4887-4", 9 | location = "Halifax, NS, Canada", 10 | pages = "1763--1771", 11 | url = "https://doi.acm.org/10.1145/3097983.3098171", 12 | doi = "10.1145/3097983.3098171", 13 | acmid = "3098171", 14 | publisher = "ACM", 15 | address = "New York, NY, USA", 16 | textVersion = 'Heng-Tze Cheng, Lichan Hong, Mustafa Ispir, Clemens Mewald, Zakaria Haque, Illia Polosukhin, Georgios Roumpos, D Sculley, Jamie Smith, David Soergel, Yuan Tang, Philipp Tucker, Martin Wicke, Cassandra Xia, and Jianwei Xie. TensorFlow Estimators: Managing Simplicity vs. Flexibility in High-Level Machine Learning Frameworks. Proceedings of the 23rd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, Halifax, NS, Canada, August 13 - August 17 (2017). ' 17 | ) 18 | 19 | bibentry(bibtype = "Manual", 20 | author = c( 21 | person("Yuan", "Tang", role = c("aut", "cre")), 22 | person("JJ", "Allaire", role = c("aut")), 23 | person("RStudio", role = c("cph", "fnd")), 24 | person("Kevin", "Ushey", role = c("aut")), 25 | person("Kevin", "Kuo", role = c("aut")), 26 | person(family = "Google Inc.", role = c("cph"))), 27 | title = "tfestimators: High-level Estimator Interface to TensorFlow in R", 28 | year = "2018", 29 | url = "https://github.com/rstudio/tfestimators", 30 | textVersion = "Yuan Tang, JJ Allaire, Kevin Ushey, and Kevin Kuo. tfestimators: High-level Estimator Interface to TensorFlow in R. https://github.com/rstudio/tfestimators" 31 | ) 32 | -------------------------------------------------------------------------------- /inst/python/estimatortools/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /inst/python/estimatortools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/inst/python/estimatortools/__init__.py -------------------------------------------------------------------------------- /inst/python/estimatortools/session_run_hooks.py: -------------------------------------------------------------------------------- 1 | from tensorflow.python.training.session_run_hook import SessionRunHook 2 | 3 | class RSessionRunHook(SessionRunHook): 4 | def __init__(self, r_begin, r_after_create_session, r_before_run, r_after_run, r_end): 5 | super(SessionRunHook, self).__init__() 6 | self.r_begin = r_begin 7 | self.r_after_create_session = r_after_create_session 8 | self.r_before_run = r_before_run 9 | self.r_after_run = r_after_run 10 | self.r_end = r_end 11 | 12 | def begin(self): 13 | try: self.r_begin() 14 | except: pass 15 | 16 | def after_create_session(self, session, coord): 17 | """Called when new TensorFlow session is created. 18 | This is called to signal the hooks that a new session has been created. This 19 | has two essential differences with the situation in which `begin` is called: 20 | * When this is called, the graph is finalized and ops can no longer be added 21 | to the graph. 22 | * This method will also be called as a result of recovering a wrapped 23 | session, not only at the beginning of the overall session. 24 | Args: 25 | session: A TensorFlow Session that has been created. 26 | coord: A Coordinator object which keeps track of all threads. 27 | """ 28 | try: self.r_after_create_session(session, coord) 29 | except: pass 30 | 31 | def before_run(self, run_context): 32 | """Called before each call to run(). 33 | You can return from this call a `SessionRunArgs` object indicating ops or 34 | tensors to add to the upcoming `run()` call. These ops/tensors will be run 35 | together with the ops/tensors originally passed to the original run() call. 36 | The run args you return can also contain feeds to be added to the run() 37 | call. 38 | The `run_context` argument is a `SessionRunContext` that provides 39 | information about the upcoming `run()` call: the originally requested 40 | op/tensors, the TensorFlow Session. 41 | At this point graph is finalized and you can not add ops. 42 | Args: 43 | run_context: A `SessionRunContext` object. 44 | Returns: 45 | None or a `SessionRunArgs` object. 46 | """ 47 | try: return self.r_before_run(run_context) 48 | except: pass 49 | 50 | def after_run(self, run_context, run_values): 51 | """Called after each call to run(). 52 | The `run_values` argument contains results of requested ops/tensors by 53 | `before_run()`. 54 | The `run_context` argument is the same one send to `before_run` call. 55 | `run_context.request_stop()` can be called to stop the iteration. 56 | If `session.run()` raises any exceptions then `after_run()` is not called. 57 | Args: 58 | run_context: A `SessionRunContext` object. 59 | run_values: A SessionRunValues object. 60 | """ 61 | try: self.r_after_run(run_context, run_values) 62 | except: pass 63 | 64 | def end(self, session): 65 | """Called at the end of session. 66 | The `session` argument can be used in case the hook wants to run final ops, 67 | such as saving a last checkpoint. 68 | If `session.run()` raises exception other than OutOfRangeError or 69 | StopIteration then `end()` is not called. 70 | Note the difference between `end()` and `after_run()` behavior when 71 | `session.run()` raises OutOfRangeError or StopIteration. In that case 72 | `end()` is called but `after_run()` is not called. 73 | Args: 74 | session: A TensorFlow Session that will be soon closed. 75 | """ 76 | try: self.r_end(session) 77 | except: pass 78 | -------------------------------------------------------------------------------- /man-roxygen/roxlate-activation-fn.R: -------------------------------------------------------------------------------- 1 | #' @param <%= name %> 2 | #' The activation function to apply to each layer. This can either be an 3 | #' actual activation function (e.g. \code{tf$nn$relu}), or the name of an 4 | #' activation function (e.g. \code{"relu"}). Defaults to the 5 | #' \code{"<%= default %>"} activation function. See 6 | #' \url{https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nn} 7 | #' for documentation related to the set of activation functions available 8 | #' in TensorFlow. 9 | -------------------------------------------------------------------------------- /man-roxygen/roxlate-object-estimator.R: -------------------------------------------------------------------------------- 1 | #' @param object A TensorFlow estimator. 2 | -------------------------------------------------------------------------------- /man-roxygen/roxlate-object-experiment.R: -------------------------------------------------------------------------------- 1 | #' @param object A TensorFlow experiment. 2 | -------------------------------------------------------------------------------- /man/boosted_trees_estimators.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boosted_trees_estimators.R 3 | \name{boosted_trees_estimators} 4 | \alias{boosted_trees_estimators} 5 | \alias{boosted_trees_regressor} 6 | \alias{boosted_trees_classifier} 7 | \title{Boosted Trees Estimator} 8 | \usage{ 9 | boosted_trees_regressor( 10 | feature_columns, 11 | n_batches_per_layer, 12 | model_dir = NULL, 13 | label_dimension = 1L, 14 | weight_column = NULL, 15 | n_trees = 100L, 16 | max_depth = 6L, 17 | learning_rate = 0.1, 18 | l1_regularization = 0, 19 | l2_regularization = 0, 20 | tree_complexity = 0, 21 | min_node_weight = 0, 22 | config = NULL 23 | ) 24 | 25 | boosted_trees_classifier( 26 | feature_columns, 27 | n_batches_per_layer, 28 | model_dir = NULL, 29 | n_classes = 2L, 30 | weight_column = NULL, 31 | label_vocabulary = NULL, 32 | n_trees = 100L, 33 | max_depth = 6L, 34 | learning_rate = 0.1, 35 | l1_regularization = 0, 36 | l2_regularization = 0, 37 | tree_complexity = 0, 38 | min_node_weight = 0, 39 | config = NULL 40 | ) 41 | } 42 | \arguments{ 43 | \item{feature_columns}{An \R list containing all of the feature columns used 44 | by the model (typically, generated by \code{\link[=feature_columns]{feature_columns()}}).} 45 | 46 | \item{n_batches_per_layer}{The number of batches to collect 47 | statistics per layer.} 48 | 49 | \item{model_dir}{Directory to save the model parameters, graph, and so on. 50 | This can also be used to load checkpoints from the directory into a 51 | estimator to continue training a previously saved model.} 52 | 53 | \item{label_dimension}{Number of regression targets per example. This is the 54 | size of the last dimension of the labels and logits \code{Tensor} objects 55 | (typically, these have shape \verb{[batch_size, label_dimension]}).} 56 | 57 | \item{weight_column}{A string, or a numeric column created by 58 | \code{\link[=column_numeric]{column_numeric()}} defining feature column representing weights. It is used 59 | to down weight or boost examples during training. It will be multiplied by 60 | the loss of the example. If it is a string, it is used as a key to fetch 61 | weight tensor from the \code{features} argument. If it is a numeric column, 62 | then the raw tensor is fetched by key \code{weight_column$key}, then 63 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 64 | 65 | \item{n_trees}{Number trees to be created.} 66 | 67 | \item{max_depth}{Maximum depth of the tree to grow.} 68 | 69 | \item{learning_rate}{Shrinkage parameter to be used when a tree 70 | added to the model.} 71 | 72 | \item{l1_regularization}{Regularization multiplier applied to the 73 | absolute weights of the tree leafs.} 74 | 75 | \item{l2_regularization}{Regularization multiplier applied to the 76 | square weights of the tree leafs.} 77 | 78 | \item{tree_complexity}{Regularization factor to penalize trees 79 | with more leaves.} 80 | 81 | \item{min_node_weight}{Minimum hessian a node must have for a 82 | split to be considered. The value will be compared with 83 | sum(leaf_hessian)/(batch_size * n_batches_per_layer).} 84 | 85 | \item{config}{A run configuration created by \code{\link[=run_config]{run_config()}}, used to configure the runtime 86 | settings.} 87 | 88 | \item{n_classes}{The number of label classes.} 89 | 90 | \item{label_vocabulary}{A list of strings represents possible label values. 91 | If given, labels must be string type and have any value in 92 | \code{label_vocabulary}. If it is not given, that means labels are already 93 | encoded as integer or float within \verb{[0, 1]} for \code{n_classes == 2} and 94 | encoded as integer values in \verb{\{0, 1,..., n_classes -1\}} for \code{n_classes > 2}. Also there will be errors if vocabulary is not provided and labels are 95 | string.} 96 | } 97 | \description{ 98 | Construct a boosted trees estimator. 99 | } 100 | \seealso{ 101 | Other canned estimators: 102 | \code{\link{dnn_estimators}}, 103 | \code{\link{dnn_linear_combined_estimators}}, 104 | \code{\link{linear_estimators}} 105 | } 106 | \concept{canned estimators} 107 | -------------------------------------------------------------------------------- /man/classifier_parse_example_spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/parsing_utils.R 3 | \name{classifier_parse_example_spec} 4 | \alias{classifier_parse_example_spec} 5 | \title{Generates Parsing Spec for TensorFlow Example to be Used with Classifiers} 6 | \usage{ 7 | classifier_parse_example_spec( 8 | feature_columns, 9 | label_key, 10 | label_dtype = tf$int64, 11 | label_default = NULL, 12 | weight_column = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{feature_columns}{An iterable containing all feature columns. All items 17 | should be instances of classes derived from \verb{_FeatureColumn}.} 18 | 19 | \item{label_key}{A string identifying the label. It means \code{tf$Example} stores 20 | labels with this key.} 21 | 22 | \item{label_dtype}{A \code{tf$dtype} identifies the type of labels. By default it 23 | is \code{tf$int64}. If user defines a \code{label_vocabulary}, this should be set as 24 | \code{tf$string}. \code{tf$float32} labels are only supported for binary 25 | classification.} 26 | 27 | \item{label_default}{used as label if label_key does not exist in given 28 | \code{tf$Example}. An example usage: let's say \code{label_key} is 'clicked' and 29 | \code{tf$Example} contains clicked data only for positive examples in following 30 | format \verb{key:clicked, value:1}. This means that if there is no data with key 31 | 'clicked' it should count as negative example by setting 32 | \code{label_deafault=0}. Type of this value should be compatible with 33 | \code{label_dtype}.} 34 | 35 | \item{weight_column}{A string or a numeric column created by 36 | \code{\link[=column_numeric]{column_numeric()}} defining feature column representing 37 | weights. It is used to down weight or boost examples during training. It 38 | will be multiplied by the loss of the example. If it is a string, it is 39 | used as a key to fetch weight tensor from the \code{features}. If it is a 40 | numeric column, raw tensor is fetched by key \code{weight_column$key}, then 41 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 42 | } 43 | \value{ 44 | A dict mapping each feature key to a \code{FixedLenFeature} or 45 | \code{VarLenFeature} value. 46 | } 47 | \description{ 48 | If users keep data in TensorFlow Example format, they need to call \code{tf$parse_example} 49 | with a proper feature spec. There are two main things that this utility 50 | helps: 51 | \itemize{ 52 | \item Users need to combine parsing spec of features with labels and 53 | weights (if any) since they are all parsed from same \code{tf$Example} instance. 54 | This utility combines these specs. 55 | \item It is difficult to map expected label by 56 | a classifier such as \code{dnn_classifier} to corresponding \code{tf$parse_example} spec. 57 | This utility encodes it by getting related information from users (key, 58 | dtype). 59 | } 60 | } 61 | \section{Raises}{ 62 | 63 | \itemize{ 64 | \item ValueError: If label is used in \code{feature_columns}. 65 | \item ValueError: If weight_column is used in \code{feature_columns}. 66 | \item ValueError: If any of the given \code{feature_columns} is not a feature column instance. 67 | \item ValueError: If \code{weight_column} is not a numeric column instance. 68 | \item ValueError: if label_key is \code{NULL}. 69 | } 70 | } 71 | 72 | \seealso{ 73 | Other parsing utilities: 74 | \code{\link{regressor_parse_example_spec}()} 75 | } 76 | \concept{parsing utilities} 77 | -------------------------------------------------------------------------------- /man/column-scope.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tidyselect.R 3 | \name{column-scope} 4 | \alias{column-scope} 5 | \alias{set_columns} 6 | \alias{with_columns} 7 | \alias{scoped_columns} 8 | \title{Establish a Feature Columns Selection Scope} 9 | \usage{ 10 | set_columns(columns) 11 | 12 | with_columns(columns, expr) 13 | 14 | scoped_columns(columns) 15 | } 16 | \arguments{ 17 | \item{columns}{Either a named \R object (whose names will be 18 | used to provide a selection context), or a character vector 19 | of such names.} 20 | 21 | \item{expr}{An \R expression, to be evaluated with the selection 22 | context active.} 23 | } 24 | \description{ 25 | This helper function provides a set of names to be 26 | used by \code{tidyselect} helpers in e.g. \code{\link[=feature_columns]{feature_columns()}}. 27 | } 28 | -------------------------------------------------------------------------------- /man/column_base.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_base} 4 | \alias{column_base} 5 | \title{Base Documentation for Feature Column Constructors} 6 | \arguments{ 7 | \item{...}{Expression(s) identifying input feature(s). Used as the column 8 | name and the dictionary key for feature parsing configs, feature tensors, 9 | and feature columns.} 10 | } 11 | \description{ 12 | Base Documentation for Feature Column Constructors 13 | } 14 | -------------------------------------------------------------------------------- /man/column_bucketized.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_bucketized} 4 | \alias{column_bucketized} 5 | \title{Construct a Bucketized Column} 6 | \usage{ 7 | column_bucketized(source_column, boundaries) 8 | } 9 | \arguments{ 10 | \item{source_column}{A one-dimensional dense column, as generated by \code{\link[=column_numeric]{column_numeric()}}.} 11 | 12 | \item{boundaries}{A sorted list or list of floats specifying the boundaries.} 13 | } 14 | \value{ 15 | A bucketized column. 16 | } 17 | \description{ 18 | Construct a bucketized column, representing discretized dense input. Buckets 19 | include the left boundary, and exclude the right boundary. 20 | } 21 | \section{Raises}{ 22 | 23 | \itemize{ 24 | \item ValueError: If \code{source_column} is not a numeric column, or if it is not one-dimensional. 25 | \item ValueError: If \code{boundaries} is not a sorted list or list. 26 | } 27 | } 28 | 29 | \seealso{ 30 | Other feature column constructors: 31 | \code{\link{column_categorical_weighted}()}, 32 | \code{\link{column_categorical_with_hash_bucket}()}, 33 | \code{\link{column_categorical_with_identity}()}, 34 | \code{\link{column_categorical_with_vocabulary_file}()}, 35 | \code{\link{column_categorical_with_vocabulary_list}()}, 36 | \code{\link{column_crossed}()}, 37 | \code{\link{column_embedding}()}, 38 | \code{\link{column_numeric}()}, 39 | \code{\link{input_layer}()} 40 | } 41 | \concept{feature column constructors} 42 | -------------------------------------------------------------------------------- /man/column_categorical_weighted.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_categorical_weighted} 4 | \alias{column_categorical_weighted} 5 | \title{Construct a Weighted Categorical Column} 6 | \usage{ 7 | column_categorical_weighted( 8 | categorical_column, 9 | weight_feature_key, 10 | dtype = tf$float32 11 | ) 12 | } 13 | \arguments{ 14 | \item{categorical_column}{A categorical column created by 15 | \verb{column_categorical_*()} functions.} 16 | 17 | \item{weight_feature_key}{String key for weight values.} 18 | 19 | \item{dtype}{Type of weights, such as \code{tf$float32}. Only float and integer 20 | weights are supported.} 21 | } 22 | \value{ 23 | A categorical column composed of two sparse features: one 24 | represents id, the other represents weight (value) of the id feature in 25 | that example. 26 | } 27 | \description{ 28 | Use this when each of your sparse inputs has both an ID and a value. For 29 | example, if you're representing text documents as a collection of word 30 | frequencies, you can provide 2 parallel sparse input features ('terms' and 31 | 'frequencies' below). 32 | } 33 | \section{Raises}{ 34 | 35 | \itemize{ 36 | \item ValueError: if \code{dtype} is not convertible to float. 37 | } 38 | } 39 | 40 | \seealso{ 41 | Other feature column constructors: 42 | \code{\link{column_bucketized}()}, 43 | \code{\link{column_categorical_with_hash_bucket}()}, 44 | \code{\link{column_categorical_with_identity}()}, 45 | \code{\link{column_categorical_with_vocabulary_file}()}, 46 | \code{\link{column_categorical_with_vocabulary_list}()}, 47 | \code{\link{column_crossed}()}, 48 | \code{\link{column_embedding}()}, 49 | \code{\link{column_numeric}()}, 50 | \code{\link{input_layer}()} 51 | } 52 | \concept{feature column constructors} 53 | -------------------------------------------------------------------------------- /man/column_categorical_with_hash_bucket.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_categorical_with_hash_bucket} 4 | \alias{column_categorical_with_hash_bucket} 5 | \title{Represents Sparse Feature where IDs are set by Hashing} 6 | \usage{ 7 | column_categorical_with_hash_bucket(..., hash_bucket_size, dtype = tf$string) 8 | } 9 | \arguments{ 10 | \item{...}{Expression(s) identifying input feature(s). Used as the column 11 | name and the dictionary key for feature parsing configs, feature tensors, 12 | and feature columns.} 13 | 14 | \item{hash_bucket_size}{An int > 1. The number of buckets.} 15 | 16 | \item{dtype}{The type of features. Only string and integer types are 17 | supported.} 18 | } 19 | \value{ 20 | A \verb{_HashedCategoricalColumn}. 21 | } 22 | \description{ 23 | Use this when your sparse features are in string or integer format, and you 24 | want to distribute your inputs into a finite number of buckets by hashing. 25 | output_id = Hash(input_feature_string) \% bucket_size For input dictionary 26 | \code{features}, \verb{features$key$} is either tensor or sparse tensor object. If it's 27 | tensor object, missing values can be represented by \code{-1} for int and \code{''} for 28 | string. Note that these values are independent of the \code{default_value} 29 | argument. 30 | } 31 | \section{Raises}{ 32 | 33 | \itemize{ 34 | \item ValueError: \code{hash_bucket_size} is not greater than 1. 35 | \item ValueError: \code{dtype} is neither string nor integer. 36 | } 37 | } 38 | 39 | \seealso{ 40 | Other feature column constructors: 41 | \code{\link{column_bucketized}()}, 42 | \code{\link{column_categorical_weighted}()}, 43 | \code{\link{column_categorical_with_identity}()}, 44 | \code{\link{column_categorical_with_vocabulary_file}()}, 45 | \code{\link{column_categorical_with_vocabulary_list}()}, 46 | \code{\link{column_crossed}()}, 47 | \code{\link{column_embedding}()}, 48 | \code{\link{column_numeric}()}, 49 | \code{\link{input_layer}()} 50 | } 51 | \concept{feature column constructors} 52 | -------------------------------------------------------------------------------- /man/column_categorical_with_identity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_categorical_with_identity} 4 | \alias{column_categorical_with_identity} 5 | \title{Construct a Categorical Column that Returns Identity Values} 6 | \usage{ 7 | column_categorical_with_identity(..., num_buckets, default_value = NULL) 8 | } 9 | \arguments{ 10 | \item{...}{Expression(s) identifying input feature(s). Used as the column 11 | name and the dictionary key for feature parsing configs, feature tensors, 12 | and feature columns.} 13 | 14 | \item{num_buckets}{Number of unique values.} 15 | 16 | \item{default_value}{If \code{NULL}, this column's graph operations will fail for 17 | out-of-range inputs. Otherwise, this value must be in the range \verb{[0, num_buckets)}, and will replace inputs in that range.} 18 | } 19 | \value{ 20 | A categorical column that returns identity values. 21 | } 22 | \description{ 23 | Use this when your inputs are integers in the range \verb{[0, num_buckets)}, and 24 | you want to use the input value itself as the categorical ID. Values outside 25 | this range will result in \code{default_value} if specified, otherwise it will 26 | fail. 27 | } 28 | \details{ 29 | Typically, this is used for contiguous ranges of integer indexes, but it 30 | doesn't have to be. This might be inefficient, however, if many of IDs are 31 | unused. Consider \code{column_categorical_with_hash_bucket()} in that case. 32 | 33 | For input dictionary \code{features}, \code{features$key} is either tensor or sparse 34 | tensor object. If it's tensor object, missing values can be represented by \code{-1} for 35 | int and \code{''} for string. Note that these values are independent of the 36 | \code{default_value} argument. 37 | } 38 | \section{Raises}{ 39 | 40 | \itemize{ 41 | \item ValueError: if \code{num_buckets} is less than one. 42 | \item ValueError: if \code{default_value} is not in range \verb{[0, num_buckets)}. 43 | } 44 | } 45 | 46 | \seealso{ 47 | Other feature column constructors: 48 | \code{\link{column_bucketized}()}, 49 | \code{\link{column_categorical_weighted}()}, 50 | \code{\link{column_categorical_with_hash_bucket}()}, 51 | \code{\link{column_categorical_with_vocabulary_file}()}, 52 | \code{\link{column_categorical_with_vocabulary_list}()}, 53 | \code{\link{column_crossed}()}, 54 | \code{\link{column_embedding}()}, 55 | \code{\link{column_numeric}()}, 56 | \code{\link{input_layer}()} 57 | } 58 | \concept{feature column constructors} 59 | -------------------------------------------------------------------------------- /man/column_categorical_with_vocabulary_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_categorical_with_vocabulary_file} 4 | \alias{column_categorical_with_vocabulary_file} 5 | \title{Construct a Categorical Column with a Vocabulary File} 6 | \usage{ 7 | column_categorical_with_vocabulary_file( 8 | ..., 9 | vocabulary_file, 10 | vocabulary_size, 11 | num_oov_buckets = 0L, 12 | default_value = NULL, 13 | dtype = tf$string 14 | ) 15 | } 16 | \arguments{ 17 | \item{...}{Expression(s) identifying input feature(s). Used as the column 18 | name and the dictionary key for feature parsing configs, feature tensors, 19 | and feature columns.} 20 | 21 | \item{vocabulary_file}{The vocabulary file name.} 22 | 23 | \item{vocabulary_size}{Number of the elements in the vocabulary. This must be 24 | no greater than length of \code{vocabulary_file}, if less than length, later 25 | values are ignored.} 26 | 27 | \item{num_oov_buckets}{Non-negative integer, the number of out-of-vocabulary 28 | buckets. All out-of-vocabulary inputs will be assigned IDs in the range 29 | \verb{[vocabulary_size, vocabulary_size+num_oov_buckets)} based on a hash of the 30 | input value. A positive \code{num_oov_buckets} can not be specified with 31 | \code{default_value}.} 32 | 33 | \item{default_value}{The integer ID value to return for out-of-vocabulary 34 | feature values, defaults to \code{-1}. This can not be specified with a positive 35 | \code{num_oov_buckets}.} 36 | 37 | \item{dtype}{The type of features. Only string and integer types are 38 | supported.} 39 | } 40 | \value{ 41 | A categorical column with a vocabulary file. 42 | } 43 | \description{ 44 | Use this when your inputs are in string or integer format, and you have a 45 | vocabulary file that maps each value to an integer ID. By default, 46 | out-of-vocabulary values are ignored. Use either (but not both) of 47 | \code{num_oov_buckets} and \code{default_value} to specify how to include 48 | out-of-vocabulary values. For input dictionary \code{features}, \code{features[key]} is 49 | either tensor or sparse tensor object. If it's tensor object, missing values can be 50 | represented by \code{-1} for int and \code{''} for string. Note that these values are 51 | independent of the \code{default_value} argument. 52 | } 53 | \section{Raises}{ 54 | 55 | \itemize{ 56 | \item ValueError: \code{vocabulary_file} is missing. 57 | \item ValueError: \code{vocabulary_size} is missing or < 1. 58 | \item ValueError: \code{num_oov_buckets} is not a non-negative integer. 59 | \item ValueError: \code{dtype} is neither string nor integer. 60 | } 61 | } 62 | 63 | \seealso{ 64 | Other feature column constructors: 65 | \code{\link{column_bucketized}()}, 66 | \code{\link{column_categorical_weighted}()}, 67 | \code{\link{column_categorical_with_hash_bucket}()}, 68 | \code{\link{column_categorical_with_identity}()}, 69 | \code{\link{column_categorical_with_vocabulary_list}()}, 70 | \code{\link{column_crossed}()}, 71 | \code{\link{column_embedding}()}, 72 | \code{\link{column_numeric}()}, 73 | \code{\link{input_layer}()} 74 | } 75 | \concept{feature column constructors} 76 | -------------------------------------------------------------------------------- /man/column_categorical_with_vocabulary_list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_categorical_with_vocabulary_list} 4 | \alias{column_categorical_with_vocabulary_list} 5 | \title{Construct a Categorical Column with In-Memory Vocabulary} 6 | \usage{ 7 | column_categorical_with_vocabulary_list( 8 | ..., 9 | vocabulary_list, 10 | dtype = NULL, 11 | default_value = -1L, 12 | num_oov_buckets = 0L 13 | ) 14 | } 15 | \arguments{ 16 | \item{...}{Expression(s) identifying input feature(s). Used as the column 17 | name and the dictionary key for feature parsing configs, feature tensors, 18 | and feature columns.} 19 | 20 | \item{vocabulary_list}{An ordered iterable defining the vocabulary. Each 21 | feature is mapped to the index of its value (if present) in 22 | \code{vocabulary_list}. Must be castable to \code{dtype}.} 23 | 24 | \item{dtype}{The type of features. Only string and integer types are 25 | supported. If \code{NULL}, it will be inferred from \code{vocabulary_list}.} 26 | 27 | \item{default_value}{The value to use for values not in \code{vocabulary_list}.} 28 | 29 | \item{num_oov_buckets}{Non-negative integer, the number of out-of-vocabulary 30 | buckets. All out-of-vocabulary inputs will be assigned IDs in the range 31 | \verb{[vocabulary_size, vocabulary_size+num_oov_buckets)} based on a hash of the 32 | input value. A positive \code{num_oov_buckets} can not be specified with 33 | \code{default_value}.} 34 | } 35 | \value{ 36 | A categorical column with in-memory vocabulary. 37 | } 38 | \description{ 39 | Use this when your inputs are in string or integer format, and you have an 40 | in-memory vocabulary mapping each value to an integer ID. By default, 41 | out-of-vocabulary values are ignored. Use \code{default_value} to specify how to 42 | include out-of-vocabulary values. For the input dictionary \code{features}, 43 | \code{features$key} is either tensor or sparse tensor object. If it's tensor object, 44 | missing values can be represented by \code{-1} for int and \code{''} for string. 45 | } 46 | \details{ 47 | Note that these values are independent of the \code{default_value} argument. 48 | } 49 | \section{Raises}{ 50 | 51 | \itemize{ 52 | \item ValueError: if \code{vocabulary_list} is empty, or contains 53 | duplicate keys. 54 | \item ValueError: if \code{dtype} is not integer or string. 55 | } 56 | } 57 | 58 | \seealso{ 59 | Other feature column constructors: 60 | \code{\link{column_bucketized}()}, 61 | \code{\link{column_categorical_weighted}()}, 62 | \code{\link{column_categorical_with_hash_bucket}()}, 63 | \code{\link{column_categorical_with_identity}()}, 64 | \code{\link{column_categorical_with_vocabulary_file}()}, 65 | \code{\link{column_crossed}()}, 66 | \code{\link{column_embedding}()}, 67 | \code{\link{column_numeric}()}, 68 | \code{\link{input_layer}()} 69 | } 70 | \concept{feature column constructors} 71 | -------------------------------------------------------------------------------- /man/column_crossed.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_crossed} 4 | \alias{column_crossed} 5 | \title{Construct a Crossed Column} 6 | \usage{ 7 | column_crossed(keys, hash_bucket_size, hash_key = NULL) 8 | } 9 | \arguments{ 10 | \item{keys}{An iterable identifying the features to be crossed. Each element 11 | can be either: 12 | \itemize{ 13 | \item string: Will use the corresponding feature which must be of string type. 14 | \item categorical column: Will use the transformed tensor 15 | produced by this column. Does not support hashed categorical columns. 16 | }} 17 | 18 | \item{hash_bucket_size}{The number of buckets (> 1).} 19 | 20 | \item{hash_key}{Optional: specify the hash_key that will be used by the 21 | \code{FingerprintCat64} function to combine the crosses fingerprints on 22 | \code{SparseCrossOp}.} 23 | } 24 | \value{ 25 | A crossed column. 26 | } 27 | \description{ 28 | Returns a column for performing crosses of categorical features. Crossed 29 | features will be hashed according to \code{hash_bucket_size}. 30 | } 31 | \section{Raises}{ 32 | 33 | \itemize{ 34 | \item ValueError: If \code{len(keys) < 2}. 35 | \item ValueError: If any of the keys is neither a string nor categorical column. 36 | \item ValueError: If any of the keys is \verb{_HashedCategoricalColumn}. 37 | \item ValueError: If \code{hash_bucket_size < 1}. 38 | } 39 | } 40 | 41 | \seealso{ 42 | Other feature column constructors: 43 | \code{\link{column_bucketized}()}, 44 | \code{\link{column_categorical_weighted}()}, 45 | \code{\link{column_categorical_with_hash_bucket}()}, 46 | \code{\link{column_categorical_with_identity}()}, 47 | \code{\link{column_categorical_with_vocabulary_file}()}, 48 | \code{\link{column_categorical_with_vocabulary_list}()}, 49 | \code{\link{column_embedding}()}, 50 | \code{\link{column_numeric}()}, 51 | \code{\link{input_layer}()} 52 | } 53 | \concept{feature column constructors} 54 | -------------------------------------------------------------------------------- /man/column_embedding.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_embedding} 4 | \alias{column_embedding} 5 | \title{Construct a Dense Column} 6 | \usage{ 7 | column_embedding( 8 | categorical_column, 9 | dimension, 10 | combiner = "mean", 11 | initializer = NULL, 12 | ckpt_to_load_from = NULL, 13 | tensor_name_in_ckpt = NULL, 14 | max_norm = NULL, 15 | trainable = TRUE 16 | ) 17 | } 18 | \arguments{ 19 | \item{categorical_column}{A categorical column created by a 20 | \verb{column_categorical_*()} function. This column produces the sparse IDs 21 | that are inputs to the embedding lookup.} 22 | 23 | \item{dimension}{A positive integer, specifying dimension of the embedding.} 24 | 25 | \item{combiner}{A string specifying how to reduce if there are multiple 26 | entries in a single row. Currently \code{"mean"}, \code{"sqrtn"} and \code{"sum"} are 27 | supported, with \code{"mean"} the default. \code{"sqrtn"}' often achieves good 28 | accuracy, in particular with bag-of-words columns. Each of this can be 29 | thought as example level normalizations on the column.} 30 | 31 | \item{initializer}{A variable initializer function to be used in embedding 32 | variable initialization. If not specified, defaults to 33 | \code{tf$truncated_normal_initializer} with mean \code{0.0} and standard deviation 34 | \code{1 / sqrt(dimension)}.} 35 | 36 | \item{ckpt_to_load_from}{String representing checkpoint name/pattern from 37 | which to restore column weights. Required if \code{tensor_name_in_ckpt} is not 38 | \code{NULL}.} 39 | 40 | \item{tensor_name_in_ckpt}{Name of the \code{Tensor} in \code{ckpt_to_load_from} from 41 | which to restore the column weights. Required if \code{ckpt_to_load_from} is not 42 | \code{NULL}.} 43 | 44 | \item{max_norm}{If not \code{NULL}, embedding values are l2-normalized to this 45 | value.} 46 | 47 | \item{trainable}{Whether or not the embedding is trainable. Default is TRUE.} 48 | } 49 | \value{ 50 | A dense column that converts from sparse input. 51 | } 52 | \description{ 53 | Use this when your inputs are sparse, but you want to convert them to a dense 54 | representation (e.g., to feed to a DNN). Inputs must be a 55 | categorical column created by any of the \verb{column_categorical_*()} 56 | functions. 57 | } 58 | \section{Raises}{ 59 | 60 | \itemize{ 61 | \item ValueError: if \code{dimension} not > 0. 62 | \item ValueError: if exactly one of \code{ckpt_to_load_from} and \code{tensor_name_in_ckpt} 63 | is specified. 64 | \item ValueError: if \code{initializer} is specified and is not callable. 65 | } 66 | } 67 | 68 | \seealso{ 69 | Other feature column constructors: 70 | \code{\link{column_bucketized}()}, 71 | \code{\link{column_categorical_weighted}()}, 72 | \code{\link{column_categorical_with_hash_bucket}()}, 73 | \code{\link{column_categorical_with_identity}()}, 74 | \code{\link{column_categorical_with_vocabulary_file}()}, 75 | \code{\link{column_categorical_with_vocabulary_list}()}, 76 | \code{\link{column_crossed}()}, 77 | \code{\link{column_numeric}()}, 78 | \code{\link{input_layer}()} 79 | } 80 | \concept{feature column constructors} 81 | -------------------------------------------------------------------------------- /man/column_indicator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_indicator} 4 | \alias{column_indicator} 5 | \title{Represents Multi-Hot Representation of Given Categorical Column} 6 | \usage{ 7 | column_indicator(categorical_column) 8 | } 9 | \arguments{ 10 | \item{categorical_column}{A categorical column which is created by 11 | the \verb{column_categorical_with_*()} or \code{column_crossed()} functions.} 12 | } 13 | \value{ 14 | An indicator column. 15 | } 16 | \description{ 17 | Used to wrap any \verb{column_categorical()*} (e.g., to feed to DNN). Use 18 | \code{column_embedding()} if the inputs are sparse. 19 | } 20 | -------------------------------------------------------------------------------- /man/column_numeric.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{column_numeric} 4 | \alias{column_numeric} 5 | \title{Construct a Real-Valued Column} 6 | \usage{ 7 | column_numeric( 8 | ..., 9 | shape = c(1L), 10 | default_value = NULL, 11 | dtype = tf$float32, 12 | normalizer_fn = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{...}{Expression(s) identifying input feature(s). Used as the column 17 | name and the dictionary key for feature parsing configs, feature tensors, 18 | and feature columns.} 19 | 20 | \item{shape}{An integer vector that specifies the shape of the tensor. An 21 | integer can be given which means a single dimension tensor with given 22 | width. The tensor representing the column will have the shape of 23 | \code{batch_size + shape}.} 24 | 25 | \item{default_value}{A single value compatible with \code{dtype} or an iterable of 26 | values compatible with \code{dtype} which the column takes on during parsing if 27 | data is missing. A default value of \code{NULL} will cause \code{tf$parse_example} to 28 | fail if an example does not contain this column. If a single value is 29 | provided, the same value will be applied as the default value for every 30 | item. If an iterable of values is provided, the shape of the 31 | \code{default_value} should be equal to the given \code{shape}.} 32 | 33 | \item{dtype}{The types for values contained in the column. The default value 34 | is \code{tf$float32}. Must be a non-quantized, real integer or floating point 35 | type.} 36 | 37 | \item{normalizer_fn}{If not \code{NULL}, a function that can be used to normalize 38 | the value of the tensor after \code{default_value} is applied for parsing. 39 | Normalizer function takes the input \code{Tensor} as its argument, and returns 40 | the output tensor. (e.g. \verb{function(x) \{(x - 3.0) / 4.2)\}}. Please note that 41 | even though the most common use case of this function is normalization, it 42 | can be used for any kind of Tensorflow transformations.} 43 | } 44 | \value{ 45 | A numeric column. 46 | } 47 | \description{ 48 | Construct a Real-Valued Column 49 | } 50 | \section{Raises}{ 51 | 52 | \itemize{ 53 | \item TypeError: if any dimension in shape is not an int 54 | \item ValueError: if any dimension in shape is not a positive integer 55 | \item TypeError: if \code{default_value} is an iterable but not compatible with \code{shape} 56 | \item TypeError: if \code{default_value} is not compatible with \code{dtype} 57 | \item ValueError: if \code{dtype} is not convertible to \code{tf$float32} 58 | } 59 | } 60 | 61 | \seealso{ 62 | Other feature column constructors: 63 | \code{\link{column_bucketized}()}, 64 | \code{\link{column_categorical_weighted}()}, 65 | \code{\link{column_categorical_with_hash_bucket}()}, 66 | \code{\link{column_categorical_with_identity}()}, 67 | \code{\link{column_categorical_with_vocabulary_file}()}, 68 | \code{\link{column_categorical_with_vocabulary_list}()}, 69 | \code{\link{column_crossed}()}, 70 | \code{\link{column_embedding}()}, 71 | \code{\link{input_layer}()} 72 | } 73 | \concept{feature column constructors} 74 | -------------------------------------------------------------------------------- /man/dnn_estimators.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dnn_estimators.R 3 | \name{dnn_estimators} 4 | \alias{dnn_estimators} 5 | \alias{dnn_regressor} 6 | \alias{dnn_classifier} 7 | \title{Deep Neural Networks} 8 | \usage{ 9 | dnn_regressor( 10 | hidden_units, 11 | feature_columns, 12 | model_dir = NULL, 13 | label_dimension = 1L, 14 | weight_column = NULL, 15 | optimizer = "Adagrad", 16 | activation_fn = "relu", 17 | dropout = NULL, 18 | input_layer_partitioner = NULL, 19 | config = NULL 20 | ) 21 | 22 | dnn_classifier( 23 | hidden_units, 24 | feature_columns, 25 | model_dir = NULL, 26 | n_classes = 2L, 27 | weight_column = NULL, 28 | label_vocabulary = NULL, 29 | optimizer = "Adagrad", 30 | activation_fn = "relu", 31 | dropout = NULL, 32 | input_layer_partitioner = NULL, 33 | config = NULL 34 | ) 35 | } 36 | \arguments{ 37 | \item{hidden_units}{An integer vector, indicating the number of hidden 38 | units in each layer. All layers are fully connected. For example, 39 | \code{c(64, 32)} means the first layer has 64 nodes, and the second layer 40 | has 32 nodes.} 41 | 42 | \item{feature_columns}{An \R list containing all of the feature columns used 43 | by the model (typically, generated by \code{\link[=feature_columns]{feature_columns()}}).} 44 | 45 | \item{model_dir}{Directory to save the model parameters, graph, and so on. 46 | This can also be used to load checkpoints from the directory into a 47 | estimator to continue training a previously saved model.} 48 | 49 | \item{label_dimension}{Number of regression targets per example. This is the 50 | size of the last dimension of the labels and logits \code{Tensor} objects 51 | (typically, these have shape \verb{[batch_size, label_dimension]}).} 52 | 53 | \item{weight_column}{A string, or a numeric column created by 54 | \code{\link[=column_numeric]{column_numeric()}} defining feature column representing weights. It is used 55 | to down weight or boost examples during training. It will be multiplied by 56 | the loss of the example. If it is a string, it is used as a key to fetch 57 | weight tensor from the \code{features} argument. If it is a numeric column, 58 | then the raw tensor is fetched by key \code{weight_column$key}, then 59 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 60 | 61 | \item{optimizer}{Either the name of the optimizer to be used when training 62 | the model, or a TensorFlow optimizer instance. Defaults to the Adagrad 63 | optimizer.} 64 | 65 | \item{activation_fn}{The activation function to apply to each layer. This can either be an 66 | actual activation function (e.g. \code{tf$nn$relu}), or the name of an 67 | activation function (e.g. \code{"relu"}). Defaults to the 68 | \code{"relu"} activation function. See 69 | \url{https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nn} 70 | for documentation related to the set of activation functions available 71 | in TensorFlow.} 72 | 73 | \item{dropout}{When not \code{NULL}, the probability we will drop out a given 74 | coordinate.} 75 | 76 | \item{input_layer_partitioner}{An optional partitioner for the input layer. 77 | Defaults to \code{min_max_variable_partitioner} with \code{min_slice_size} 64 << 20.} 78 | 79 | \item{config}{A run configuration created by \code{\link[=run_config]{run_config()}}, used to configure the runtime 80 | settings.} 81 | 82 | \item{n_classes}{The number of label classes.} 83 | 84 | \item{label_vocabulary}{A list of strings represents possible label values. 85 | If given, labels must be string type and have any value in 86 | \code{label_vocabulary}. If it is not given, that means labels are already 87 | encoded as integer or float within \verb{[0, 1]} for \code{n_classes == 2} and 88 | encoded as integer values in \verb{\{0, 1,..., n_classes -1\}} for \code{n_classes > 2}. Also there will be errors if vocabulary is not provided and labels are 89 | string.} 90 | } 91 | \description{ 92 | Create a deep neural network (DNN) estimator. 93 | } 94 | \seealso{ 95 | Other canned estimators: 96 | \code{\link{boosted_trees_estimators}}, 97 | \code{\link{dnn_linear_combined_estimators}}, 98 | \code{\link{linear_estimators}} 99 | } 100 | \concept{canned estimators} 101 | -------------------------------------------------------------------------------- /man/estimator_spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_custom_estimator.R 3 | \name{estimator_spec} 4 | \alias{estimator_spec} 5 | \title{Define an Estimator Specification} 6 | \usage{ 7 | estimator_spec( 8 | mode, 9 | predictions = NULL, 10 | loss = NULL, 11 | train_op = NULL, 12 | eval_metric_ops = NULL, 13 | training_hooks = NULL, 14 | evaluation_hooks = NULL, 15 | prediction_hooks = NULL, 16 | training_chief_hooks = NULL, 17 | ... 18 | ) 19 | } 20 | \arguments{ 21 | \item{mode}{A key that specifies whether we are performing 22 | training (\code{"train"}), evaluation (\code{"eval"}), or prediction (\code{"infer"}). 23 | These values can also be accessed through the \code{\link[=mode_keys]{mode_keys()}} object.} 24 | 25 | \item{predictions}{The prediction tensor(s).} 26 | 27 | \item{loss}{The training loss tensor. Must be either scalar, or with shape \code{c(1)}.} 28 | 29 | \item{train_op}{The training operation -- typically, a call to \code{optimizer$minimize(...)}, 30 | depending on the type of optimizer used during training.} 31 | 32 | \item{eval_metric_ops}{A list of metrics to be computed as part of evaluation. 33 | This should be a named list, mapping metric names (e.g. \code{"rmse"}) to the operation 34 | that computes the associated metric (e.g. \code{tf$metrics$root_mean_squared_error(...)}). 35 | These metric operations should be evaluated without any impact on state (typically 36 | is a pure computation results based on variables). For example, it should not 37 | trigger the update ops or requires any input fetching.} 38 | 39 | \item{training_hooks}{(Available since TensorFlow v1.4) A list of session run hooks to run on all workers during training.} 40 | 41 | \item{evaluation_hooks}{(Available since TensorFlow v1.4) A list of session run hooks to run during evaluation.} 42 | 43 | \item{prediction_hooks}{(Available since TensorFlow v1.7) A list of session run hooks to run during prediciton.} 44 | 45 | \item{training_chief_hooks}{(Available since TensorFlow v1.4) A list of session run hooks to run on chief worker during training.} 46 | 47 | \item{...}{Other optional (named) arguments, to be passed to the \code{EstimatorSpec} constructor.} 48 | } 49 | \description{ 50 | Define the estimator specification, used as part of the \code{model_fn} defined with 51 | custom estimators created by \code{\link[=estimator]{estimator()}}. See \code{\link[=estimator]{estimator()}} for more details. 52 | } 53 | \seealso{ 54 | Other custom estimator methods: 55 | \code{\link{estimator}()}, 56 | \code{\link{evaluate.tf_estimator}()}, 57 | \code{\link{export_savedmodel.tf_estimator}()}, 58 | \code{\link{predict.tf_estimator}()}, 59 | \code{\link{train.tf_estimator}()} 60 | } 61 | \concept{custom estimator methods} 62 | -------------------------------------------------------------------------------- /man/estimators.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{estimators} 4 | \alias{estimators} 5 | \title{Base Documentation for Canned Estimators} 6 | \arguments{ 7 | \item{object}{A TensorFlow estimator.} 8 | 9 | \item{feature_columns}{An \R list containing all of the feature columns used 10 | by the model (typically, generated by \code{\link[=feature_columns]{feature_columns()}}).} 11 | 12 | \item{model_dir}{Directory to save the model parameters, graph, and so on. 13 | This can also be used to load checkpoints from the directory into a 14 | estimator to continue training a previously saved model.} 15 | 16 | \item{label_dimension}{Number of regression targets per example. This is the 17 | size of the last dimension of the labels and logits \code{Tensor} objects 18 | (typically, these have shape \verb{[batch_size, label_dimension]}).} 19 | 20 | \item{label_vocabulary}{A list of strings represents possible label values. 21 | If given, labels must be string type and have any value in 22 | \code{label_vocabulary}. If it is not given, that means labels are already 23 | encoded as integer or float within \verb{[0, 1]} for \code{n_classes == 2} and 24 | encoded as integer values in \verb{\{0, 1,..., n_classes -1\}} for \code{n_classes > 2}. Also there will be errors if vocabulary is not provided and labels are 25 | string.} 26 | 27 | \item{weight_column}{A string, or a numeric column created by 28 | \code{\link[=column_numeric]{column_numeric()}} defining feature column representing weights. It is used 29 | to down weight or boost examples during training. It will be multiplied by 30 | the loss of the example. If it is a string, it is used as a key to fetch 31 | weight tensor from the \code{features} argument. If it is a numeric column, 32 | then the raw tensor is fetched by key \code{weight_column$key}, then 33 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 34 | 35 | \item{n_classes}{The number of label classes.} 36 | 37 | \item{config}{A run configuration created by \code{\link[=run_config]{run_config()}}, used to configure the runtime 38 | settings.} 39 | 40 | \item{input_layer_partitioner}{An optional partitioner for the input layer. 41 | Defaults to \code{min_max_variable_partitioner} with \code{min_slice_size} 64 << 20.} 42 | 43 | \item{partitioner}{An optional partitioner for the input layer.} 44 | } 45 | \description{ 46 | Base Documentation for Canned Estimators 47 | } 48 | -------------------------------------------------------------------------------- /man/eval_spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/train_and_evaluate.R 3 | \name{eval_spec} 4 | \alias{eval_spec} 5 | \title{Configuration for the eval component of \code{train_and_evaluate}} 6 | \usage{ 7 | eval_spec( 8 | input_fn, 9 | steps = 100, 10 | name = NULL, 11 | hooks = NULL, 12 | exporters = NULL, 13 | start_delay_secs = 120, 14 | throttle_secs = 600 15 | ) 16 | } 17 | \arguments{ 18 | \item{input_fn}{Evaluation input function returning a tuple of: 19 | \itemize{ 20 | \item features - \code{Tensor} or dictionary of string feature name to \code{Tensor}. 21 | \item labels - \code{Tensor} or dictionary of \code{Tensor} with labels. 22 | }} 23 | 24 | \item{steps}{Positive number of steps for which to evaluate model. 25 | If \code{NULL}, evaluates until \code{input_fn} raises an end-of-input exception.} 26 | 27 | \item{name}{Name of the evaluation if user needs to run multiple 28 | evaluations on different data sets. Metrics for different evaluations 29 | are saved in separate folders, and appear separately in tensorboard.} 30 | 31 | \item{hooks}{List of session run hooks to run 32 | during evaluation.} 33 | 34 | \item{exporters}{List of \code{Exporter}s, or a single one, or \code{NULL}. 35 | \code{exporters} will be invoked after each evaluation.} 36 | 37 | \item{start_delay_secs}{Start evaluating after waiting for this many 38 | seconds.} 39 | 40 | \item{throttle_secs}{Do not re-evaluate unless the last evaluation was 41 | started at least this many seconds ago. Of course, evaluation does not 42 | occur if no new checkpoints are available, hence, this is the minimum.} 43 | } 44 | \description{ 45 | \code{EvalSpec} combines details of evaluation of the trained model as well as its 46 | export. Evaluation consists of computing metrics to judge the performance of 47 | the trained model. Export writes out the trained model on to external 48 | storage. 49 | } 50 | \seealso{ 51 | Other training methods: 52 | \code{\link{train_and_evaluate.tf_estimator}()}, 53 | \code{\link{train_spec}()} 54 | } 55 | \concept{training methods} 56 | -------------------------------------------------------------------------------- /man/evaluate.tf_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{evaluate.tf_estimator} 4 | \alias{evaluate.tf_estimator} 5 | \title{Evaluate an Estimator} 6 | \usage{ 7 | \method{evaluate}{tf_estimator}( 8 | object, 9 | input_fn, 10 | steps = NULL, 11 | checkpoint_path = NULL, 12 | name = NULL, 13 | hooks = NULL, 14 | simplify = TRUE, 15 | ... 16 | ) 17 | } 18 | \arguments{ 19 | \item{object}{A TensorFlow estimator.} 20 | 21 | \item{input_fn}{An input function, typically generated by the \code{\link[=input_fn]{input_fn()}} 22 | helper function.} 23 | 24 | \item{steps}{The number of steps for which the model should be evaluated on 25 | this particular \code{evaluate()} invocation. If \code{NULL} (the default), this function 26 | will either evaluate forever, or until the supplied \code{input_fn()} has provided 27 | all available data.} 28 | 29 | \item{checkpoint_path}{The path to a specific model checkpoint to be used for 30 | prediction. If \code{NULL} (the default), the latest checkpoint in \code{model_dir} 31 | is used.} 32 | 33 | \item{name}{Name of the evaluation if user needs to run multiple evaluations 34 | on different data sets, such as on training data vs test data. Metrics for 35 | different evaluations are saved in separate folders, and appear separately 36 | in tensorboard.} 37 | 38 | \item{hooks}{A list of \R functions, to be used as callbacks inside the 39 | training loop. By default, \code{hook_history_saver(every_n_step = 10)} and 40 | \code{hook_progress_bar()} will be attached if not provided to save the metrics 41 | history and create the progress bar.} 42 | 43 | \item{simplify}{Whether to simplify evaluation results into a \code{tibble}, as 44 | opposed to a list. Defaults to \code{TRUE}.} 45 | 46 | \item{...}{Optional arguments passed on to the estimator's \code{evaluate()} 47 | method.} 48 | } 49 | \value{ 50 | An \R list of evaluation metrics. 51 | } 52 | \description{ 53 | Evaluate an estimator on input data provided by an \code{input_fn()}. 54 | } 55 | \details{ 56 | For each step, this method will call \code{input_fn()} to produce a single batch 57 | of data. Evaluation continues until: 58 | \itemize{ 59 | \item \code{steps} batches are processed, or 60 | \item The \code{input_fn()} is exhausted of data. 61 | } 62 | } 63 | \seealso{ 64 | Other custom estimator methods: 65 | \code{\link{estimator_spec}()}, 66 | \code{\link{estimator}()}, 67 | \code{\link{export_savedmodel.tf_estimator}()}, 68 | \code{\link{predict.tf_estimator}()}, 69 | \code{\link{train.tf_estimator}()} 70 | } 71 | \concept{custom estimator methods} 72 | -------------------------------------------------------------------------------- /man/experiment.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/generic_methods.R 3 | \name{experiment} 4 | \alias{experiment} 5 | \title{Construct an Experiment} 6 | \usage{ 7 | experiment(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{An \R object.} 11 | 12 | \item{...}{Optional arguments passed on to implementing methods.} 13 | } 14 | \description{ 15 | Construct an experiment object. 16 | } 17 | -------------------------------------------------------------------------------- /man/export_savedmodel.tf_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{export_savedmodel.tf_estimator} 4 | \alias{export_savedmodel.tf_estimator} 5 | \title{Save an Estimator} 6 | \usage{ 7 | \method{export_savedmodel}{tf_estimator}( 8 | object, 9 | export_dir_base, 10 | serving_input_receiver_fn = NULL, 11 | assets_extra = NULL, 12 | as_text = FALSE, 13 | checkpoint_path = NULL, 14 | overwrite = TRUE, 15 | versioned = !overwrite, 16 | ... 17 | ) 18 | } 19 | \arguments{ 20 | \item{object}{A TensorFlow estimator.} 21 | 22 | \item{export_dir_base}{A string containing a directory in which to export the 23 | SavedModel.} 24 | 25 | \item{serving_input_receiver_fn}{A function that takes no argument and 26 | returns a \code{ServingInputReceiver}. Required for custom models.} 27 | 28 | \item{assets_extra}{A dict specifying how to populate the assets.extra 29 | directory within the exported SavedModel, or \code{NULL} if no extra assets are 30 | needed.} 31 | 32 | \item{as_text}{whether to write the SavedModel proto in text format.} 33 | 34 | \item{checkpoint_path}{The checkpoint path to export. If \code{NULL} (the 35 | default), the most recent checkpoint found within the model directory is 36 | chosen.} 37 | 38 | \item{overwrite}{Should the \code{export_dir} directory be overwritten?} 39 | 40 | \item{versioned}{Should the model be exported under a versioned subdirectory?} 41 | 42 | \item{...}{Optional arguments passed on to the estimator's 43 | \code{export_savedmodel()} method.} 44 | } 45 | \value{ 46 | The path to the exported directory, as a string. 47 | } 48 | \description{ 49 | Save an estimator (alongside its weights) to the directory \code{export_dir_base}. 50 | } 51 | \details{ 52 | This method builds a new graph by first calling the serving_input_receiver_fn 53 | to obtain feature \code{Tensor}s, and then calling this \code{Estimator}'s model_fn to 54 | generate the model graph based on those features. It restores the given 55 | checkpoint (or, lacking that, the most recent checkpoint) into this graph in 56 | a fresh session. Finally it creates a timestamped export directory below the 57 | given export_dir_base, and writes a \code{SavedModel} into it containing a single 58 | \code{MetaGraphDef} saved from this session. The exported \code{MetaGraphDef} will 59 | provide one \code{SignatureDef} for each element of the export_outputs dict 60 | returned from the model_fn, named using the same keys. One of these keys is 61 | always signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating 62 | which signature will be served when a serving request does not specify one. 63 | For each signature, the outputs are provided by the corresponding 64 | \code{ExportOutput}s, and the inputs are always the input receivers provided by 65 | the serving_input_receiver_fn. Extra assets may be written into the 66 | SavedModel via the extra_assets argument. This should be a dict, where each 67 | key gives a destination path (including the filename) relative to the 68 | assets.extra directory. The corresponding value gives the full path of the 69 | source file to be copied. For example, the simple case of copying a single 70 | file without renaming it is specified as \code{{'my_asset_file.txt': '/path/to/my_asset_file.txt'}}. 71 | } 72 | \section{Raises}{ 73 | ValueError: if no serving_input_receiver_fn is provided, no 74 | export_outputs are provided, or no checkpoint can be found. 75 | } 76 | 77 | \seealso{ 78 | Other custom estimator methods: 79 | \code{\link{estimator_spec}()}, 80 | \code{\link{estimator}()}, 81 | \code{\link{evaluate.tf_estimator}()}, 82 | \code{\link{predict.tf_estimator}()}, 83 | \code{\link{train.tf_estimator}()} 84 | } 85 | \concept{custom estimator methods} 86 | -------------------------------------------------------------------------------- /man/feature_columns.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{feature_columns} 4 | \alias{feature_columns} 5 | \title{Feature Columns} 6 | \usage{ 7 | feature_columns(..., names = NULL) 8 | } 9 | \arguments{ 10 | \item{...}{One or more feature column definitions. The \link{tidyselect} package 11 | is used to power generation of feature columns.} 12 | 13 | \item{names}{Available feature names (for selection / pattern matching) as a 14 | character vector (or R object that implements \code{names()} or \code{colnames()}).} 15 | } 16 | \description{ 17 | Constructors for feature columns. A feature column defines the expected 18 | 'shape' of an input Tensor. 19 | } 20 | -------------------------------------------------------------------------------- /man/graph_keys.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/estimator_keys.R 3 | \name{graph_keys} 4 | \alias{graph_keys} 5 | \title{Standard Names to Use for Graph Collections} 6 | \usage{ 7 | graph_keys() 8 | } 9 | \description{ 10 | The standard library uses various well-known names to collect and retrieve 11 | values associated with a graph. 12 | } 13 | \details{ 14 | For example, the \code{tf$Optimizer} subclasses default to optimizing the 15 | variables collected under\code{graph_keys()$TRAINABLE_VARIABLES} if \code{NULL} is 16 | specified, but it is also possible to pass an explicit list of variables. 17 | 18 | The following standard keys are defined: 19 | \itemize{ 20 | \item \code{GLOBAL_VARIABLES}: the default collection of \code{Variable} objects, shared 21 | across distributed environment (model variables are subset of these). See 22 | \code{tf$global_variables} for more details. Commonly, all \code{TRAINABLE_VARIABLES} 23 | variables will be in \code{MODEL_VARIABLES}, and all \code{MODEL_VARIABLES} variables 24 | will be in \code{GLOBAL_VARIABLES}. 25 | \item \code{LOCAL_VARIABLES}: the subset of \code{Variable} objects that are local to each 26 | machine. Usually used for temporarily variables, like counters. Note: use 27 | \code{tf$contrib$framework$local_variable} to add to this collection. 28 | \item \code{MODEL_VARIABLES}: the subset of \code{Variable} objects that are used in the 29 | model for inference (feed forward). Note: use 30 | \code{tf$contrib$framework$model_variable} to add to this collection. 31 | \item \code{TRAINABLE_VARIABLES}: the subset of \code{Variable} objects that will be 32 | trained by an optimizer. See \code{tf$trainable_variables} for more details. 33 | \item \code{SUMMARIES}: the summary \code{Tensor} objects that have been created in the 34 | graph. See \code{tf$summary$merge_all} for more details. 35 | \item \code{QUEUE_RUNNERS}: the \code{QueueRunner} objects that are used to produce input 36 | for a computation. See \code{tf$train$start_queue_runners} for more details. 37 | \item \code{MOVING_AVERAGE_VARIABLES}: the subset of \code{Variable} objects that will also 38 | keep moving averages. See \code{tf$moving_average_variables} for more details. 39 | \item \code{REGULARIZATION_LOSSES}: regularization losses collected during graph 40 | construction. The following standard keys are defined, but their 41 | collections are \strong{not} automatically populated as many of the others are: 42 | \itemize{ 43 | \item \code{WEIGHTS} 44 | \item \code{BIASES} 45 | \item \code{ACTIVATIONS} 46 | } 47 | } 48 | } 49 | \examples{ 50 | \dontrun{ 51 | graph_keys() 52 | graph_keys()$LOSSES 53 | } 54 | 55 | } 56 | \seealso{ 57 | Other utility functions: 58 | \code{\link{latest_checkpoint}()} 59 | } 60 | \concept{utility functions} 61 | -------------------------------------------------------------------------------- /man/hook_checkpoint_saver.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_checkpoint_saver} 4 | \alias{hook_checkpoint_saver} 5 | \title{Saves Checkpoints Every N Steps or Seconds} 6 | \usage{ 7 | hook_checkpoint_saver( 8 | checkpoint_dir, 9 | save_secs = NULL, 10 | save_steps = NULL, 11 | saver = NULL, 12 | checkpoint_basename = "model.ckpt", 13 | scaffold = NULL, 14 | listeners = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{checkpoint_dir}{The base directory for the checkpoint files.} 19 | 20 | \item{save_secs}{An integer, indicating saving checkpoints every N secs.} 21 | 22 | \item{save_steps}{An integer, indicating saving checkpoints every N steps.} 23 | 24 | \item{saver}{A saver object, used for saving.} 25 | 26 | \item{checkpoint_basename}{The base name for the checkpoint files.} 27 | 28 | \item{scaffold}{A scaffold, used to get saver object.} 29 | 30 | \item{listeners}{List of checkpoint saver listener subclass instances, used 31 | for callbacks that run immediately after the corresponding 32 | \code{hook_checkpoint_saver} callbacks, only in steps where \verb{the hook_checkpoint_saver} 33 | was triggered.} 34 | } 35 | \description{ 36 | Saves Checkpoints Every N Steps or Seconds 37 | } 38 | \seealso{ 39 | Other session_run_hook wrappers: 40 | \code{\link{hook_global_step_waiter}()}, 41 | \code{\link{hook_history_saver}()}, 42 | \code{\link{hook_logging_tensor}()}, 43 | \code{\link{hook_nan_tensor}()}, 44 | \code{\link{hook_progress_bar}()}, 45 | \code{\link{hook_step_counter}()}, 46 | \code{\link{hook_stop_at_step}()}, 47 | \code{\link{hook_summary_saver}()}, 48 | \code{\link{session_run_hook}()} 49 | } 50 | \concept{session_run_hook wrappers} 51 | -------------------------------------------------------------------------------- /man/hook_global_step_waiter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_global_step_waiter} 4 | \alias{hook_global_step_waiter} 5 | \title{Delay Execution until Global Step Reaches to \code{wait_until_step}.} 6 | \usage{ 7 | hook_global_step_waiter(wait_until_step) 8 | } 9 | \arguments{ 10 | \item{wait_until_step}{An integer indicating that until which global step should we wait.} 11 | } 12 | \description{ 13 | This hook delays execution until global step reaches to \code{wait_until_step}. It 14 | is used to gradually start workers in distributed settings. One example usage 15 | would be setting \code{wait_until_step=int(K*log(task_id+1))} assuming that 16 | \code{task_id=0} is the chief. 17 | } 18 | \seealso{ 19 | Other session_run_hook wrappers: 20 | \code{\link{hook_checkpoint_saver}()}, 21 | \code{\link{hook_history_saver}()}, 22 | \code{\link{hook_logging_tensor}()}, 23 | \code{\link{hook_nan_tensor}()}, 24 | \code{\link{hook_progress_bar}()}, 25 | \code{\link{hook_step_counter}()}, 26 | \code{\link{hook_stop_at_step}()}, 27 | \code{\link{hook_summary_saver}()}, 28 | \code{\link{session_run_hook}()} 29 | } 30 | \concept{session_run_hook wrappers} 31 | -------------------------------------------------------------------------------- /man/hook_history_saver.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_custom.R 3 | \name{hook_history_saver} 4 | \alias{hook_history_saver} 5 | \title{A Custom Run Hook for Saving Metrics History} 6 | \usage{ 7 | hook_history_saver(every_n_step = 10) 8 | } 9 | \arguments{ 10 | \item{every_n_step}{Save the metrics every N steps} 11 | } 12 | \description{ 13 | This hook allows users to save the metrics history produced during training or evaluation in 14 | a specified frequency. 15 | } 16 | \seealso{ 17 | Other session_run_hook wrappers: 18 | \code{\link{hook_checkpoint_saver}()}, 19 | \code{\link{hook_global_step_waiter}()}, 20 | \code{\link{hook_logging_tensor}()}, 21 | \code{\link{hook_nan_tensor}()}, 22 | \code{\link{hook_progress_bar}()}, 23 | \code{\link{hook_step_counter}()}, 24 | \code{\link{hook_stop_at_step}()}, 25 | \code{\link{hook_summary_saver}()}, 26 | \code{\link{session_run_hook}()} 27 | } 28 | \concept{session_run_hook wrappers} 29 | -------------------------------------------------------------------------------- /man/hook_logging_tensor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_logging_tensor} 4 | \alias{hook_logging_tensor} 5 | \title{Prints Given Tensors Every N Local Steps, Every N Seconds, or at End} 6 | \usage{ 7 | hook_logging_tensor( 8 | tensors, 9 | every_n_iter = NULL, 10 | every_n_secs = NULL, 11 | formatter = NULL, 12 | at_end = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{tensors}{A list that maps string-valued tags to tensors/tensor names.} 17 | 18 | \item{every_n_iter}{An integer value, indicating the values of \code{tensors} will be printed 19 | once every N local steps taken on the current worker.} 20 | 21 | \item{every_n_secs}{An integer or float value, indicating the values of \code{tensors} will be printed 22 | once every N seconds. Exactly one of \code{every_n_iter} and \code{every_n_secs} should be provided.} 23 | 24 | \item{formatter}{A function that takes \code{list(tag = tensor)} and returns a 25 | string. If \code{NULL} uses default printing all tensors.} 26 | 27 | \item{at_end}{A boolean value specifying whether to print the values of \code{tensors} at the 28 | end of the run.} 29 | } 30 | \description{ 31 | The tensors will be printed to the log, with \code{INFO} severity. 32 | } 33 | \details{ 34 | Note that if \code{at_end} is \code{TRUE}, \code{tensors} should not include any tensor 35 | whose evaluation produces a side effect such as consuming additional inputs. 36 | } 37 | \seealso{ 38 | Other session_run_hook wrappers: 39 | \code{\link{hook_checkpoint_saver}()}, 40 | \code{\link{hook_global_step_waiter}()}, 41 | \code{\link{hook_history_saver}()}, 42 | \code{\link{hook_nan_tensor}()}, 43 | \code{\link{hook_progress_bar}()}, 44 | \code{\link{hook_step_counter}()}, 45 | \code{\link{hook_stop_at_step}()}, 46 | \code{\link{hook_summary_saver}()}, 47 | \code{\link{session_run_hook}()} 48 | } 49 | \concept{session_run_hook wrappers} 50 | -------------------------------------------------------------------------------- /man/hook_nan_tensor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_nan_tensor} 4 | \alias{hook_nan_tensor} 5 | \title{NaN Loss Monitor} 6 | \usage{ 7 | hook_nan_tensor(loss_tensor, fail_on_nan_loss = TRUE) 8 | } 9 | \arguments{ 10 | \item{loss_tensor}{The loss tensor.} 11 | 12 | \item{fail_on_nan_loss}{A boolean indicating whether to raise exception when loss is NaN.} 13 | } 14 | \description{ 15 | Monitors loss and stops training if loss is NaN. Can either fail with 16 | exception or just stop training. 17 | } 18 | \seealso{ 19 | Other session_run_hook wrappers: 20 | \code{\link{hook_checkpoint_saver}()}, 21 | \code{\link{hook_global_step_waiter}()}, 22 | \code{\link{hook_history_saver}()}, 23 | \code{\link{hook_logging_tensor}()}, 24 | \code{\link{hook_progress_bar}()}, 25 | \code{\link{hook_step_counter}()}, 26 | \code{\link{hook_stop_at_step}()}, 27 | \code{\link{hook_summary_saver}()}, 28 | \code{\link{session_run_hook}()} 29 | } 30 | \concept{session_run_hook wrappers} 31 | -------------------------------------------------------------------------------- /man/hook_progress_bar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_custom.R 3 | \name{hook_progress_bar} 4 | \alias{hook_progress_bar} 5 | \title{A Custom Run Hook to Create and Update Progress Bar During Training or Evaluation} 6 | \usage{ 7 | hook_progress_bar() 8 | } 9 | \description{ 10 | This hook creates a progress bar that creates and updates the progress bar during training 11 | or evaluation. 12 | } 13 | \seealso{ 14 | Other session_run_hook wrappers: 15 | \code{\link{hook_checkpoint_saver}()}, 16 | \code{\link{hook_global_step_waiter}()}, 17 | \code{\link{hook_history_saver}()}, 18 | \code{\link{hook_logging_tensor}()}, 19 | \code{\link{hook_nan_tensor}()}, 20 | \code{\link{hook_step_counter}()}, 21 | \code{\link{hook_stop_at_step}()}, 22 | \code{\link{hook_summary_saver}()}, 23 | \code{\link{session_run_hook}()} 24 | } 25 | \concept{session_run_hook wrappers} 26 | -------------------------------------------------------------------------------- /man/hook_step_counter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_step_counter} 4 | \alias{hook_step_counter} 5 | \title{Steps per Second Monitor} 6 | \usage{ 7 | hook_step_counter( 8 | every_n_steps = 100, 9 | every_n_secs = NULL, 10 | output_dir = NULL, 11 | summary_writer = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{every_n_steps}{Run this counter every N steps} 16 | 17 | \item{every_n_secs}{Run this counter every N seconds} 18 | 19 | \item{output_dir}{The output directory} 20 | 21 | \item{summary_writer}{The summary writer} 22 | } 23 | \description{ 24 | Steps per Second Monitor 25 | } 26 | \seealso{ 27 | Other session_run_hook wrappers: 28 | \code{\link{hook_checkpoint_saver}()}, 29 | \code{\link{hook_global_step_waiter}()}, 30 | \code{\link{hook_history_saver}()}, 31 | \code{\link{hook_logging_tensor}()}, 32 | \code{\link{hook_nan_tensor}()}, 33 | \code{\link{hook_progress_bar}()}, 34 | \code{\link{hook_stop_at_step}()}, 35 | \code{\link{hook_summary_saver}()}, 36 | \code{\link{session_run_hook}()} 37 | } 38 | \concept{session_run_hook wrappers} 39 | -------------------------------------------------------------------------------- /man/hook_stop_at_step.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_stop_at_step} 4 | \alias{hook_stop_at_step} 5 | \title{Monitor to Request Stop at a Specified Step} 6 | \usage{ 7 | hook_stop_at_step(num_steps = NULL, last_step = NULL) 8 | } 9 | \arguments{ 10 | \item{num_steps}{Number of steps to execute.} 11 | 12 | \item{last_step}{Step after which to stop.} 13 | } 14 | \description{ 15 | Monitor to Request Stop at a Specified Step 16 | } 17 | \seealso{ 18 | Other session_run_hook wrappers: 19 | \code{\link{hook_checkpoint_saver}()}, 20 | \code{\link{hook_global_step_waiter}()}, 21 | \code{\link{hook_history_saver}()}, 22 | \code{\link{hook_logging_tensor}()}, 23 | \code{\link{hook_nan_tensor}()}, 24 | \code{\link{hook_progress_bar}()}, 25 | \code{\link{hook_step_counter}()}, 26 | \code{\link{hook_summary_saver}()}, 27 | \code{\link{session_run_hook}()} 28 | } 29 | \concept{session_run_hook wrappers} 30 | -------------------------------------------------------------------------------- /man/hook_summary_saver.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{hook_summary_saver} 4 | \alias{hook_summary_saver} 5 | \title{Saves Summaries Every N Steps} 6 | \usage{ 7 | hook_summary_saver( 8 | save_steps = NULL, 9 | save_secs = NULL, 10 | output_dir = NULL, 11 | summary_writer = NULL, 12 | scaffold = NULL, 13 | summary_op = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{save_steps}{An integer indicating saving summaries every N steps. Exactly one of 18 | \code{save_secs} and \code{save_steps} should be set.} 19 | 20 | \item{save_secs}{An integer indicating saving summaries every N seconds.} 21 | 22 | \item{output_dir}{The directory to save the summaries to. Only used 23 | if no \code{summary_writer} is supplied.} 24 | 25 | \item{summary_writer}{The summary writer. If \code{NULL} and an \code{output_dir} was 26 | passed, one will be created accordingly.} 27 | 28 | \item{scaffold}{A scaffold to get summary_op if it's not provided.} 29 | 30 | \item{summary_op}{A tensor of type \code{tf$string} containing the serialized 31 | summary protocol buffer or a list of tensors. They are most likely an 32 | output by TensorFlow summary methods like \code{tf$summary$scalar} or 33 | \code{tf$summary$merge_all}. It can be passed in as one tensor; if more than 34 | one, they must be passed in as a list.} 35 | } 36 | \description{ 37 | Saves Summaries Every N Steps 38 | } 39 | \seealso{ 40 | Other session_run_hook wrappers: 41 | \code{\link{hook_checkpoint_saver}()}, 42 | \code{\link{hook_global_step_waiter}()}, 43 | \code{\link{hook_history_saver}()}, 44 | \code{\link{hook_logging_tensor}()}, 45 | \code{\link{hook_nan_tensor}()}, 46 | \code{\link{hook_progress_bar}()}, 47 | \code{\link{hook_step_counter}()}, 48 | \code{\link{hook_stop_at_step}()}, 49 | \code{\link{session_run_hook}()} 50 | } 51 | \concept{session_run_hook wrappers} 52 | -------------------------------------------------------------------------------- /man/input_fn.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/input_fn.R 3 | \name{input_fn} 4 | \alias{input_fn} 5 | \alias{input_fn.default} 6 | \alias{input_fn.formula} 7 | \alias{input_fn.data.frame} 8 | \alias{input_fn.list} 9 | \alias{input_fn.matrix} 10 | \title{Construct an Input Function} 11 | \usage{ 12 | input_fn(object, ...) 13 | 14 | \method{input_fn}{default}(object, ...) 15 | 16 | \method{input_fn}{formula}(object, data, ...) 17 | 18 | \method{input_fn}{data.frame}( 19 | object, 20 | features, 21 | response = NULL, 22 | batch_size = 128, 23 | shuffle = "auto", 24 | num_epochs = 1, 25 | queue_capacity = 1000, 26 | num_threads = 1, 27 | ... 28 | ) 29 | 30 | \method{input_fn}{list}( 31 | object, 32 | features, 33 | response = NULL, 34 | batch_size = 128, 35 | shuffle = "auto", 36 | num_epochs = 1, 37 | queue_capacity = 1000, 38 | num_threads = 1, 39 | ... 40 | ) 41 | 42 | \method{input_fn}{matrix}(object, ...) 43 | } 44 | \arguments{ 45 | \item{object, data}{An 'input source' -- either a data set (e.g. an \R \code{data.frame}), 46 | or another kind of object that can provide the data required for training.} 47 | 48 | \item{...}{Optional arguments passed on to implementing submethods.} 49 | 50 | \item{features}{The names of feature variables to be used.} 51 | 52 | \item{response}{The name of the response variable.} 53 | 54 | \item{batch_size}{The batch size.} 55 | 56 | \item{shuffle}{Whether to shuffle the queue. When \code{"auto"} (the default), 57 | shuffling will be performed except when this input function is called by 58 | a \code{predict()} method.} 59 | 60 | \item{num_epochs}{The number of epochs to iterate over data.} 61 | 62 | \item{queue_capacity}{The size of queue to accumulate.} 63 | 64 | \item{num_threads}{The number of threads used for reading and enqueueing. In 65 | order to have predictable and repeatable order of reading and enqueueing, 66 | such as in prediction and evaluation mode, \code{num_threads} should be 1.} 67 | } 68 | \description{ 69 | This function constructs input function from various types of input used to 70 | feed different TensorFlow estimators. 71 | } 72 | \details{ 73 | For list objects, this method is particularly useful when constructing 74 | dynamic length of inputs for models like recurrent neural networks. Note that 75 | some arguments are not available yet for input_fn applied to list objects. 76 | See S3 method signatures below for more details. 77 | } 78 | \examples{ 79 | \dontrun{ 80 | # Construct the input function through formula interface 81 | input_fn1 <- input_fn(mpg ~ drat + cyl, mtcars) 82 | } 83 | 84 | \dontrun{ 85 | # Construct the input function from a data.frame object 86 | input_fn1 <- input_fn(mtcars, response = mpg, features = c(drat, cyl)) 87 | } 88 | 89 | \dontrun{ 90 | # Construct the input function from a list object 91 | input_fn1 <- input_fn( 92 | object = list( 93 | feature1 = list( 94 | list(list(1), list(2), list(3)), 95 | list(list(4), list(5), list(6))), 96 | feature2 = list( 97 | list(list(7), list(8), list(9)), 98 | list(list(10), list(11), list(12))), 99 | response = list( 100 | list(1, 2, 3), list(4, 5, 6))), 101 | features = c("feature1", "feature2"), 102 | response = "response", 103 | batch_size = 10L) 104 | } 105 | 106 | } 107 | \seealso{ 108 | Other input functions: 109 | \code{\link{numpy_input_fn}()} 110 | } 111 | \concept{input function constructors} 112 | \concept{input functions} 113 | -------------------------------------------------------------------------------- /man/input_layer.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/feature_columns.R 3 | \name{input_layer} 4 | \alias{input_layer} 5 | \title{Construct an Input Layer} 6 | \usage{ 7 | input_layer( 8 | features, 9 | feature_columns, 10 | weight_collections = NULL, 11 | trainable = TRUE 12 | ) 13 | } 14 | \arguments{ 15 | \item{features}{A mapping from key to tensors. Feature columns look up via 16 | these keys. For example \code{column_numeric('price')} will look at 'price' key 17 | in this dict. Values can be a sparse tensor or tensor depends on 18 | corresponding feature column.} 19 | 20 | \item{feature_columns}{An iterable containing the FeatureColumns to use as 21 | inputs to your model. All items should be instances of classes derived from 22 | a dense column such as \code{\link[=column_numeric]{column_numeric()}}, \code{\link[=column_embedding]{column_embedding()}}, 23 | \code{\link[=column_bucketized]{column_bucketized()}}, \code{\link[=column_indicator]{column_indicator()}}. If you have categorical features, 24 | you can wrap them with an \code{\link[=column_embedding]{column_embedding()}} or \code{\link[=column_indicator]{column_indicator()}}.} 25 | 26 | \item{weight_collections}{A list of collection names to which the Variable 27 | will be added. Note that, variables will also be added to collections 28 | \code{graph_keys()$GLOBAL_VARIABLES} and \code{graph_keys()$MODEL_VARIABLES}.} 29 | 30 | \item{trainable}{If \code{TRUE} also add the variable to the graph collection 31 | \code{graph_keys()$TRAINABLE_VARIABLES} (see \code{tf$Variable}).} 32 | } 33 | \value{ 34 | A tensor which represents input layer of a model. Its shape is 35 | (batch_size, first_layer_dimension) and its dtype is \code{float32}. 36 | first_layer_dimension is determined based on given \code{feature_columns}. 37 | } 38 | \description{ 39 | Returns a dense tensor as input layer based on given \code{feature_columns}. 40 | At the first layer of the model, this column oriented data should be converted 41 | to a single tensor. 42 | } 43 | \section{Raises}{ 44 | 45 | \itemize{ 46 | \item ValueError: if an item in \code{feature_columns} is not a dense column. 47 | } 48 | } 49 | 50 | \seealso{ 51 | Other feature column constructors: 52 | \code{\link{column_bucketized}()}, 53 | \code{\link{column_categorical_weighted}()}, 54 | \code{\link{column_categorical_with_hash_bucket}()}, 55 | \code{\link{column_categorical_with_identity}()}, 56 | \code{\link{column_categorical_with_vocabulary_file}()}, 57 | \code{\link{column_categorical_with_vocabulary_list}()}, 58 | \code{\link{column_crossed}()}, 59 | \code{\link{column_embedding}()}, 60 | \code{\link{column_numeric}()} 61 | } 62 | \concept{feature column constructors} 63 | -------------------------------------------------------------------------------- /man/keras_model_to_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/keras_estimator.R 3 | \name{keras_model_to_estimator} 4 | \alias{keras_model_to_estimator} 5 | \title{Keras Estimators} 6 | \usage{ 7 | keras_model_to_estimator( 8 | keras_model = NULL, 9 | keras_model_path = NULL, 10 | custom_objects = NULL, 11 | model_dir = NULL, 12 | config = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{keras_model}{A keras model.} 17 | 18 | \item{keras_model_path}{Directory to a keras model on disk.} 19 | 20 | \item{custom_objects}{Dictionary for custom objects.} 21 | 22 | \item{model_dir}{Directory to save Estimator model parameters, graph and etc.} 23 | 24 | \item{config}{Configuration object.} 25 | } 26 | \description{ 27 | Create an Estimator from a compiled Keras model 28 | } 29 | -------------------------------------------------------------------------------- /man/latest_checkpoint.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils_tf.R 3 | \name{latest_checkpoint} 4 | \alias{latest_checkpoint} 5 | \title{Get the Latest Checkpoint in a Checkpoint Directory} 6 | \usage{ 7 | latest_checkpoint(checkpoint_dir, ...) 8 | } 9 | \arguments{ 10 | \item{checkpoint_dir}{The path to the checkpoint directory.} 11 | 12 | \item{...}{Optional arguments passed on to \code{latest_checkpoint()}.} 13 | } 14 | \description{ 15 | Get the Latest Checkpoint in a Checkpoint Directory 16 | } 17 | \seealso{ 18 | Other utility functions: 19 | \code{\link{graph_keys}()} 20 | } 21 | \concept{utility functions} 22 | -------------------------------------------------------------------------------- /man/linear_estimators.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/linear_estimators.R 3 | \name{linear_estimators} 4 | \alias{linear_estimators} 5 | \alias{linear_regressor} 6 | \alias{linear_classifier} 7 | \title{Construct a Linear Estimator} 8 | \usage{ 9 | linear_regressor( 10 | feature_columns, 11 | model_dir = NULL, 12 | label_dimension = 1L, 13 | weight_column = NULL, 14 | optimizer = "Ftrl", 15 | config = NULL, 16 | partitioner = NULL 17 | ) 18 | 19 | linear_classifier( 20 | feature_columns, 21 | model_dir = NULL, 22 | n_classes = 2L, 23 | weight_column = NULL, 24 | label_vocabulary = NULL, 25 | optimizer = "Ftrl", 26 | config = NULL, 27 | partitioner = NULL 28 | ) 29 | } 30 | \arguments{ 31 | \item{feature_columns}{An \R list containing all of the feature columns used 32 | by the model (typically, generated by \code{\link[=feature_columns]{feature_columns()}}).} 33 | 34 | \item{model_dir}{Directory to save the model parameters, graph, and so on. 35 | This can also be used to load checkpoints from the directory into a 36 | estimator to continue training a previously saved model.} 37 | 38 | \item{label_dimension}{Number of regression targets per example. This is the 39 | size of the last dimension of the labels and logits \code{Tensor} objects 40 | (typically, these have shape \verb{[batch_size, label_dimension]}).} 41 | 42 | \item{weight_column}{A string, or a numeric column created by 43 | \code{\link[=column_numeric]{column_numeric()}} defining feature column representing weights. It is used 44 | to down weight or boost examples during training. It will be multiplied by 45 | the loss of the example. If it is a string, it is used as a key to fetch 46 | weight tensor from the \code{features} argument. If it is a numeric column, 47 | then the raw tensor is fetched by key \code{weight_column$key}, then 48 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 49 | 50 | \item{optimizer}{Either the name of the optimizer to be used when training 51 | the model, or a TensorFlow optimizer instance. Defaults to the FTRL 52 | optimizer.} 53 | 54 | \item{config}{A run configuration created by \code{\link[=run_config]{run_config()}}, used to configure the runtime 55 | settings.} 56 | 57 | \item{partitioner}{An optional partitioner for the input layer.} 58 | 59 | \item{n_classes}{The number of label classes.} 60 | 61 | \item{label_vocabulary}{A list of strings represents possible label values. 62 | If given, labels must be string type and have any value in 63 | \code{label_vocabulary}. If it is not given, that means labels are already 64 | encoded as integer or float within \verb{[0, 1]} for \code{n_classes == 2} and 65 | encoded as integer values in \verb{\{0, 1,..., n_classes -1\}} for \code{n_classes > 2}. Also there will be errors if vocabulary is not provided and labels are 66 | string.} 67 | } 68 | \description{ 69 | Construct a linear model, which can be used to predict a continuous outcome 70 | (in the case of \code{linear_regressor()}) or a categorical outcome (in the case 71 | of \code{linear_classifier()}). 72 | } 73 | \seealso{ 74 | Other canned estimators: 75 | \code{\link{boosted_trees_estimators}}, 76 | \code{\link{dnn_estimators}}, 77 | \code{\link{dnn_linear_combined_estimators}} 78 | } 79 | \concept{canned estimators} 80 | -------------------------------------------------------------------------------- /man/metric_keys.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/estimator_keys.R 3 | \name{metric_keys} 4 | \alias{metric_keys} 5 | \title{Canonical Metric Keys} 6 | \usage{ 7 | metric_keys() 8 | } 9 | \description{ 10 | The canonical set of keys that can be used to access metrics from canned 11 | estimators. 12 | } 13 | \examples{ 14 | \dontrun{ 15 | metrics <- metric_keys() 16 | 17 | # Get the available keys 18 | metrics 19 | 20 | metrics$ACCURACY 21 | } 22 | 23 | } 24 | \seealso{ 25 | Other estimator keys: 26 | \code{\link{mode_keys}()}, 27 | \code{\link{prediction_keys}()} 28 | } 29 | \concept{estimator keys} 30 | -------------------------------------------------------------------------------- /man/mode_keys.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/estimator_keys.R 3 | \name{mode_keys} 4 | \alias{mode_keys} 5 | \title{Canonical Mode Keys} 6 | \usage{ 7 | mode_keys() 8 | } 9 | \description{ 10 | The names for different possible modes for an estimator. The following 11 | standard keys are defined: 12 | } 13 | \details{ 14 | \tabular{ll}{ 15 | \code{TRAIN} \tab Training mode. \cr 16 | \code{EVAL} \tab Evaluation mode. \cr 17 | \code{PREDICT} \tab Prediction / inference mode. \cr 18 | } 19 | } 20 | \examples{ 21 | \dontrun{ 22 | modes <- mode_keys() 23 | modes$TRAIN 24 | } 25 | 26 | } 27 | \seealso{ 28 | Other estimator keys: 29 | \code{\link{metric_keys}()}, 30 | \code{\link{prediction_keys}()} 31 | } 32 | \concept{estimator keys} 33 | -------------------------------------------------------------------------------- /man/model_dir.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils_tf.R 3 | \name{model_dir} 4 | \alias{model_dir} 5 | \title{Model directory} 6 | \usage{ 7 | model_dir(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{Model object} 11 | 12 | \item{...}{Unused} 13 | } 14 | \description{ 15 | Get the directory where a model's artifacts are stored. 16 | } 17 | -------------------------------------------------------------------------------- /man/numpy_input_fn.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/input_fn.R 3 | \name{numpy_input_fn} 4 | \alias{numpy_input_fn} 5 | \title{Construct Input Function Containing Python Dictionaries of Numpy Arrays} 6 | \usage{ 7 | numpy_input_fn( 8 | x, 9 | y = NULL, 10 | batch_size = 128, 11 | num_epochs = 1, 12 | shuffle = NULL, 13 | queue_capacity = 1000, 14 | num_threads = 1 15 | ) 16 | } 17 | \arguments{ 18 | \item{x}{dict of numpy array object.} 19 | 20 | \item{y}{numpy array object. \code{NULL} if absent.} 21 | 22 | \item{batch_size}{Integer, size of batches to return.} 23 | 24 | \item{num_epochs}{Integer, number of epochs to iterate over data. If \code{NULL} 25 | will run forever.} 26 | 27 | \item{shuffle}{Boolean, if \code{TRUE} shuffles the queue. Avoid shuffle at 28 | prediction time.} 29 | 30 | \item{queue_capacity}{Integer, size of queue to accumulate.} 31 | 32 | \item{num_threads}{Integer, number of threads used for reading and 33 | enqueueing. In order to have predicted and repeatable order of reading and 34 | enqueueing, such as in prediction and evaluation mode, \code{num_threads} should 35 | be 1. #'} 36 | } 37 | \description{ 38 | This returns a function outputting \code{features} and \code{target} based on the dict 39 | of numpy arrays. The dict \code{features} has the same keys as the \code{x}. 40 | } 41 | \details{ 42 | Note that this function is still experimental and should only be used if 43 | necessary, e.g. feed in data that's dictionary of numpy arrays. 44 | } 45 | \section{Raises}{ 46 | ValueError: if the shape of \code{y} mismatches the shape of 47 | values in \code{x} (i.e., values in \code{x} have same shape). TypeError: \code{x} is not 48 | a dict or \code{shuffle} is not bool. 49 | } 50 | 51 | \seealso{ 52 | Other input functions: 53 | \code{\link{input_fn}()} 54 | } 55 | \concept{input functions} 56 | -------------------------------------------------------------------------------- /man/plot.tf_estimator_history.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/history.R 3 | \name{plot.tf_estimator_history} 4 | \alias{plot.tf_estimator_history} 5 | \title{Plot training history} 6 | \usage{ 7 | \method{plot}{tf_estimator_history}( 8 | x, 9 | y, 10 | metrics = NULL, 11 | method = c("auto", "ggplot2", "base"), 12 | smooth = getOption("tf.estimator.plot.history.smooth", TRUE), 13 | theme_bw = getOption("tf.estimator.plot.history.theme_bw", FALSE), 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{x}{Training history object returned from \code{train()}.} 19 | 20 | \item{y}{Unused.} 21 | 22 | \item{metrics}{One or more metrics to plot (e.g. \code{c('total_losses', 'mean_losses')}). 23 | Defaults to plotting all captured metrics.} 24 | 25 | \item{method}{Method to use for plotting. The default "auto" will use 26 | \pkg{ggplot2} if available, and otherwise will use base graphics.} 27 | 28 | \item{smooth}{Whether a loess smooth should be added to the plot, only 29 | available for the \code{ggplot2} method. If the number of data points is smaller 30 | than ten, it is forced to false.} 31 | 32 | \item{theme_bw}{Use \code{ggplot2::theme_bw()} to plot the history in 33 | black and white.} 34 | 35 | \item{...}{Additional parameters to pass to the \code{\link[=plot]{plot()}} method.} 36 | } 37 | \description{ 38 | Plots metrics recorded during training. 39 | } 40 | -------------------------------------------------------------------------------- /man/predict.tf_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{predict.tf_estimator} 4 | \alias{predict.tf_estimator} 5 | \title{Generate Predictions with an Estimator} 6 | \usage{ 7 | \method{predict}{tf_estimator}( 8 | object, 9 | input_fn, 10 | checkpoint_path = NULL, 11 | predict_keys = c("predictions", "classes", "class_ids", "logistic", "logits", 12 | "probabilities"), 13 | hooks = NULL, 14 | as_iterable = FALSE, 15 | simplify = TRUE, 16 | yield_single_examples = TRUE, 17 | ... 18 | ) 19 | } 20 | \arguments{ 21 | \item{object}{A TensorFlow estimator.} 22 | 23 | \item{input_fn}{An input function, typically generated by the \code{\link[=input_fn]{input_fn()}} 24 | helper function.} 25 | 26 | \item{checkpoint_path}{The path to a specific model checkpoint to be used for 27 | prediction. If \code{NULL} (the default), the latest checkpoint in \code{model_dir} 28 | is used.} 29 | 30 | \item{predict_keys}{The types of predictions that should be produced, as an 31 | \R list. When this argument is not specified (the default), all possible 32 | predicted values will be returned.} 33 | 34 | \item{hooks}{A list of \R functions, to be used as callbacks inside the 35 | training loop. By default, \code{hook_history_saver(every_n_step = 10)} and 36 | \code{hook_progress_bar()} will be attached if not provided to save the metrics 37 | history and create the progress bar.} 38 | 39 | \item{as_iterable}{Boolean; should a raw Python generator be returned? When 40 | \code{FALSE} (the default), the predicted values will be consumed from the 41 | generator and returned as an \R object.} 42 | 43 | \item{simplify}{Whether to simplify prediction results into a \code{tibble}, 44 | as opposed to a list. Defaults to \code{TRUE}.} 45 | 46 | \item{yield_single_examples}{(Available since TensorFlow v1.7) If \code{FALSE}, 47 | yields the whole batch as returned by the \code{model_fn} instead of decomposing 48 | the batch into individual elements. This is useful if \code{model_fn} returns some 49 | tensors with first dimension not equal to the batch size.} 50 | 51 | \item{...}{Optional arguments passed on to the estimator's \code{predict()} 52 | method.} 53 | } 54 | \description{ 55 | Generate predicted labels / values for input data provided by \code{input_fn()}. 56 | } 57 | \section{Yields}{ 58 | Evaluated values of \code{predictions} tensors. 59 | } 60 | 61 | \section{Raises}{ 62 | ValueError: Could not find a trained model in model_dir. 63 | ValueError: if batch length of predictions are not same. ValueError: If 64 | there is a conflict between \code{predict_keys} and \code{predictions}. For example 65 | if \code{predict_keys} is not \code{NULL} but \code{EstimatorSpec.predictions} is not a 66 | \code{dict}. 67 | } 68 | 69 | \seealso{ 70 | Other custom estimator methods: 71 | \code{\link{estimator_spec}()}, 72 | \code{\link{estimator}()}, 73 | \code{\link{evaluate.tf_estimator}()}, 74 | \code{\link{export_savedmodel.tf_estimator}()}, 75 | \code{\link{train.tf_estimator}()} 76 | } 77 | \concept{custom estimator methods} 78 | -------------------------------------------------------------------------------- /man/prediction_keys.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/estimator_keys.R 3 | \name{prediction_keys} 4 | \alias{prediction_keys} 5 | \title{Canonical Model Prediction Keys} 6 | \usage{ 7 | prediction_keys() 8 | } 9 | \description{ 10 | The canonical set of keys used for models and estimators that provide 11 | different types of predicted values through their \code{predict()} method. 12 | } 13 | \examples{ 14 | \dontrun{ 15 | keys <- prediction_keys() 16 | 17 | # Get the available keys 18 | keys 19 | 20 | # Key for retrieving probabilities from prediction values 21 | keys$PROBABILITIES 22 | } 23 | 24 | } 25 | \seealso{ 26 | Other estimator keys: 27 | \code{\link{metric_keys}()}, 28 | \code{\link{mode_keys}()} 29 | } 30 | \concept{estimator keys} 31 | -------------------------------------------------------------------------------- /man/reexports.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/reexport-tidyselect.R, R/reexports.R 3 | \docType{import} 4 | \name{reexports} 5 | \alias{reexports} 6 | \alias{contains} 7 | \alias{select_helpers} 8 | \alias{ends_with} 9 | \alias{everything} 10 | \alias{matches} 11 | \alias{num_range} 12 | \alias{one_of} 13 | \alias{starts_with} 14 | \alias{last_col} 15 | \alias{\%>\%} 16 | \alias{use_python} 17 | \alias{use_virtualenv} 18 | \alias{use_condaenv} 19 | \alias{array_reshape} 20 | \alias{tf} 21 | \alias{shape} 22 | \alias{install_tensorflow} 23 | \alias{tf_config} 24 | \alias{tensorboard} 25 | \alias{evaluate} 26 | \alias{train} 27 | \alias{train_and_evaluate} 28 | \alias{export_savedmodel} 29 | \alias{flags} 30 | \alias{flag_numeric} 31 | \alias{flag_integer} 32 | \alias{flag_string} 33 | \alias{flag_boolean} 34 | \alias{run_dir} 35 | \alias{glimpse} 36 | \title{Objects exported from other packages} 37 | \keyword{internal} 38 | \description{ 39 | These objects are imported from other packages. Follow the links 40 | below to see their documentation. 41 | 42 | \describe{ 43 | \item{magrittr}{\code{\link[magrittr:pipe]{\%>\%}}} 44 | 45 | \item{reticulate}{\code{\link[reticulate]{array_reshape}}, \code{\link[reticulate:use_python]{use_condaenv}}, \code{\link[reticulate]{use_python}}, \code{\link[reticulate:use_python]{use_virtualenv}}} 46 | 47 | \item{tensorflow}{\code{\link[tensorflow]{evaluate}}, \code{\link[tensorflow]{export_savedmodel}}, \code{\link[tensorflow]{install_tensorflow}}, \code{\link[tensorflow]{shape}}, \code{\link[tensorflow]{tensorboard}}, \code{\link[tensorflow]{tf}}, \code{\link[tensorflow]{tf_config}}, \code{\link[tensorflow]{train}}, \code{\link[tensorflow]{train_and_evaluate}}} 48 | 49 | \item{tfruns}{\code{\link[tfruns:flags]{flag_boolean}}, \code{\link[tfruns:flags]{flag_integer}}, \code{\link[tfruns:flags]{flag_numeric}}, \code{\link[tfruns:flags]{flag_string}}, \code{\link[tfruns]{flags}}, \code{\link[tfruns]{run_dir}}} 50 | 51 | \item{tibble}{\code{\link[tibble:reexports]{glimpse}}} 52 | 53 | \item{tidyselect}{\code{\link[tidyselect:starts_with]{contains}}, \code{\link[tidyselect:starts_with]{ends_with}}, \code{\link[tidyselect]{everything}}, \code{\link[tidyselect:everything]{last_col}}, \code{\link[tidyselect:starts_with]{matches}}, \code{\link[tidyselect:starts_with]{num_range}}, \code{\link[tidyselect]{one_of}}, \code{\link[tidyselect]{starts_with}}} 54 | }} 55 | 56 | -------------------------------------------------------------------------------- /man/regressor_parse_example_spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/parsing_utils.R 3 | \name{regressor_parse_example_spec} 4 | \alias{regressor_parse_example_spec} 5 | \title{Generates Parsing Spec for TensorFlow Example to be Used with Regressors} 6 | \usage{ 7 | regressor_parse_example_spec( 8 | feature_columns, 9 | label_key, 10 | label_dtype = tf$float32, 11 | label_default = NULL, 12 | label_dimension = 1L, 13 | weight_column = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{feature_columns}{An iterable containing all feature columns. All items 18 | should be instances of classes derived from \verb{_FeatureColumn}.} 19 | 20 | \item{label_key}{A string identifying the label. It means \code{tf$Example} stores 21 | labels with this key.} 22 | 23 | \item{label_dtype}{A \code{tf$dtype} identifies the type of labels. By default it 24 | is \code{tf$float32}.} 25 | 26 | \item{label_default}{used as label if label_key does not exist in given 27 | \code{tf$Example}. By default default_value is none, which means 28 | \code{tf$parse_example} will error out if there is any missing label.} 29 | 30 | \item{label_dimension}{Number of regression targets per example. This is the 31 | size of the last dimension of the labels and logits \code{Tensor} objects 32 | (typically, these have shape \verb{[batch_size, label_dimension]}).} 33 | 34 | \item{weight_column}{A string or a \verb{_NumericColumn} created by 35 | \code{column_numeric} defining feature column representing 36 | weights. It is used to down weight or boost examples during training. It 37 | will be multiplied by the loss of the example. If it is a string, it is 38 | used as a key to fetch weight tensor from the \code{features}. If it is a 39 | \verb{_NumericColumn}, raw tensor is fetched by key \code{weight_column$key}, then 40 | \code{weight_column$normalizer_fn} is applied on it to get weight tensor.} 41 | } 42 | \value{ 43 | A dict mapping each feature key to a \code{FixedLenFeature} or 44 | \code{VarLenFeature} value. 45 | } 46 | \description{ 47 | If users keep data in \code{tf$Example} format, they need to call \code{tf$parse_example} 48 | with a proper feature spec. There are two main things that this utility 49 | helps: 50 | \itemize{ 51 | \item Users need to combine parsing spec of features with labels and weights (if 52 | any) since they are all parsed from same \code{tf$Example} instance. This utility 53 | combines these specs. 54 | \item It is difficult to map expected label by a regressor such as \code{dnn_regressor} 55 | to corresponding \code{tf$parse_example} spec. This utility encodes it by getting 56 | related information from users (key, dtype). 57 | } 58 | } 59 | \section{Raises}{ 60 | 61 | \itemize{ 62 | \item ValueError: If label is used in \code{feature_columns}. 63 | \item ValueError: If weight_column is used in \code{feature_columns}. 64 | \item ValueError: If any of the given \code{feature_columns} is not a \verb{_FeatureColumn} instance. 65 | \item ValueError: If \code{weight_column} is not a \verb{_NumericColumn} instance. 66 | \item ValueError: if label_key is \code{NULL}. 67 | } 68 | } 69 | 70 | \seealso{ 71 | Other parsing utilities: 72 | \code{\link{classifier_parse_example_spec}()} 73 | } 74 | \concept{parsing utilities} 75 | -------------------------------------------------------------------------------- /man/run_config.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/run_config.R 3 | \name{run_config} 4 | \alias{run_config} 5 | \title{Run Configuration} 6 | \usage{ 7 | run_config() 8 | } 9 | \description{ 10 | This class specifies the configurations for an \code{Estimator} run. 11 | } 12 | \examples{ 13 | \dontrun{ 14 | config <- run_config() 15 | 16 | # Get the properties of the config 17 | names(config) 18 | 19 | # Change the mutable properties of the config 20 | config <- config$replace(tf_random_seed = 11L, save_summary_steps = 12L) 21 | 22 | # Print config as key value pairs 23 | print(config) 24 | } 25 | 26 | } 27 | \seealso{ 28 | Other run_config methods: 29 | \code{\link{task_type}()} 30 | } 31 | \concept{run_config methods} 32 | -------------------------------------------------------------------------------- /man/session_run_args.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{session_run_args} 4 | \alias{session_run_args} 5 | \title{Create Session Run Arguments} 6 | \usage{ 7 | session_run_args(...) 8 | } 9 | \arguments{ 10 | \item{...}{A set of tensors or operations.} 11 | } 12 | \description{ 13 | Create a set of session run arguments. These are used as the return values in 14 | the \code{before_run(context)} callback of a \code{\link[=session_run_hook]{session_run_hook()}}, for requesting 15 | the values of specific tensor in the \code{after_run(context, values)} callback. 16 | } 17 | \seealso{ 18 | \code{\link[=session_run_hook]{session_run_hook()}} 19 | } 20 | -------------------------------------------------------------------------------- /man/session_run_hook.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/session_run_hooks_builtin_wrappers.R 3 | \name{session_run_hook} 4 | \alias{session_run_hook} 5 | \title{Create Custom Session Run Hooks} 6 | \usage{ 7 | session_run_hook( 8 | begin = function() { }, 9 | after_create_session = function(session, coord) { }, 10 | before_run = function(context) { }, 11 | after_run = function(context, values) { }, 12 | end = function(session) { } 13 | ) 14 | } 15 | \arguments{ 16 | \item{begin}{\verb{function()}: An \R function, to be called once before using the session.} 17 | 18 | \item{after_create_session}{\verb{function(session, coord)}: An \R function, to be called 19 | once the new TensorFlow session has been created.} 20 | 21 | \item{before_run}{\verb{function(run_context)}: An \R function to be called before a run.} 22 | 23 | \item{after_run}{\verb{function(run_context, run_values)}: An \R function to be called 24 | after a run.} 25 | 26 | \item{end}{\verb{function(session)}: An \R function to be called at the end of the session. 27 | 28 | Typically, you'll want to define a \code{before_run()} hook that defines the set 29 | of tensors you're interested in for a particular run, and then you'll use the 30 | resulting values of those tensors in your \code{after_run()} hook. The tensors 31 | requested in your \code{before_run()} hook will be made available as part of the 32 | second argument in the \code{after_run()} hook (the \code{values} argument).} 33 | } 34 | \description{ 35 | Create a set of session run hooks, used to record information during 36 | training of an estimator. See \strong{Details} for more information on the 37 | various hooks that can be defined. 38 | } 39 | \seealso{ 40 | \code{\link[=session_run_args]{session_run_args()}} 41 | 42 | Other session_run_hook wrappers: 43 | \code{\link{hook_checkpoint_saver}()}, 44 | \code{\link{hook_global_step_waiter}()}, 45 | \code{\link{hook_history_saver}()}, 46 | \code{\link{hook_logging_tensor}()}, 47 | \code{\link{hook_nan_tensor}()}, 48 | \code{\link{hook_progress_bar}()}, 49 | \code{\link{hook_step_counter}()}, 50 | \code{\link{hook_stop_at_step}()}, 51 | \code{\link{hook_summary_saver}()} 52 | } 53 | \concept{session_run_hook wrappers} 54 | -------------------------------------------------------------------------------- /man/task_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/run_config.R 3 | \name{task_type} 4 | \alias{task_type} 5 | \title{Task Types} 6 | \usage{ 7 | task_type() 8 | } 9 | \description{ 10 | This constant class gives the constant strings for available task types 11 | used in \code{run_config}. 12 | } 13 | \examples{ 14 | \dontrun{ 15 | task_type()$MASTER 16 | } 17 | 18 | } 19 | \seealso{ 20 | Other run_config methods: 21 | \code{\link{run_config}()} 22 | } 23 | \concept{run_config methods} 24 | -------------------------------------------------------------------------------- /man/tfestimators.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/package.R 3 | \docType{package} 4 | \name{tfestimators} 5 | \alias{tfestimators} 6 | \title{High-level Estimator API in TensorFlow for R} 7 | \description{ 8 | This library provides an R interface to the 9 | \href{https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/estimator}{Estimator} 10 | API inside TensorFlow that's designed to streamline the process of creating, 11 | evaluating, and deploying general machine learning and deep learning models. 12 | } 13 | \details{ 14 | \href{https://www.tensorflow.org}{TensorFlow} is an open source software library 15 | for numerical computation using data flow graphs. Nodes in the graph 16 | represent mathematical operations, while the graph edges represent the 17 | multidimensional data arrays (tensors) communicated between them. The 18 | flexible architecture allows you to deploy computation to one or more CPUs or 19 | GPUs in a desktop, server, or mobile device with a single API. 20 | 21 | The \href{https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/all_symbols}{TensorFlow 22 | API} is composed of a set of Python modules that enable constructing and 23 | executing TensorFlow graphs. The tensorflow package provides access to the 24 | complete TensorFlow API from within R. 25 | 26 | For additional documentation on the tensorflow package see 27 | \href{https://tensorflow.rstudio.com}{https://tensorflow.rstudio.com} 28 | } 29 | -------------------------------------------------------------------------------- /man/train-evaluate-predict.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{train-evaluate-predict} 4 | \alias{train-evaluate-predict} 5 | \title{Base Documentation for train, evaluate, and predict.} 6 | \arguments{ 7 | \item{input_fn}{An input function, typically generated by the \code{\link[=input_fn]{input_fn()}} 8 | helper function.} 9 | 10 | \item{hooks}{A list of \R functions, to be used as callbacks inside the 11 | training loop. By default, \code{hook_history_saver(every_n_step = 10)} and 12 | \code{hook_progress_bar()} will be attached if not provided to save the metrics 13 | history and create the progress bar.} 14 | 15 | \item{checkpoint_path}{The path to a specific model checkpoint to be used for 16 | prediction. If \code{NULL} (the default), the latest checkpoint in \code{model_dir} 17 | is used.} 18 | } 19 | \description{ 20 | Base Documentation for train, evaluate, and predict. 21 | } 22 | -------------------------------------------------------------------------------- /man/train.tf_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{train.tf_estimator} 4 | \alias{train.tf_estimator} 5 | \title{Train an Estimator} 6 | \usage{ 7 | \method{train}{tf_estimator}( 8 | object, 9 | input_fn, 10 | steps = NULL, 11 | hooks = NULL, 12 | max_steps = NULL, 13 | saving_listeners = NULL, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{object}{A TensorFlow estimator.} 19 | 20 | \item{input_fn}{An input function, typically generated by the \code{\link[=input_fn]{input_fn()}} 21 | helper function.} 22 | 23 | \item{steps}{The number of steps for which the model should be trained on 24 | this particular \code{train()} invocation. If \code{NULL} (the default), this 25 | function will either train forever, or until the supplied \code{input_fn()} has 26 | provided all available data.} 27 | 28 | \item{hooks}{A list of \R functions, to be used as callbacks inside the 29 | training loop. By default, \code{hook_history_saver(every_n_step = 10)} and 30 | \code{hook_progress_bar()} will be attached if not provided to save the metrics 31 | history and create the progress bar.} 32 | 33 | \item{max_steps}{The total number of steps for which the model should be 34 | trained. If set, \code{steps} must be \code{NULL}. If the estimator has already been 35 | trained a total of \code{max_steps} times, then no training will be performed.} 36 | 37 | \item{saving_listeners}{(Available since TensorFlow v1.4) A list of 38 | \code{CheckpointSaverListener} objects used for callbacks that run immediately 39 | before or after checkpoint savings.} 40 | 41 | \item{...}{Optional arguments, passed on to the estimator's \code{train()} method.} 42 | } 43 | \value{ 44 | A data.frame of the training loss history. 45 | } 46 | \description{ 47 | Train an estimator on a set of input data provides by the \code{input_fn()}. 48 | } 49 | \seealso{ 50 | Other custom estimator methods: 51 | \code{\link{estimator_spec}()}, 52 | \code{\link{estimator}()}, 53 | \code{\link{evaluate.tf_estimator}()}, 54 | \code{\link{export_savedmodel.tf_estimator}()}, 55 | \code{\link{predict.tf_estimator}()} 56 | } 57 | \concept{custom estimator methods} 58 | -------------------------------------------------------------------------------- /man/train_and_evaluate.tf_estimator.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/train_and_evaluate.R 3 | \name{train_and_evaluate.tf_estimator} 4 | \alias{train_and_evaluate.tf_estimator} 5 | \title{Train and evaluate the estimator.} 6 | \usage{ 7 | \method{train_and_evaluate}{tf_estimator}(object, train_spec, eval_spec, ...) 8 | } 9 | \arguments{ 10 | \item{object}{An estimator object to train and evaluate.} 11 | 12 | \item{train_spec}{A \code{TrainSpec} instance to specify the training specification.} 13 | 14 | \item{eval_spec}{A \code{EvalSpec} instance to specify the evaluation and export specification.} 15 | 16 | \item{...}{Not used.} 17 | } 18 | \description{ 19 | (Available since TensorFlow v1.4) 20 | } 21 | \details{ 22 | This utility function trains, evaluates, and (optionally) exports the model by 23 | using the given \code{estimator}. All training related specification is held in 24 | \code{train_spec}, including training \code{input_fn} and training max steps, etc. All 25 | evaluation and export related specification is held in \code{eval_spec}, including 26 | evaluation \code{input_fn}, steps, etc. 27 | 28 | This utility function provides consistent behavior for both local 29 | (non-distributed) and distributed configurations. Currently, the only 30 | supported distributed training configuration is between-graph replication. 31 | 32 | Overfitting: In order to avoid overfitting, it is recommended to set up the 33 | training \code{input_fn} to shuffle the training data properly. It is also 34 | recommended to train the model a little longer, say multiple epochs, before 35 | performing evaluation, as the input pipeline starts from scratch for each 36 | training. It is particularly important for local training and evaluation. 37 | 38 | Stop condition: In order to support both distributed and non-distributed 39 | configuration reliably, the only supported stop condition for model 40 | training is \code{train_spec.max_steps}. If \code{train_spec.max_steps} is \code{NULL}, the 41 | model is trained forever. \emph{Use with care} if model stop condition is 42 | different. For example, assume that the model is expected to be trained with 43 | one epoch of training data, and the training \code{input_fn} is configured to throw 44 | \code{OutOfRangeError} after going through one epoch, which stops the 45 | \code{Estimator.train}. For a three-training-worker distributed configuration, each 46 | training worker is likely to go through the whole epoch independently. So, the 47 | model will be trained with three epochs of training data instead of one epoch. 48 | } 49 | \section{Raises}{ 50 | 51 | \itemize{ 52 | \item ValueError: if environment variable \code{TF_CONFIG} is incorrectly set. 53 | } 54 | } 55 | 56 | \seealso{ 57 | Other training methods: 58 | \code{\link{eval_spec}()}, 59 | \code{\link{train_spec}()} 60 | } 61 | \concept{training methods} 62 | -------------------------------------------------------------------------------- /man/train_spec.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/train_and_evaluate.R 3 | \name{train_spec} 4 | \alias{train_spec} 5 | \title{Configuration for the train component of \code{train_and_evaluate}} 6 | \usage{ 7 | train_spec(input_fn, max_steps = NULL, hooks = NULL) 8 | } 9 | \arguments{ 10 | \item{input_fn}{Training input function returning a tuple of: 11 | \itemize{ 12 | \item features - \code{Tensor} or dictionary of string feature name to \code{Tensor}. 13 | \item labels - \code{Tensor} or dictionary of \code{Tensor} with labels. 14 | }} 15 | 16 | \item{max_steps}{Positive number of total steps for which to train model. 17 | If \code{NULL}, train forever. The training \code{input_fn} is not expected to 18 | generate \code{OutOfRangeError} or \code{StopIteration} exceptions.} 19 | 20 | \item{hooks}{List of session run hooks to run on all workers 21 | (including chief) during training.} 22 | } 23 | \description{ 24 | \code{TrainSpec} determines the input data for the training, as well as the 25 | duration. Optional hooks run at various stages of training. 26 | } 27 | \seealso{ 28 | Other training methods: 29 | \code{\link{eval_spec}()}, 30 | \code{\link{train_and_evaluate.tf_estimator}()} 31 | } 32 | \concept{training methods} 33 | -------------------------------------------------------------------------------- /man/variable_names_values.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tf_estimator.R 3 | \name{variable_names_values} 4 | \alias{variable_names_values} 5 | \alias{variable_names} 6 | \alias{variable_value} 7 | \title{Get variable names and values associated with an estimator} 8 | \usage{ 9 | variable_names(object) 10 | 11 | variable_value(object, variable = NULL) 12 | } 13 | \arguments{ 14 | \item{object}{A trained estimator model.} 15 | 16 | \item{variable}{(Optional) Names of variables to extract as a character vector. If not specified, values for all variables are returned.} 17 | } 18 | \value{ 19 | For \code{variable_names()}, a vector of variable names. For \code{variable_values()}, a named list of variable values. 20 | } 21 | \description{ 22 | These helper functions extract the names and values of variables 23 | in the graphs associated with trained estimator models. 24 | } 25 | -------------------------------------------------------------------------------- /pkgdown/_pkgdown.yml: -------------------------------------------------------------------------------- 1 | template: 2 | params: 3 | bootswatch: cosmo 4 | 5 | navbar: 6 | title: "tfestimators" 7 | type: inverse 8 | left: 9 | - text: "Home" 10 | href: index.html 11 | - text: "Using" 12 | menu: 13 | - text: "Getting Started" 14 | - text: "Estimator Basics" 15 | href: articles/estimator_basics.html 16 | - text: "Input Functions" 17 | href: articles/input_functions.html 18 | - text: "Feature Columns" 19 | href: articles/feature_columns.html 20 | - text: "Dataset API" 21 | href: articles/dataset_api.html 22 | - text: "--------------------------" 23 | - text: "Advanced" 24 | - text: "Run Hooks" 25 | href: articles/run_hooks.html 26 | - text: "Custom Estimators" 27 | href: articles/creating_estimators.html 28 | - text: "TensorFlow Layers" 29 | href: articles/layers.html 30 | - text: "TensorBoard Visualization" 31 | href: articles/tensorboard.html 32 | - text: "Parsing Utilities" 33 | href: articles/parsing_spec.html 34 | # - text: "Deployment" 35 | # href: articles/deployment.html 36 | - text: "Examples" 37 | href: articles/examples/index.html 38 | - text: "Reference" 39 | href: reference/index.html 40 | right: 41 | - icon: fa-github 42 | href: https://github.com/rstudio/tfestimators 43 | 44 | reference: 45 | 46 | - title: "Canned Estimators" 47 | contents: 48 | - linear_regressor 49 | - linear_classifier 50 | - dnn_regressor 51 | - dnn_classifier 52 | - dnn_linear_combined_regressor 53 | - dnn_linear_combined_classifier 54 | - boosted_trees_regressor 55 | - boosted_trees_classifier 56 | 57 | - title: "Estimator Methods" 58 | contents: 59 | - train.tf_estimator 60 | - predict.tf_estimator 61 | - evaluate.tf_estimator 62 | - export_savedmodel.tf_estimator 63 | 64 | - title: "Input Function" 65 | contents: 66 | - input_fn 67 | - numpy_input_fn 68 | 69 | - title: "Feature Columns" 70 | contents: 71 | - feature_columns 72 | - input_layer 73 | - column_categorical_with_vocabulary_list 74 | - column_categorical_with_vocabulary_file 75 | - column_categorical_with_identity 76 | - column_categorical_with_hash_bucket 77 | - column_categorical_weighted 78 | - column_indicator 79 | - column_numeric 80 | - column_embedding 81 | - column_crossed 82 | - column_bucketized 83 | 84 | - title: "Custom Estimators" 85 | contents: 86 | - estimator 87 | - estimator_spec 88 | 89 | - title: "Run Hooks" 90 | contents: 91 | - hook_checkpoint_saver 92 | - hook_global_step_waiter 93 | - hook_history_saver 94 | - hook_logging_tensor 95 | - hook_nan_tensor 96 | - hook_progress_bar 97 | - hook_step_counter 98 | - hook_stop_at_step 99 | - hook_summary_saver 100 | - session_run_hook 101 | 102 | - title: "Run Configuration" 103 | contents: 104 | - run_config 105 | - task_type 106 | 107 | - title: "Estimator Keys" 108 | contents: 109 | - mode_keys 110 | - metric_keys 111 | - prediction_keys 112 | 113 | - title: "Parsing Utilities" 114 | contents: 115 | - regressor_parse_example_spec 116 | - classifier_parse_example_spec 117 | 118 | - title: "Other Utilities" 119 | contents: 120 | - graph_keys 121 | - latest_checkpoint 122 | -------------------------------------------------------------------------------- /pkgdown/extra.css: -------------------------------------------------------------------------------- 1 | 2 | h4.date, 3 | h4.author { 4 | display: none; 5 | } 6 | 7 | h2.hasAnchor { 8 | font-weight: 350; 9 | } 10 | 11 | .ref-index tbody { 12 | margin-bottom: 60px; 13 | } 14 | 15 | pre:not([class]) { 16 | background-color: white; 17 | } 18 | 19 | .contents a { 20 | text-decoration: none; 21 | } 22 | 23 | blockquote { 24 | font-size: inherit; 25 | } 26 | 27 | .examples .page-header { 28 | border-bottom: none; 29 | margin: 0; 30 | padding-bottom: 0; 31 | } 32 | 33 | .examples .sourceCode { 34 | margin-top: 25px; 35 | } 36 | 37 | #sidebar .nav>li>a { 38 | padding-top: 1px; 39 | padding-bottom: 2px; 40 | } 41 | 42 | #installation .sourceCode { 43 | font-size: 13px; 44 | } 45 | 46 | .r-plot { 47 | margin-bottom: 25px; 48 | } 49 | 50 | .screenshot { 51 | margin-bottom: 20px; 52 | } 53 | 54 | .source-ref { 55 | margin-bottom: 20px; 56 | } 57 | 58 | .source-ref .caption { 59 | display: none; 60 | } 61 | 62 | 63 | 64 | .alert-warning { 65 | color: #8a6d3b; 66 | background-color: #fcf8e3; 67 | padding: 15px; 68 | margin-top: 20px; 69 | margin-bottom: 20px; 70 | background-image: linear-gradient(to bottom,#fcf8e3 0,#f8efc0 100%); 71 | filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffcf8e3', endColorstr='#fff8efc0', GradientType=0); 72 | background-repeat: repeat-x; 73 | border: 1px solid #f5e79e; 74 | border-radius: 4px; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /pkgdown/extra.js: -------------------------------------------------------------------------------- 1 | 2 | $(document).ready(function() { 3 | 4 | // turn functions section into ref-table 5 | $('#functions').find('table').attr('class', 'ref-index'); 6 | 7 | // are we in examples? 8 | var examples = window.location.href.match("/articles/examples/") !== null; 9 | if (examples) { 10 | $('.template-vignette').addClass('examples'); 11 | 12 | // remove right column 13 | $(".col-md-9").removeClass("col-md-9").addClass('col-md-10'); 14 | $(".col-md-3").remove(); 15 | 16 | } 17 | }); 18 | -------------------------------------------------------------------------------- /scripts/travis_install.sh: -------------------------------------------------------------------------------- 1 | pip2.7 install --upgrade --ignore-installed --user travis virtualenv 2 | 3 | echo "Installing TensorFlow v$TF_VERSION..." 4 | Rscript -e 'tensorflow::install_tensorflow(version = Sys.getenv("TF_VERSION"))'; 5 | 6 | mkdir inst/examples 7 | cp -R vignettes/examples/* inst/examples 8 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | library(testthat) 4 | library(tfestimators) 5 | 6 | if (identical(Sys.getenv("NOT_CRAN"), "true")) 7 | test_check("tfestimators") 8 | 9 | -------------------------------------------------------------------------------- /tests/testthat/test-boosted-trees-estimators.R: -------------------------------------------------------------------------------- 1 | context("Testing boosted trees estimators") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("boosted_trees_regressor() runs successfully", { 6 | skip_if_tensorflow_below("1.8.0") 7 | specs <- mtcars_regression_specs() 8 | 9 | estimator <- boosted_trees_regressor( 10 | feature_columns = specs$bucketized_columns, 11 | n_batches_per_layer = 1) 12 | estimator %>% train(input_fn = specs$input_fn) 13 | 14 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 15 | expect_equal(length(predictions), 32) 16 | }) 17 | 18 | 19 | test_succeeds("boosted_trees_classifier() runs successfully", { 20 | skip_if_tensorflow_below("1.8.0") 21 | specs <- mtcars_classification_specs() 22 | 23 | estimator <- boosted_trees_classifier( 24 | feature_columns = specs$bucketized_columns, 25 | n_batches_per_layer = 1) 26 | estimator %>% train(input_fn = specs$input_fn) 27 | 28 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 29 | expect_equal(length(predictions), 32) 30 | }) 31 | 32 | test_succeeds("boosted_trees_classifier() runs successfully with integer labels", { 33 | skip_if_tensorflow_below("1.9.0") 34 | specs <- mtcars_classification_specs() 35 | 36 | estimator <- boosted_trees_classifier( 37 | feature_columns = specs$bucketized_columns, 38 | n_batches_per_layer = 1) 39 | estimator %>% train(input_fn = specs$input_fn_integer_response) 40 | 41 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 42 | expect_equal(length(predictions), 32) 43 | }) 44 | -------------------------------------------------------------------------------- /tests/testthat/test-dnn_linear_combined-estimators.R: -------------------------------------------------------------------------------- 1 | context("Testing linear dnn combined estimators") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("linear_dnn_combined_regressor() runs successfully", { 6 | 7 | specs <- mtcars_regression_specs() 8 | reg <- 9 | dnn_linear_combined_regressor( 10 | linear_feature_columns = specs$linear_feature_columns, 11 | dnn_feature_columns = specs$dnn_feature_columns, 12 | dnn_hidden_units = c(1L, 1L), 13 | dnn_optimizer = "Adagrad" 14 | ) 15 | reg %>% train(input_fn = specs$input_fn) 16 | 17 | predictions <- predict(reg, input_fn = specs$input_fn, simplify = FALSE) 18 | expect_equal(length(predictions), 32) 19 | }) 20 | 21 | test_succeeds("linear_dnn_combined_classifier() runs successfully", { 22 | 23 | specs <- mtcars_classification_specs() 24 | clf <- 25 | dnn_linear_combined_classifier( 26 | linear_feature_columns = specs$linear_feature_columns, 27 | dnn_feature_columns = specs$dnn_feature_columns, 28 | dnn_hidden_units = c(3L, 3L), 29 | dnn_optimizer = "Adagrad" 30 | ) 31 | clf %>% train(input_fn = specs$input_fn) 32 | 33 | predictions <- predict(clf, input_fn = specs$input_fn, simplify = FALSE) 34 | expect_equal(length(predictions), 32) 35 | }) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-estimator-utils.R: -------------------------------------------------------------------------------- 1 | context("Testing variable name and value extraction") 2 | 3 | source("helper-utils.R") 4 | 5 | if (have_tensorflow()) { 6 | mtcars_input_fn <- function(data) { 7 | input_fn(data, 8 | features = c("disp", "cyl"), 9 | response = "mpg", 10 | batch_size = 32) 11 | } 12 | cols <- feature_columns( 13 | column_numeric("disp", "cyl") 14 | ) 15 | model <- linear_regressor(feature_columns = cols) 16 | } 17 | 18 | test_succeeds("variable_names() error on untrained model", { 19 | expect_error(variable_names(model), 20 | "'variable_names\\(\\)' must be called on a trained model") 21 | }) 22 | 23 | test_succeeds("variable_value() error on untrained model", { 24 | expect_error(variable_value(model), 25 | "'variable_value\\(\\)' must be called on a trained model") 26 | }) 27 | 28 | if (have_tensorflow()) 29 | model %>% train(mtcars_input_fn(mtcars)) 30 | 31 | test_succeeds("variable_names() works properly", { 32 | expect_identical(variable_names(model)[[1]], "global_step") 33 | }) 34 | 35 | test_succeeds("variable_value() works properly", { 36 | expect_identical(variable_value(model, "global_step"), 37 | list(global_step = 1)) 38 | expect_identical(variable_value(model) %>% 39 | names(), 40 | variable_names(model)) 41 | }) 42 | 43 | test_succeeds("variable_value() errors when variable isn't found", { 44 | expect_error(variable_value(model, "foo"), 45 | "Variable not found: foo") 46 | }) 47 | -------------------------------------------------------------------------------- /tests/testthat/test-examples.R: -------------------------------------------------------------------------------- 1 | context("Testing examples") 2 | 3 | source("helper-utils.R") 4 | 5 | # some helpers 6 | run_example <- function(example_path) { 7 | env <- new.env() 8 | capture.output({ 9 | example_path <- system.file("examples", example_path, package = "tfestimators") 10 | old_wd <- setwd(dirname(example_path)) 11 | on.exit(setwd(old_wd), add = TRUE) 12 | source(basename(example_path), local = env) 13 | }, type = "output") 14 | 15 | rm(list = ls(env), envir = env) 16 | gc() 17 | } 18 | 19 | examples <- NULL 20 | # examples <- if (TRUE) { 21 | # c( 22 | # "tensorflow_layers.R", 23 | # "custom_estimator.R", 24 | # "iris_custom_decay_dnn.R" 25 | # ) 26 | # } 27 | 28 | if (!is.null(examples)) { 29 | for (example in examples) { 30 | test_succeeds(paste(example, "example runs successfully"), { 31 | skip_if_no_tensorflow() 32 | expect_error(run_example(example), NA) 33 | }) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/testthat/test-feature-columns.R: -------------------------------------------------------------------------------- 1 | context("Testing feature columns") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("feature columns can be constructed correctly", { 6 | 7 | fcs <- feature_columns(column_numeric("drat")) 8 | expect_equal(length(fcs), 1) 9 | expect_true(grepl("NumericColumn", class(fcs[[1]])[1])) 10 | fcs <- feature_columns(column_numeric("drat", "cyl")) 11 | expect_equal(length(fcs), 2) 12 | expect_true(grepl("NumericColumn", class(fcs[[1]])[1])) 13 | }) 14 | 15 | test_succeeds("feature columns can be constructed with (cond) ~ (op) syntax", { 16 | 17 | names <- do.call(paste0, expand.grid(letters, 0:9)) 18 | 19 | # bare calls (no extra arguments to column function) 20 | mild <- feature_columns( 21 | column_numeric(starts_with("a")), 22 | names = names 23 | ) 24 | 25 | spicy <- feature_columns( 26 | starts_with("a") ~ column_numeric(), 27 | names = names 28 | ) 29 | 30 | expect_equal(mild, spicy) 31 | 32 | # extra arguments to 'column_numeric()' 33 | mild <- feature_columns( 34 | column_numeric(starts_with("a"), shape = 1L), 35 | names = names 36 | ) 37 | 38 | spicy <- feature_columns( 39 | starts_with("a") ~ column_numeric(shape = 1L), 40 | names = names 41 | ) 42 | 43 | expect_equal(mild, spicy) 44 | 45 | }) 46 | 47 | test_succeeds("duplicates columns are dropped", { 48 | 49 | names <- c("aa") 50 | columns <- feature_columns( 51 | column_numeric(starts_with("a")), 52 | column_numeric(ends_with("a")), 53 | names = names 54 | ) 55 | 56 | expect_true(length(columns) == 1) 57 | }) 58 | -------------------------------------------------------------------------------- /tests/testthat/test-formulas.R: -------------------------------------------------------------------------------- 1 | context("Testing formulas") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("parse_formula parses formula correct", { 6 | parsed <- parse_formula(y ~ tf$Tensor(x)) 7 | expect_equal(parsed$features, "tf$Tensor(x)") 8 | expect_equal(parsed$response, "y") 9 | expect_equal(parsed$intercept, TRUE) 10 | 11 | parsed <- parse_formula(y ~ tf$Tensor(x) + x1) 12 | expect_equal(parsed$features, c("tf$Tensor(x)", "x1")) 13 | }) 14 | -------------------------------------------------------------------------------- /tests/testthat/test-hooks.R: -------------------------------------------------------------------------------- 1 | context("Testing hooks") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("Hooks works with linear_regressor", { 6 | specs <- mtcars_regression_specs() 7 | 8 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 9 | lr %>% train( 10 | input_fn = specs$input_fn, 11 | steps = 10, 12 | hooks = hook_logging_tensor( 13 | tensors = list("global_step"), 14 | every_n_iter = 2)) 15 | 16 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 17 | lr %>% train( 18 | input_fn = specs$input_fn, 19 | steps = 10, 20 | hooks = hook_checkpoint_saver( 21 | checkpoint_dir = "/tmp/ckpt_dir", 22 | save_secs = 2)) 23 | expect_true(length(list.files("/tmp/ckpt_dir")) > 1) 24 | }) 25 | 26 | test_succeeds("Custom hooks work with linear_regressor", { 27 | specs <- mtcars_regression_specs() 28 | custom_hook <- session_run_hook( 29 | end = function(session) { 30 | cat(expected_output) 31 | } 32 | ) 33 | 34 | expected_output <- "Running custom session run hook at the end of a session" 35 | 36 | actual_output <- capture.output( 37 | linear_regressor( 38 | feature_columns = specs$linear_feature_columns 39 | ) %>% train( 40 | input_fn = specs$input_fn, 41 | steps = 10, 42 | hooks = custom_hook) 43 | ) 44 | expect_equal(actual_output, expected_output) 45 | }) 46 | 47 | test_succeeds("Built-in Custom Hook works with linear_regressor", { 48 | specs <- mtcars_regression_specs() 49 | 50 | # Test hook_progress_bar 51 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 52 | training_history <- lr %>% train( 53 | input_fn = specs$input_fn, 54 | steps = 2, 55 | hooks = list( 56 | hook_progress_bar() 57 | )) 58 | lr %>% evaluate( 59 | input_fn = specs$input_fn, 60 | steps = 2, 61 | hooks = list( 62 | hook_progress_bar())) 63 | 64 | # Test hook_history_saver 65 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 66 | training_history <- lr %>% train( 67 | input_fn = specs$input_fn, 68 | steps = 10, 69 | hooks = list( 70 | hook_history_saver(every_n_step = 2) 71 | )) 72 | lr %>% evaluate( 73 | input_fn = specs$input_fn, 74 | steps = 10, 75 | hooks = list( 76 | hook_history_saver(every_n_step = 2))) 77 | # verify history is saved for both training and evaluation 78 | expect_equal( 79 | lapply(tfestimators:::.globals$history, function(x) dim(as.data.frame(x))), 80 | list(train = c(6, 3), eval = c(6, 3)) 81 | ) 82 | expect_equal(dim(as.data.frame(training_history)), c(12, 3)) 83 | 84 | # Test whether default hooks are attached successfully without any hooks specified 85 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 86 | training_history <- lr %>% train( 87 | input_fn = specs$input_fn, 88 | steps = 2) 89 | 90 | # Test whether default hooks are attached successfully with wrapper hooks 91 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 92 | training_history <- lr %>% train( 93 | input_fn = specs$input_fn, 94 | steps = 2, 95 | hooks = list( 96 | hook_logging_tensor( 97 | tensors = list("global_step"), 98 | every_n_iter = 2), 99 | hook_checkpoint_saver( 100 | checkpoint_dir = "/tmp/ckpt_dir", 101 | save_secs = 2))) 102 | # Test whether default hooks are attached successfully with wrapper hook and built-in custom hook 103 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 104 | training_history <- lr %>% train( 105 | input_fn = specs$input_fn, 106 | steps = 2, 107 | hooks = list( 108 | hook_logging_tensor( 109 | tensors = list("global_step"), 110 | every_n_iter = 2), 111 | hook_history_saver(every_n_step = 2))) 112 | }) 113 | 114 | test_succeeds("First step of training is always saved in default history saver", { 115 | specs <- mtcars_regression_specs() 116 | lr <- linear_regressor(feature_columns = specs$linear_feature_columns) 117 | training_history <- lr %>% train( 118 | input_fn = specs$input_fn, 119 | steps = 1) 120 | expect_equal(dim(as.data.frame(training_history)), 121 | c(2, 3)) 122 | }) 123 | -------------------------------------------------------------------------------- /tests/testthat/test-linear-estimators.R: -------------------------------------------------------------------------------- 1 | context("Testing linear estimators") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("linear_regressor() runs successfully", { 6 | specs <- mtcars_regression_specs() 7 | 8 | estimator <- linear_regressor(feature_columns = specs$linear_feature_columns) 9 | estimator %>% train(input_fn = specs$input_fn, steps = 2) 10 | 11 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 12 | expect_equal(length(predictions), 32) 13 | }) 14 | 15 | 16 | test_succeeds("linear_classifier() runs successfully", { 17 | specs <- mtcars_classification_specs() 18 | 19 | estimator <- linear_classifier(feature_columns = specs$linear_feature_columns) 20 | estimator %>% train(input_fn = specs$input_fn, steps = 2) 21 | 22 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 23 | expect_equal(length(predictions), 32) 24 | }) 25 | -------------------------------------------------------------------------------- /tests/testthat/test-save.R: -------------------------------------------------------------------------------- 1 | context("Testing model save") 2 | 3 | source("helper-utils.R") 4 | 5 | check_contents <- function(path) { 6 | dir_contents <- dir(path, recursive = TRUE) 7 | 8 | expect_true(any(grepl("saved_model\\.pb", dir_contents))) 9 | expect_true(any(grepl("variables\\.data", dir_contents))) 10 | expect_true(any(grepl("variables\\.index", dir_contents))) 11 | 12 | unlink(path, recursive = TRUE) 13 | } 14 | 15 | export_test_savedmodel <- function(model) { 16 | temp_path <- file.path(tempfile(), "testthat-save") 17 | if (dir.exists(temp_path)) unlink(temp_path, recursive = TRUE) 18 | 19 | export_savedmodel(model, temp_path, overwrite = FALSE) 20 | 21 | check_contents(temp_path) 22 | } 23 | 24 | test_succeeds("export_savedmodel() runs successfully for linear_regressor", { 25 | specs <- mtcars_regression_specs() 26 | 27 | model <- linear_regressor(feature_columns = specs$linear_feature_columns) 28 | model %>% train(input_fn = specs$input_fn, steps = 2) 29 | 30 | export_test_savedmodel(model) 31 | }) 32 | 33 | test_succeeds("export_savedmodel() runs successfully for dnn_linear_combined_regressor", { 34 | specs <- mtcars_regression_specs() 35 | 36 | model <- dnn_linear_combined_regressor( 37 | linear_feature_columns = specs$linear_feature_columns, 38 | dnn_feature_columns = specs$dnn_feature_columns, 39 | dnn_hidden_units = c(3, 3)) 40 | model %>% train(input_fn = specs$input_fn, steps = 2) 41 | 42 | export_test_savedmodel(model) 43 | }) 44 | 45 | test_succeeds("export_savedmodel() runs successfully for dnn_regressor", { 46 | specs <- mtcars_regression_specs() 47 | 48 | model <- dnn_regressor( 49 | hidden_units = c(3,3), 50 | feature_columns = specs$linear_feature_columns 51 | ) 52 | model %>% train(input_fn = specs$input_fn, steps = 2) 53 | 54 | export_test_savedmodel(model) 55 | }) 56 | 57 | test_succeeds("export_savedmodel() runs successfully for linear_classifier", { 58 | specs <- mtcars_classification_specs() 59 | 60 | model <- linear_classifier(feature_columns = specs$linear_feature_columns) 61 | model %>% train(input_fn = specs$input_fn, steps = 2) 62 | 63 | export_test_savedmodel(model) 64 | }) 65 | 66 | test_succeeds("export_savedmodel() runs successfully for dnn_linear_combined_classifier", { 67 | specs <- mtcars_classification_specs() 68 | 69 | model <- dnn_linear_combined_classifier( 70 | linear_feature_columns = specs$linear_feature_columns, 71 | dnn_feature_columns = specs$dnn_feature_columns, 72 | dnn_hidden_units = c(3, 3)) 73 | model %>% train(input_fn = specs$input_fn, steps = 2) 74 | 75 | export_test_savedmodel(model) 76 | }) 77 | 78 | test_succeeds("export_savedmodel() runs successfully for dnn_classifier", { 79 | specs <- mtcars_classification_specs() 80 | 81 | model <- dnn_classifier( 82 | hidden_units = c(3,3), 83 | feature_columns = specs$linear_feature_columns 84 | ) 85 | model %>% train(input_fn = specs$input_fn, steps = 2) 86 | 87 | export_test_savedmodel(model) 88 | }) 89 | -------------------------------------------------------------------------------- /tests/testthat/test-tf-custom-models.R: -------------------------------------------------------------------------------- 1 | context("Testing tf_custom_models methods") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("custom model works on iris data", { 6 | 7 | constructed_input_fn <- input_fn( 8 | object = iris, 9 | response = "Species", 10 | features = c( 11 | "Sepal.Length", 12 | "Sepal.Width", 13 | "Petal.Length", 14 | "Petal.Width"), 15 | batch_size = 10L 16 | ) 17 | 18 | tmp_dir <- tempfile() 19 | 20 | # training 21 | classifier <- estimator(model_fn = simple_custom_model_fn, model_dir = tmp_dir) 22 | classifier %>% train(input_fn = constructed_input_fn, steps = 2L) 23 | 24 | # check whether tensorboard works with custom estimator 25 | # tensorboard(log_dir = tmp_dir, launch_browser = FALSE) 26 | 27 | # predictions simplified 28 | predictions <- predict(classifier, input_fn = constructed_input_fn, simplify = TRUE) 29 | expect_equal(dim(predictions), c(150, 2)) 30 | # predictions not simplified 31 | predictions <- predict(classifier, input_fn = constructed_input_fn, simplify = FALSE) 32 | expect_equal(length(predictions), 150) 33 | 34 | # extract predicted classes 35 | predicted_classes <- unlist(lapply(predictions, function(prediction) { 36 | prediction$class 37 | })) 38 | expect_equal(length(predicted_classes), 150) 39 | 40 | # extract predicted probabilities 41 | predicted_probs <- lapply(predictions, function(prediction) { 42 | prediction$prob 43 | }) 44 | expect_equal(length(predicted_probs), 150) 45 | expect_equal(length(unlist(predicted_probs)), 150 * length(unique(iris$Species))) 46 | expect_lte(max(unlist(predicted_probs)), 1) 47 | expect_gte(min(unlist(predicted_probs)), 0) 48 | # each row of probability should sum to 1 49 | expect_equal(lapply(predictions, function(pred) sum(pred$prob)), rep(list(1), length(predictions))) 50 | 51 | # evaluate 52 | expect_equal(names(evaluate(classifier, constructed_input_fn, steps = 2L, simplify = FALSE)), 53 | c("loss", "global_step")) 54 | }) 55 | -------------------------------------------------------------------------------- /tests/testthat/test-tf-models.R: -------------------------------------------------------------------------------- 1 | context("Testing tf models") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("train(), predict(), and evaluate() work for regressors", { 6 | 7 | specs <- mtcars_regression_specs() 8 | 9 | estimator <- dnn_linear_combined_regressor( 10 | linear_feature_columns = specs$linear_feature_columns, 11 | dnn_feature_columns = specs$dnn_feature_columns, 12 | dnn_hidden_units = c(1L, 1L), 13 | dnn_optimizer = "Adagrad" 14 | ) 15 | 16 | train(estimator, input_fn = specs$input_fn, steps = 20) 17 | 18 | variable_values <- variable_value(estimator) 19 | expect_gt(length(variable_values), 0) 20 | 21 | predictions <- predict(estimator, input_fn = specs$input_fn, simplify = FALSE) 22 | expect_equal(length(predictions), 32) 23 | 24 | loss <- evaluate(estimator, input_fn = specs$input_fn)$loss 25 | expect_lte(loss, 6000) 26 | }) 27 | 28 | test_succeeds("train(), predict(), and evaluate() work for classifiers", { 29 | 30 | specs <- mtcars_classification_specs() 31 | 32 | tmp_dir <- tempfile() 33 | clf <- dnn_linear_combined_classifier( 34 | linear_feature_columns = specs$linear_feature_columns, 35 | dnn_feature_columns = specs$dnn_feature_columns, 36 | dnn_hidden_units = c(3L, 3L), 37 | dnn_optimizer = "Adagrad", 38 | model_dir = tmp_dir 39 | ) 40 | clf %>% train(input_fn = specs$input_fn) 41 | 42 | # check whether tensorboard works with canned estimator 43 | # tensorboard(log_dir = tmp_dir, launch_browser = FALSE) 44 | 45 | variable_values <- variable_value(clf) 46 | expect_gt(length(variable_values), 0) 47 | 48 | predictions <- predict(clf, input_fn = specs$input_fn, simplify = FALSE) 49 | expect_equal(length(predictions), 32) 50 | 51 | # Test prediction simplification for canned estimator 52 | predictions <- predict(clf, input_fn = specs$input_fn, simplify = TRUE) 53 | expect_equal(dim(predictions), c(32, 5)) 54 | # Test default of simplify for canned estimator 55 | predictions <- predict(clf, input_fn = specs$input_fn) 56 | expect_equal(dim(predictions), c(32, 5)) 57 | 58 | # probabilities 59 | predictions <- unlist(predict(clf, input_fn = specs$input_fn, predict_keys = prediction_keys()$PROBABILITIES, simplify = FALSE)) 60 | expect_equal(length(predictions), 64) 61 | expect_lte(max(predictions), 1) 62 | expect_gte(min(predictions), 0) 63 | # other types that is in PredictionKey 64 | predictions <- predict(clf, input_fn = specs$input_fn, predict_keys = prediction_keys()$LOGISTIC, simplify = FALSE) 65 | 66 | # Evaluation without simplify 67 | accuracy <- evaluate(clf, input_fn = specs$input_fn, simplify = FALSE)$accuracy 68 | expect_lte(accuracy, 0.6) 69 | # Evaluation with simplify 70 | evaluation_results <- evaluate(clf, input_fn = specs$input_fn, simplify = TRUE) 71 | expect_gte(ncol(evaluation_results), 9) 72 | expect_equal(nrow(evaluation_results), 1) 73 | }) 74 | -------------------------------------------------------------------------------- /tests/testthat/test-training.R: -------------------------------------------------------------------------------- 1 | context("Test training methods") 2 | 3 | source("helper-utils.R") 4 | 5 | test_succeeds("train_and_evaluate() work for canned estimators", { 6 | 7 | skip_if_tensorflow_below("1.4") 8 | 9 | specs <- mtcars_regression_specs() 10 | 11 | est <- dnn_linear_combined_regressor( 12 | linear_feature_columns = specs$linear_feature_columns, 13 | dnn_feature_columns = specs$dnn_feature_columns, 14 | dnn_hidden_units = c(1L, 1L), 15 | dnn_optimizer = "Adagrad" 16 | ) 17 | 18 | tr_spec <- train_spec(input_fn = specs$input_fn, max_steps = 10) 19 | ev_spec <- eval_spec(input_fn = specs$input_fn, steps = 2) 20 | train_and_evaluate( 21 | est, 22 | train_spec = tr_spec, 23 | eval_spec = ev_spec 24 | ) 25 | 26 | }) 27 | 28 | test_succeeds("train_and_evaluate() work for custom estimators", { 29 | 30 | skip_if_tensorflow_below("1.4") 31 | 32 | input <- input_fn( 33 | object = iris, 34 | response = "Species", 35 | features = c( 36 | "Sepal.Length", 37 | "Sepal.Width", 38 | "Petal.Length", 39 | "Petal.Width"), 40 | batch_size = 10L 41 | ) 42 | 43 | est <- estimator(model_fn = simple_custom_model_fn) 44 | 45 | tr_spec <- train_spec(input_fn = input, max_steps = 2) 46 | ev_spec <- eval_spec(input_fn = input, steps = 2) 47 | train_and_evaluate( 48 | est, 49 | train_spec = tr_spec, 50 | eval_spec = ev_spec 51 | ) 52 | 53 | }) 54 | -------------------------------------------------------------------------------- /tfestimators.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | PackageRoxygenize: rd,collate,namespace 21 | -------------------------------------------------------------------------------- /vignettes/dataset_api.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Dataset API" 3 | output: 4 | rmarkdown::html_vignette 5 | vignette: > 6 | %\VignetteIndexEntry{Dataset API} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | %\VignetteEncoding{UTF-8} 9 | type: docs 10 | repo: https://github.com/rstudio/tfestimators 11 | menu: 12 | main: 13 | name: "Dataset API" 14 | identifier: "tfestimators-dataset-api" 15 | parent: "tfestimators-advanced" 16 | weight: 40 17 | --- 18 | 19 | 20 | ```{r setup, include=FALSE} 21 | knitr::opts_chunk$set(echo = TRUE) 22 | knitr::opts_chunk$set(eval = FALSE) 23 | ``` 24 | 25 | 26 | ## Overview 27 | 28 | We can access the TensorFlow Dataset API via the [tfdatasets](https://tensorflow.rstudio.com/tools/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. 29 | 30 | ## Dataset Preparation 31 | 32 | 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: 33 | 34 | ```{r} 35 | set.seed(123) 36 | train_idx <- sample(nrow(iris), nrow(iris) * 2/3) 37 | 38 | iris_train <- iris[train_idx,] 39 | iris_validation <- iris[-train_idx,] 40 | iris_sample <- iris_train %>% 41 | head(10) 42 | 43 | write.csv(iris_train, "iris_train.csv", row.names = FALSE) 44 | write.csv(iris_validation, "iris_validation.csv", row.names = FALSE) 45 | write.csv(iris_sample, "iris_sample.csv", row.names = FALSE) 46 | ``` 47 | 48 | ## Estimator Construction 49 | 50 | We construct the classifier as usual -- see [Estimator Basics](https://tensorflow.rstudio.com/guide/tfestimators/estimator_basics/) for details on feature columns and creating estimators. 51 | 52 | ```{r} 53 | library(tfestimators) 54 | response <- "Species" 55 | features <- setdiff(names(iris), response) 56 | feature_columns <- feature_columns( 57 | column_numeric(features) 58 | ) 59 | 60 | classifier <- dnn_classifier( 61 | feature_columns = feature_columns, 62 | hidden_units = c(16, 32, 16), 63 | n_classes = 3, 64 | label_vocabulary = c("setosa", "virginica", "versicolor") 65 | ) 66 | ``` 67 | 68 | ## Input Function 69 | 70 | The creation of the input function is similar to the [in-memory case](https://tensorflow.rstudio.com/guide/tfestimators/estimator_basics/#input-functions). 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. 71 | 72 | ```{r} 73 | iris_input_fn <- function(data) { 74 | input_fn(data, features = features, response = response) 75 | } 76 | 77 | iris_spec <- csv_record_spec("iris_sample.csv") 78 | iris_train <- text_line_dataset( 79 | "iris_train.csv", record_spec = iris_spec) %>% 80 | dataset_batch(10) %>% 81 | dataset_repeat(10) 82 | iris_validation <- text_line_dataset( 83 | "iris_validation.csv", record_spec = iris_spec) %>% 84 | dataset_batch(10) %>% 85 | dataset_repeat(1) 86 | ``` 87 | 88 | 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](https://tensorflow.rstudio.com/guide/tfdatasets/introduction/#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. 89 | 90 | ## Training and Evaluation 91 | 92 | Once the input functions and datasets are defined, the training and evaluation interface is exactly the same as in the in-memory case. 93 | 94 | ```{r} 95 | history <- train(classifier, input_fn = iris_input_fn(iris_train)) 96 | plot(history) 97 | predictions <- predict(classifier, input_fn = iris_input_fn(iris_validation)) 98 | predictions 99 | evaluation <- evaluate(classifier, input_fn = iris_input_fn(iris_validation)) 100 | evaluation 101 | ``` 102 | 103 | ## Learning More 104 | 105 | See the documetnation for the [tfdatasets](https://tensorflow.rstudio.com/tools/tfdatasets/) package for additional details on using TensorFlow datasets. 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /vignettes/examples/.gitignore: -------------------------------------------------------------------------------- 1 | mnist-data/ 2 | -------------------------------------------------------------------------------- /vignettes/examples/custom_estimator.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: custom_estimator 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Create a custom estimator for abalone age prediction. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/examples/index.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "TensorFlow Estimator API Examples" 3 | --- 4 | 5 | This collection of examples will show you how you can use 6 | `tfestimators` to easily construct powerful models using TensorFlow. 7 | 8 | ```{r index, echo=FALSE} 9 | # list all the .Rmd files we have 10 | files <- setdiff(list.files(pattern = "[.]Rmd$"), "index.Rmd") 11 | 12 | # extract DESCRIPTIONs from their front-matter 13 | descriptions <- sapply(files, function(file) { 14 | yaml <- rmarkdown::yaml_front_matter(file) 15 | yaml$description 16 | }) 17 | 18 | # construct markdown links 19 | examples <- sprintf( 20 | "[%s](%s)", 21 | tools::file_path_sans_ext(files), 22 | sprintf("%s.html", tools::file_path_sans_ext(files)) 23 | ) 24 | 25 | # construct dataset associating HTML link with description 26 | data <- data.frame( 27 | Example = examples, 28 | Description = descriptions, 29 | stringsAsFactors = FALSE, 30 | row.names = NULL 31 | ) 32 | 33 | # output as markdown table 34 | knitr::kable(data, format = "markdown", row.names = FALSE) 35 | ``` 36 | 37 | -------------------------------------------------------------------------------- /vignettes/examples/iris_custom_decay_dnn.R: -------------------------------------------------------------------------------- 1 | library(tfestimators) 2 | 3 | # Construct the input 4 | inputs <- input_fn( 5 | iris, 6 | response = "Species", 7 | features = c( 8 | "Sepal.Length", 9 | "Sepal.Width", 10 | "Petal.Length", 11 | "Petal.Width"), 12 | batch_size = 10 13 | ) 14 | 15 | custom_model_fn <- function(features, labels, mode, params, config) { 16 | 17 | # Create three fully connected layers respectively of size 10, 20, and 10 with 18 | # each layer having a dropout probability of 0.1. 19 | logits <- features %>% 20 | tf$contrib$layers$stack( 21 | tf$contrib$layers$fully_connected, c(10L, 20L, 10L), 22 | normalizer_fn = tf$contrib$layers$dropout, 23 | normalizer_params = list(keep_prob = 0.9)) %>% 24 | tf$contrib$layers$fully_connected(3L, activation_fn = NULL) # Compute logits (1 per class) and compute loss. 25 | 26 | # Compute predictions. 27 | predicted_classes <- tf$argmax(logits, 1L) 28 | if (mode == "infer") { 29 | predictions <- list( 30 | class = predicted_classes, 31 | prob = tf$nn$softmax(logits)) 32 | return(estimator_spec(mode = mode, predictions = predictions)) 33 | } 34 | 35 | # Convert the labels to a one-hot tensor of shape (length of features, 3) and 36 | # with a on-value of 1 for each one-hot vector of length 3. 37 | onehot_labels <- tf$one_hot(labels, 3L, 1L, 0L) 38 | # Compute loss. 39 | loss <- tf$losses$softmax_cross_entropy(onehot_labels, logits) 40 | 41 | if (mode == "train") { 42 | global_step <- tf$train$get_global_step() 43 | learning_rate <- tf$train$exponential_decay( 44 | learning_rate = 0.1, 45 | global_step = global_step, 46 | decay_steps = 100L, 47 | decay_rate = 0.001) 48 | optimizer <- tf$train$AdagradOptimizer(learning_rate = learning_rate) 49 | train_op <- optimizer$minimize(loss, global_step = global_step) 50 | return(estimator_spec(mode = mode, loss = loss, train_op = train_op)) 51 | } 52 | 53 | # Compute evaluation metrics. 54 | eval_metric_ops <- list( 55 | accuracy = tf$metrics$accuracy( 56 | labels = labels, predictions = predicted_classes 57 | )) 58 | 59 | return(estimator_spec(mode = mode, loss = loss, eval_metric_ops = eval_metric_ops)) 60 | } 61 | 62 | model_dir <- "/tmp/iris-custom-decay-cnn-model" 63 | 64 | # Intialize classifer and training 65 | classifier <- estimator( 66 | model_fn = custom_model_fn, model_dir = model_dir) 67 | 68 | classifier %>% train(input_fn = inputs, steps = 100) 69 | 70 | # Genearate predictions 71 | predictions <- predict(classifier, input_fn = inputs) 72 | 73 | -------------------------------------------------------------------------------- /vignettes/examples/iris_custom_decay_dnn.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: iris_custom_decay_dnn 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Create a deep neural network with learning rate decay for iris dataset. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/examples/iris_dnn_classifier.R: -------------------------------------------------------------------------------- 1 | library(tfestimators) 2 | 3 | response <- function() "Species" 4 | features <- function() setdiff(names(iris), response()) 5 | 6 | # split into train, test datasets 7 | set.seed(123) 8 | partitions <- modelr::resample_partition(iris, c(test = 0.2, train = 0.8)) 9 | iris_train <- as.data.frame(partitions$train) 10 | iris_test <- as.data.frame(partitions$test) 11 | 12 | # construct feature columns 13 | feature_columns <- feature_columns( 14 | column_numeric(features()) 15 | ) 16 | 17 | # construct classifier 18 | classifier <- dnn_classifier( 19 | feature_columns = feature_columns, 20 | hidden_units = c(10, 20, 10), 21 | n_classes = 3 22 | ) 23 | 24 | # construct input function 25 | iris_input_fn <- function(data) { 26 | input_fn(data, features = features(), response = response()) 27 | } 28 | 29 | # train classifier with training dataset 30 | train(classifier, input_fn = iris_input_fn(iris_train)) 31 | 32 | # valuate with test dataset 33 | predictions <- predict(classifier, input_fn = iris_input_fn(iris_test)) 34 | evaluation <- evaluate(classifier, input_fn = iris_input_fn(iris_test)) 35 | -------------------------------------------------------------------------------- /vignettes/examples/iris_dnn_classifier.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: iris_dnn_classifier 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Construct a DNN classifier using the iris data set. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/examples/mnist.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: mnist 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Train a deep learning model with the MNIST dataset. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/examples/tensorflow_layers.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: tensorflow_layers 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Build an estimator using TensorFlow layers. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/examples/wide_and_deep.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: wide_and_deep 3 | type: docs 4 | repo: https://github.com/rstudio/tfestimators 5 | description: Build a wide & deep learning model. 6 | menu: 7 | main: 8 | parent: tfestimators-examples 9 | --- 10 | 11 |
    12 | Source: `r sprintf("https://github.com/rstudio/tfestimators/blob/main/vignettes/examples/%s.R", rmarkdown::metadata$title)` 13 |
    14 | 15 | ```{r, echo = FALSE} 16 | knitr::opts_chunk$set(eval = FALSE) 17 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 18 | ``` 19 | -------------------------------------------------------------------------------- /vignettes/images/estimator-apis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/estimator-apis.png -------------------------------------------------------------------------------- /vignettes/images/estimator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/estimator.png -------------------------------------------------------------------------------- /vignettes/images/experiment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/experiment.png -------------------------------------------------------------------------------- /vignettes/images/tensorboard-graph-details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/tensorboard-graph-details.png -------------------------------------------------------------------------------- /vignettes/images/tensorboard-graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/tensorboard-graph.png -------------------------------------------------------------------------------- /vignettes/images/tensorboard-loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/tensorboard-loss.png -------------------------------------------------------------------------------- /vignettes/images/tensorflow-architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rstudio/tfestimators/93bd3cf2bac7d8a448f272f1f27ce26f0e779fa1/vignettes/images/tensorflow-architecture.png -------------------------------------------------------------------------------- /vignettes/parsing_spec.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Parsing Utilities" 3 | output: 4 | rmarkdown::html_vignette 5 | vignette: > 6 | %\VignetteIndexEntry{Parsing Utilities} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | %\VignetteEncoding{UTF-8} 9 | type: docs 10 | repo: https://github.com/rstudio/tfestimators 11 | menu: 12 | main: 13 | name: "Parsing Utilities" 14 | identifier: "tfestimators-parsing-utilities" 15 | parent: "tfestimators-advanced" 16 | weight: 30 17 | --- 18 | 19 | 20 | ```{r setup, include=FALSE} 21 | library(tfestimators) 22 | knitr::opts_chunk$set(echo = TRUE) 23 | knitr::opts_chunk$set(eval = FALSE) 24 | ``` 25 | 26 | 27 | ## Overview 28 | 29 | Parsing utilities are a set of functions that helps generate parsing spec for `tf$parse_example` to be used with estimators. If users keep data in `tf$Example` format, they need to call 30 | `tf$parse_example` with a proper feature spec. There are two main things that 31 | these utility functions help: 32 | 33 | * Users need to combine parsing spec of features with labels and weights (if any) since they are all parsed from same `tf$Example` instance. The utility functions combine these specs. 34 | 35 | * It is difficult to map expected label by a estimator such as `dnn_classifier` 36 | to corresponding `tf$parse_example` spec. The utility functions encode it by getting 37 | related information from users (key, dtype). 38 | 39 | ## Example output of parsing spec 40 | 41 | 42 | ```{r} 43 | parsing_spec <- classifier_parse_example_spec( 44 | feature_columns = column_numeric('a'), 45 | label_key = 'b', 46 | weight_column = 'c' 47 | ) 48 | ``` 49 | 50 | For the above example, `classifier_parse_example_spec` would return the following: 51 | 52 | ```{r} 53 | expected_spec <- list( 54 | a = tf$python$ops$parsing_ops$FixedLenFeature(reticulate::tuple(1L), dtype = tf$float32), 55 | c = tf$python$ops$parsing_ops$FixedLenFeature(reticulate::tuple(1L), dtype = tf$float32), 56 | b = tf$python$ops$parsing_ops$FixedLenFeature(reticulate::tuple(1L), dtype = tf$int64) 57 | ) 58 | 59 | # This should be the same as the one we constructed using `classifier_parse_example_spec` 60 | testthat::expect_equal(parsing_spec, expected_spec) 61 | ``` 62 | 63 | ## Example usage with a classifier 64 | 65 | Firstly, define features transformations and initiailize your classifier similar to the following: 66 | 67 | ```{r} 68 | fcs <- feature_columns(...) 69 | 70 | model <- dnn_classifier( 71 | n_classes = 1000, 72 | feature_columns = fcs, 73 | weight_column = 'example-weight', 74 | label_vocabulary= c('photos', 'keep', ...), 75 | hidden_units = c(256, 64, 16) 76 | ) 77 | ``` 78 | 79 | Next, create the parsing configuration for `tf$parse_example` using `classifier_parse_example_spec` and the feature columns `fcs` we have just defined: 80 | 81 | ```{r} 82 | parsing_spec <- classifier_parse_example_spec( 83 | feature_columns = fcs, 84 | label_key = 'my-label', 85 | label_dtype = tf$string, 86 | weight_column = 'example-weight' 87 | ) 88 | 89 | ``` 90 | 91 | This label configuration tells the classifier the following: 92 | 93 | * weights are retrieved with key 'example-weight' 94 | * label is string and can be one of the following `c('photos', 'keep', ...)` 95 | * integer id for label 'photos' is 0, 'keep' is 1, etc 96 | 97 | Then define your input function with the help of `read_batch_features` that reads the batches of features from files in `tf$Example` format with the parsing configuration `parsing_spec` we just defined: 98 | 99 | ```{r} 100 | input_fn_train <- function() { 101 | features <- tf$contrib$learn$read_batch_features( 102 | file_pattern = train_files, 103 | batch_size = batch_size, 104 | features = parsing_spec, 105 | reader = tf$RecordIOReader) 106 | labels <- features[["my-label"]] 107 | return(list(features, labels)) 108 | } 109 | ``` 110 | 111 | Finally we can train the model using the training input function parsed by `classifier_parse_example_spec`: 112 | 113 | ```{r} 114 | train(model, input_fn = input_fn_train) 115 | ``` 116 | -------------------------------------------------------------------------------- /vignettes/tensorboard.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "TensorBoard Visualization" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{TensorBoard Visualization} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | type: docs 9 | repo: https://github.com/rstudio/tfestimators 10 | menu: 11 | main: 12 | name: "TensorBoard" 13 | identifier: "tfestimators-tensorboard-visualizations" 14 | parent: "tfestimators-using-tfestimators" 15 | weight: 50 16 | --- 17 | 18 | ```{r setup, include = FALSE} 19 | library(tfestimators) 20 | knitr::opts_chunk$set(comment = NA, eval = FALSE) 21 | ``` 22 | 23 | ## Overview 24 | 25 | TensorBoard is a visualization tool included with TensorFlow that enables you to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it. 26 | 27 | Models built using tfestimators automatically contain most of the necessary information to visualize the TensorFlow graph, variables, etc., for you so you can easily launch the TensorBoard without additional manual specifications. 28 | 29 | ## Examples 30 | 31 | To start the TensorBoard, you trained a model in a similar fashion as follows: 32 | 33 | ```{r} 34 | estimator( 35 | model_fn = model_fn, 36 | model_dir = "/tmp/test" 37 | ) %>% train(input_fn = input, steps = 100L) 38 | 39 | ``` 40 | 41 | and then you can call 42 | 43 | ``` 44 | tensorboard(log_dir = "/tmp/test", launch_browser = TRUE) 45 | ``` 46 | with `log_dir` being the directory you used to save the model checkpoints, to launch the TensorBoard in browser. 47 | 48 | For example, here's a TensorBoard display for a model's training loss: 49 | 50 | ![](images/tensorboard-loss.png){.screenshot} 51 | 52 | 53 | 54 | Here's another TensorBoard display for this model's TensorFlow graph that demonstrates the architecture of the model: 55 | 56 | ![](images/tensorboard-graph.png){.screenshot} 57 | 58 | 59 | You can also click to see each subgraph's details: 60 | 61 | 62 | ![](images/tensorboard-graph-details.png){.screenshot} 63 | --------------------------------------------------------------------------------