kse-01/data.csv
2023-10-11 13:59:07 +02:00

5.6 MiB

1namefilelinetypecomment
20UserInputErrortensorflow/configure.py74class
31is_windowstensorflow/configure.py78function
42is_linuxtensorflow/configure.py82function
53is_macostensorflow/configure.py86function
64is_ppc64letensorflow/configure.py90function
75is_cygwintensorflow/configure.py94function
86get_inputtensorflow/configure.py98function
97symlink_forcetensorflow/configure.py109functionForce symlink, equivalent of 'ln -sf'. Args: target: items to link to. link_name: name of the link.
108sed_in_placetensorflow/configure.py126functionReplace old string with new string in file. Args: filename: string for filename. old: string to replace. new: new string to replace to.
119write_to_bazelrctensorflow/configure.py141function
1210write_action_env_to_bazelrctensorflow/configure.py146function
1311run_shelltensorflow/configure.py150function
1412cygpathtensorflow/configure.py163functionConvert path from posix to windows.
1513get_python_pathtensorflow/configure.py168functionGet the python site package paths.
1614get_python_major_versiontensorflow/configure.py198functionGet the python major version.
1715setup_pythontensorflow/configure.py203functionSetup python related env variables.
1816reset_tf_configure_bazelrctensorflow/configure.py273functionReset file that contains customized config settings.
1917cleanup_makefiletensorflow/configure.py278functionDelete any leftover BUILD files from the Makefile build. These files could interfere with Bazel parsing.
2018get_vartensorflow/configure.py292functionGet boolean input from user. If var_name is not set in env, ask user to enable query_item or not. If the response is empty, use the default. Args: environ_cp: copy of the os.environ. var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". query_item: string for feature related to the variable, e.g. "CUDA for Nvidia GPUs". enabled_by_default: boolean for default behavior. question: optional string for how to ask for user input. yes_reply: optional string for reply when feature is enabled. no_reply: optional string for reply when feature is disabled. Returns: boolean value of the variable. Raises: UserInputError: if an environment variable is set, but it cannot be interpreted as a boolean indicator, assume that the user has made a scripting error, and will continue to provide invalid input. Raise the error to avoid infinitely looping.
2119set_build_vartensorflow/configure.py377functionSet if query_item will be enabled for the build. Ask user if query_item will be enabled. Default is used if no input is given. Set subprocess environment variable and write to .bazelrc if enabled. Args: environ_cp: copy of the os.environ. var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". query_item: string for feature related to the variable, e.g. "CUDA for Nvidia GPUs". option_name: string for option to define in .bazelrc. enabled_by_default: boolean for default behavior. bazel_config_name: Name for Bazel --config argument to enable build feature.
2220set_action_env_vartensorflow/configure.py411functionSet boolean action_env variable. Ask user if query_item will be enabled. Default is used if no input is given. Set environment variable and write to .bazelrc. Args: environ_cp: copy of the os.environ. var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". query_item: string for feature related to the variable, e.g. "CUDA for Nvidia GPUs". enabled_by_default: boolean for default behavior. question: optional string for how to ask for user input. yes_reply: optional string for reply when feature is enabled. no_reply: optional string for reply when feature is disabled. bazel_config_name: adding config to .bazelrc instead of action_env.
2321convert_version_to_inttensorflow/configure.py446functionConvert a version number to a integer that can be used to compare. Version strings of the form X.YZ and X.Y.Z-xxxxx are supported. The 'xxxxx' part, for instance 'homebrew' on OS/X, is ignored. Args: version: a version to be converted Returns: An integer if converted successfully, otherwise return None.
2422check_bazel_versiontensorflow/configure.py471functionCheck installed bazel version is between min_version and max_version. Args: min_version: string for minimum bazel version (must exist!). max_version: string for maximum bazel version (must exist!). Returns: The bazel version detected.
2523set_cc_opt_flagstensorflow/configure.py518functionSet up architecture-dependent optimization flags. Also append CC optimization flags to bazel.rc.. Args: environ_cp: copy of the os.environ.
2624set_tf_cuda_clangtensorflow/configure.py546functionset TF_CUDA_CLANG action_env. Args: environ_cp: copy of the os.environ.
2725set_tf_download_clangtensorflow/configure.py566functionSet TF_DOWNLOAD_CLANG action_env.
2826get_from_env_or_user_or_defaulttensorflow/configure.py582functionGet var_name either from env, or user or default. If var_name has been set as environment variable, use the preset value, else ask for user input. If no input is provided, the default is used. Args: environ_cp: copy of the os.environ. var_name: string for name of environment variable, e.g. "TF_NEED_CUDA". ask_for_var: string for how to ask for user input. var_default: default value string. Returns: string value for var_name
2927set_clang_cuda_compiler_pathtensorflow/configure.py607functionSet CLANG_CUDA_COMPILER_PATH.
3028prompt_loop_or_load_from_envtensorflow/configure.py630functionLoop over user prompts for an ENV param until receiving a valid response. For the env param var_name, read from the environment or verify user input until receiving valid input. When done, set var_name in the environ_cp to its new value. Args: environ_cp: (Dict) copy of the os.environ. var_name: (String) string for name of environment variable, e.g. "TF_MYVAR". var_default: (String) default value string. ask_for_var: (String) string for how to ask for user input. check_success: (Function) function that takes one argument and returns a boolean. Should return True if the value provided is considered valid. May contain a complex error message if error_msg does not provide enough information. In that case, set suppress_default_error to True. error_msg: (String) String with one and only one '%s'. Formatted with each invalid response upon check_success(input) failure. suppress_default_error: (Bool) Suppress the above error message in favor of one from the check_success function. resolve_symlinks: (Bool) Translate symbolic links into the real filepath. n_ask_attempts: (Integer) Number of times to query for valid input before raising an error and quitting. Returns: [String] The value of var_name after querying for input. Raises: UserInputError: if a query has been attempted n_ask_attempts times without success, assume that the user has made a scripting error, and will continue to provide invalid input. Raise the error to avoid infinitely looping.
3129create_android_ndk_ruletensorflow/configure.py696functionSet ANDROID_NDK_HOME and write Android NDK WORKSPACE rule.
3230create_android_sdk_ruletensorflow/configure.py724functionSet Android variables and write Android SDK WORKSPACE rule.
3331get_ndk_api_leveltensorflow/configure.py788functionGets the appropriate NDK API level to use for the provided Android NDK path.
3432set_gcc_host_compiler_pathtensorflow/configure.py836functionSet GCC_HOST_COMPILER_PATH.
3533reformat_version_sequencetensorflow/configure.py858functionReformat the version string to have the given number of sequences. For example: Given (7, 2) -> 7.0 (7.0.1, 2) -> 7.0 (5, 1) -> 5 (5.0.3.2, 1) -> 5 Args: version_str: String, the version string. sequence_count: int, an integer. Returns: string, reformatted version string.
3634set_tf_cuda_pathstensorflow/configure.py881functionSet TF_CUDA_PATHS.
3735set_tf_cuda_versiontensorflow/configure.py892functionSet TF_CUDA_VERSION.
3836set_tf_cudnn_versiontensorflow/configure.py904functionSet TF_CUDNN_VERSION.
3937is_cuda_compatibletensorflow/configure.py916functionCheck compatibility between given library and cudnn/cudart libraries.
4038set_tf_tensorrt_versiontensorflow/configure.py945functionSet TF_TENSORRT_VERSION.
4139set_tf_nccl_versiontensorflow/configure.py962functionSet TF_NCCL_VERSION.
4240get_native_cuda_compute_capabilitiestensorflow/configure.py979functionGet native cuda compute capabilities. Args: environ_cp: copy of the os.environ. Returns: string of native cuda compute capabilities, separated by comma.
4341set_tf_cuda_compute_capabilitiestensorflow/configure.py1003functionSet TF_CUDA_COMPUTE_CAPABILITIES.
4442set_other_cuda_varstensorflow/configure.py1074functionSet other CUDA related variables.
4543set_host_cxx_compilertensorflow/configure.py1083functionSet HOST_CXX_COMPILER.
4644set_host_c_compilertensorflow/configure.py1100functionSet HOST_C_COMPILER.
4745set_computecpp_toolkit_pathtensorflow/configure.py1117functionSet COMPUTECPP_TOOLKIT_PATH.
4846set_trisycl_include_dirtensorflow/configure.py1149functionSet TRISYCL_INCLUDE_DIR.
4947set_system_libs_flagtensorflow/configure.py1216function
5048is_reduced_optimize_huge_functions_availabletensorflow/configure.py1233functionCheck to see if the system supports /d2ReducedOptimizeHugeFunctions. The above compiler flag is a new compiler flag introduced to the Visual Studio compiler in version 16.4 (available in Visual Studio 2019, Preview edition only, as of 2019-11-19). TensorFlow needs this flag to massively reduce compile times, but until 16.4 is officially released, we can't depend on it. See also https://groups.google.com/a/tensorflow.org/d/topic/build/SsW98Eo7l3o/discussion Because it's very annoying to check this manually (to check the MSVC installed versions, you need to use the registry, and it's not clear if Bazel will be using that install version anyway), we expect enviroments who know they may use this flag to export TF_VC_VERSION=16.4 TODO(angerson, gunan): Remove this function when TensorFlow's minimum VS version is upgraded to 16.4. Arguments: environ_cp: Environment of the current execution Returns: boolean, whether or not /d2ReducedOptimizeHugeFunctions is available on this machine.
5149set_windows_build_flagstensorflow/configure.py1262functionSet Windows specific build options.
5250config_info_linetensorflow/configure.py1283functionHelper function to print formatted help text for Bazel config options.
5351configure_iostensorflow/configure.py1288functionConfigures TensorFlow for iOS builds. This function will only be executed if `is_macos()` is true.
5452validate_cuda_configtensorflow/configure.py1305functionRun find_cuda_config.py and return cuda_toolkit_path, or None.
5553VarsAndArithmeticObjectGraphtensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py37classThree vars (one in a sub-module) and compute method.
5654computetensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py51method
5755ReferencesParenttensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py55class
5856CyclicModuletensorflow/tensorflow/cc/saved_model/testdata/generate_saved_models.py64class
5957tfaddtensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py48function
6058tfadd_with_ckpttensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py54function
6159tfadd_with_ckpt_savertensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py69function
6260tfassert_eqtensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py88function
6361tfcondtensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py96function
6462tfgathertensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py104function
6563tfmatmultensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py110function
6664tfmatmulandaddtensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py116function
6765tffunctiontensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py124function
6866tfsplitstensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py135functionA more complex graph, including splits.
6967tftop_ktensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py152function
7068tfvariable_readonlytensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py158function
7169tfvariabletensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py169function
7270tfvariable_sequential_updatestensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py177function
7371export_debug_infotensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py189functionExports debug information from a graph. Args: exported_graph: A Graph that has been created by tracing a saveable view. Returns: Corresponding GraphDebugInfo with traces for all ops in exported_graph.
7472write_graphtensorflow/tensorflow/compiler/aot/tests/make_test_graphs.py204functionBuild a graph using build_graph and write it out.
7573set_tf_optionstensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/common_v1.py38function
7674ReferencesParenttensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/cyclic_object_graph.py27class
7775Childtensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/dag_object_graph.py27class
7876plustensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/defun_export.py29function
7977write_vocabulary_filetensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/hash_table_asset_v1.py39functionWrite temporary vocab file for module construction.
8078mnist_modeltensorflow/tensorflow/compiler/mlir/tensorflow/tests/tf_saved_model/keras.py27functionCreates a MNIST model.
8179adam_update_numpytensorflow/tensorflow/compiler/tests/adam_test.py34function
8280GetRunMetadataLabelstensorflow/tensorflow/compiler/tests/dense_layer_test.py36functionReturns all labels in run_metadata.
8381InLabelstensorflow/tensorflow/compiler/tests/dense_layer_test.py45functionReturns true iff one of the labels contains substr.
8482ReferenceDepthwiseConv2Dtensorflow/tensorflow/compiler/tests/depthwise_conv_op_test.py35function
8583multiple_tpustensorflow/tensorflow/compiler/tests/eager_test.py772function
8684ExtractImagePatchestensorflow/tensorflow/compiler/tests/extract_image_patches_op_test.py29classFunctional tests for ExtractImagePatches op.
8785pick_10tensorflow/tensorflow/compiler/tests/fft_test.py38function
8886to_32bittensorflow/tensorflow/compiler/tests/fft_test.py45function
8987GatherBenchmarktensorflow/tensorflow/compiler/tests/gather_test.py158classMicrobenchmarks for the gather op.
9088benchmarkSliceGatherAxis0tensorflow/tensorflow/compiler/tests/gather_test.py183method
9189benchmarkSliceGatherAxis0XLAtensorflow/tensorflow/compiler/tests/gather_test.py186method
9290benchmarkSliceGatherAxis1tensorflow/tensorflow/compiler/tests/gather_test.py189method
9391benchmarkSliceGatherAxis1XLAtensorflow/tensorflow/compiler/tests/gather_test.py192method
9492benchmarkSliceGatherAxis4tensorflow/tensorflow/compiler/tests/gather_test.py195method
9593benchmarkSliceGatherAxis4XLAtensorflow/tensorflow/compiler/tests/gather_test.py198method
9694benchmarkNontrivialGatherAxis0tensorflow/tensorflow/compiler/tests/gather_test.py201method
9795benchmarkNontrivialGatherAxis0XLAtensorflow/tensorflow/compiler/tests/gather_test.py204method
9896benchmarkNontrivialGatherAxis1tensorflow/tensorflow/compiler/tests/gather_test.py207method
9997benchmarkNontrivialGatherAxis1XLAtensorflow/tensorflow/compiler/tests/gather_test.py210method
10098benchmarkNontrivialGatherAxis4tensorflow/tensorflow/compiler/tests/gather_test.py213method
10199benchmarkNontrivialGatherAxis4XLAtensorflow/tensorflow/compiler/tests/gather_test.py216method
102100BuilderFntensorflow/tensorflow/compiler/tests/gather_test.py163method
103101NoRewriteSessionConfigtensorflow/tensorflow/compiler/tests/jit_test.py46function
104102CompiledKerneltensorflow/tensorflow/compiler/tests/jit_test.py56functionExecute 'fn' as a compiled XLA kernel, with 'inputs'.
105103RunMetadataLabelstensorflow/tensorflow/compiler/tests/jit_test.py68functionReturns all labels in run_metadata.
106104InLabelstensorflow/tensorflow/compiler/tests/jit_test.py77functionReturns true iff one of the labels contains substr.
107105MetadataHasXlaRunOptensorflow/tensorflow/compiler/tests/jit_test.py82functionReturns true if there are XlaRun kernels in run_metadata's timeline.
108106Cliptensorflow/tensorflow/compiler/tests/lstm.py38functionClips x to the range [-1., 1.].
109107LSTMCellWeightsShapetensorflow/tensorflow/compiler/tests/lstm.py43functionReturns the shape of the weights for a single LSTM cell.
110108LSTMCelltensorflow/tensorflow/compiler/tests/lstm.py50functionUnrolls a single LSTM cell with clipped activations forward by one step. Args: weights: Weight matrix with shape LSTMCellWeightsShape. m_prev: Previous m states with shape [batch_size, num_nodes]. c_prev: Previous c states with shape [batch_size, num_nodes]. x: Input with shape [batch_size, num_inputs]. pad: Padding with shape [batch_size, 1]. Each padding value is either 0 or 1, where 1 indicates padding; i.e. the input is shorter than the sequence length, and the (m, c) states should simply be passed through from the previous states. Returns: The next (m, c) states, each with shape [batch_size, num_nodes].
111109LSTMLayertensorflow/tensorflow/compiler/tests/lstm.py88functionUnrolls a layer of LSTM cells forward by the sequence length. The sequence length is determined by the length of x_seq and pad_seq, which must be the same. Args: cell_name: Base name of each cell. weights: Weight matrix with shape LSTMCellWeightsShape. m: Initial m states with shape [batch_size, num_nodes]. c: Initial c states with shape [batch_size, num_nodes]. x_seq: List of inputs, each with shape [batch_size, num_inputs]. The length of the list is the sequence length. pad_seq: List of paddings, each with shape [batch_size, 1]. The length of the list is the sequence length. Each padding value is either 0 or 1, where 1 indicates padding; i.e. the input is shorter than the sequence length. Returns: List of per-sequence-step outputs, each with shape [batch_size, num_nodes]. Raises: ValueError: If len(x_seq) != len(pad_seq).
112110RandomVartensorflow/tensorflow/compiler/tests/lstm.py121functionReturns a variable of the given shape initialized to random values.
113111RandomInputstensorflow/tensorflow/compiler/tests/lstm.py127functionReturns randomly initialized (x_seq, pad_seq) sequences.
114112BuildLSTMLayertensorflow/tensorflow/compiler/tests/lstm.py140functionBuilds a single LSTM layer with random weights and inputs. Args: batch_size: Inputs are fed in batches of this size. seq_length: The sequence length to unroll the LSTM layer. num_inputs: Dimension of inputs that are fed into each LSTM cell. num_nodes: The number of nodes in each LSTM cell. Returns: (out_seq, weights) pair. The out_seq is a list of per-sequence-step outputs, each with shape [batch_size, num_nodes]. The weights are a list of weight variables that may be trained.
115113LSTMBenchmarktensorflow/tensorflow/compiler/tests/lstm_test.py238classMcro-benchmarks for a single layer of LSTM cells.
116114benchmarkLayerInferencetensorflow/tensorflow/compiler/tests/lstm_test.py256method
117115benchmarkLayerInferenceXLAtensorflow/tensorflow/compiler/tests/lstm_test.py260method
118116benchmarkLayerTrainingtensorflow/tensorflow/compiler/tests/lstm_test.py264method
119117benchmarkLayerTrainingXLAtensorflow/tensorflow/compiler/tests/lstm_test.py268method
120118zip_to_first_list_lengthtensorflow/tensorflow/compiler/tests/matrix_diag_ops_test.py32function
121119repack_diagonalstensorflow/tensorflow/compiler/tests/matrix_diag_ops_test.py40function
122120square_casestensorflow/tensorflow/compiler/tests/matrix_diag_ops_test.py95function
123121tall_casestensorflow/tensorflow/compiler/tests/matrix_diag_ops_test.py173function
124122fat_casestensorflow/tensorflow/compiler/tests/matrix_diag_ops_test.py261function
125123MakePlaceholdertensorflow/tensorflow/compiler/tests/matrix_triangular_solve_op_test.py36function
126124NHWCToNCHWtensorflow/tensorflow/compiler/tests/pooling_ops_test.py33functionConvert the input from NHWC format to NCHW. Args: input_tensor: a 4-D tensor, or a 4-element array representing the same. Returns: the converted tensor or a shape array
127125NCHWToNHWCtensorflow/tensorflow/compiler/tests/pooling_ops_test.py48functionConvert the input from NCHW format to NHWC. Args: input_tensor: a 4-D tensor, or a 4-element array representing the same. Returns: the converted tensor or a shape array
128126numpy_reversetensorflow/tensorflow/compiler/tests/scan_ops_test.py32function
129127handle_optionstensorflow/tensorflow/compiler/tests/scan_ops_test.py43functionAdds tf options to numpy scan ops.
130128space_to_batch_directtensorflow/tensorflow/compiler/tests/spacetobatch_op_test.py30functionDirect Python implementation of space-to-batch conversion. This is used for tests only. Args: input_array: N-D array block_shape: 1-D array of shape [num_block_dims]. paddings: 2-D array of shape [num_block_dims, 2]. Returns: Converted tensor.
131129implicit_reparameterization_gradtensorflow/tensorflow/compiler/tests/special_math_test.py58function
132130xla_devicetensorflow/tensorflow/compiler/tests/stateful_random_ops_test.py41function
133131xla_device_nametensorflow/tensorflow/compiler/tests/stateful_random_ops_test.py55function
134132StatelessRandomOpsBenchmarktensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py136classMicrobenchmarks for the stateless random ops.
135133benchmarkUniformF32tensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py152method
136134benchmarkUniformF64tensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py156method
137135benchmarkUniformF32XLAtensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py160method
138136benchmarkUniformF64XLAtensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py164method
139137BuilderFntensorflow/tensorflow/compiler/tests/stateless_random_ops_test.py141method
140138ConvertBetweenDataFormatstensorflow/tensorflow/compiler/tests/test_utils.py26functionConverts 4D tensor between data formats.
141139PermuteDimsBetweenDataFormatstensorflow/tensorflow/compiler/tests/test_utils.py47functionGet new shape for converting between data formats.
142140RunWithWarmuptensorflow/tensorflow/compiler/tests/test_utils.py71functionRuns a graph a few times to ensure that its clusters are compiled.
143141nhwc_to_formattensorflow/tensorflow/compiler/tests/unary_ops_test.py37functionConverts a numpy array from NHWC format to `data_format`.
144142StridedSliceAssignCheckertensorflow/tensorflow/compiler/tests/variable_ops_test.py422classCompares the results of a slice assignment using Tensorflow and numpy.
145143is_compile_on_demandtensorflow/tensorflow/compiler/tests/while_test.py260function
146144parse_disabled_manifesttensorflow/tensorflow/compiler/tests/xla_test.py55function
147145Benchmarktensorflow/tensorflow/compiler/tests/xla_test.py250functionBuild a graph and run benchmarks against it, with or without XLA. Args: tf_bench: An instance of tf.test.Benchmark, used to run the benchmark. builder_fn: A function that builds a graph when invoked, and returns (name, fetches), where name is the name of the test, and fetches is a list of tensors to fetch as output. use_xla_jit: If true compile with the XLA JIT, otherwise use regular TF. device: The tensorflow device to run on, e.g. "cpu", "gpu". separate_compiled_gradients: If true put each gradient subgraph into a separate compilation scope. This gives fine-grained control over which portions of the graph will be compiled as a single unit. Compiling gradients separately may yield better performance for some graphs. The scope is named based on the scope of the forward computation as well as the name of the gradients. As a result, the gradients will be compiled in a scope that is separate from both the forward computation, and from other gradients.
148146broadcasttensorflow/tensorflow/compiler/tf2xla/python/xla.py226function
149147clamptensorflow/tensorflow/compiler/tf2xla/python/xla.py234function
150148convtensorflow/tensorflow/compiler/tf2xla/python/xla.py241functionWraps the XLA ConvGeneralDilated operator. ConvGeneralDilated is the most general form of XLA convolution and is documented at https://www.tensorflow.org/performance/xla/operation_semantics#conv_convolution Args: lhs: the input tensor rhs: the kernel tensor window_strides: the inter-window strides padding: the padding to apply at the start and end of each input dimensions lhs_dilation: dilation to apply between input elements rhs_dilation: dilation to apply between kernel elements dimension_numbers: a `ConvolutionDimensionNumbers` proto. feature_group_count: number of feature groups for grouped convolution. precision_config: a `xla.PrecisionConfig` proto. name: an optional name for the operator Returns: A tensor representing the output of the convolution.
151149dottensorflow/tensorflow/compiler/tf2xla/python/xla.py291function
152150dot_generaltensorflow/tensorflow/compiler/tf2xla/python/xla.py295function
153151self_adjoint_eigtensorflow/tensorflow/compiler/tf2xla/python/xla.py307function
154152svdtensorflow/tensorflow/compiler/tf2xla/python/xla.py311function
155153random_normaltensorflow/tensorflow/compiler/tf2xla/python/xla.py327function
156154random_uniformtensorflow/tensorflow/compiler/tf2xla/python/xla.py333function
157155reduce_windowtensorflow/tensorflow/compiler/tf2xla/python/xla.py343functionWraps the XLA ReduceWindow operator. ReduceWindow is documented at https://www.tensorflow.org/performance/xla/operation_semantics#reducewindow . Args: operand: the input tensor init: a scalar tensor representing the initial value for the reduction reducer: a reduction function that combines a pair of scalars. window_dimensions: shape of the window, as a list of integers window_strides: inter-window strides, as a list of integers. Optional; if omitted, defaults to strides of 1. padding: padding to apply to 'operand'. List of (low, high) pairs of integers that specify the padding to apply before and after each dimension. Optional; if omitted, defaults to no padding. name: the operator name, or None. Returns: A tensor that represents the output of the reduce_window operator.
158156reshapetensorflow/tensorflow/compiler/tf2xla/python/xla.py391function
159157selecttensorflow/tensorflow/compiler/tf2xla/python/xla.py398function
160158slicetensorflow/tensorflow/compiler/tf2xla/python/xla.py406function
161159gathertensorflow/tensorflow/compiler/tf2xla/python/xla.py452function
162160scattertensorflow/tensorflow/compiler/tf2xla/python/xla.py463function
163161Shardingtensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py28classA class to support adding sharding attributes to Ops. Use the factory constructors and then call apply_to_tensor: Sharding.replicate().apply_to_tensor(tensor)
164162replicatetensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py40methodReturns a replicated sharding attribute. This causes an op to be computed in its entirety independently on all cores in the XLA device.
165163assign_devicetensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py50methodReturns an AssignDevice sharding attribute. This causes an op to be computed in its entirety only on one core in the XLA device. Args: core: The core to assign this Op to.
166164tiletensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py65methodReturns a Tiled sharding attribute. This causes an op to be partially computed on multiple cores in the XLA device. Args: tile_assignment: An np.ndarray describing the topology of the tiling and which device will compute which part of the topology. Raises: TypeError: tile_assignment was not of np.array type. TODO(jmolloy): This concept is nefarious and is not something we really want to expose to users (especially as the contract for tile_assignment is very strict).
167165splittensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py93methodReturns a Sharding that splits a tensor across a dimension. This creates a Tiled attribute, similar to tile(), but easier to use for the common case of tiling a tensor N ways in one dimension. Args: tensor: A tf.Tensor to split. split_dimension: The dimension number to split. num_devices: The number of cores to split `tensor` over. input_shape: The shape of the original tensor. Raises: ValueError: The tensor to split was smaller in the split dimension than the number of devices to split over.
168166apply_to_tensortensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py128methodApplies this Sharding attribute to `tensor`. Args: tensor: A tf.Tensor to split. assign_tuple_sharding: If the sharding type should be a tuple.
169167prototensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py152methodReturn the sharding protobuf of type xla_data_pb2.OpSharding.
170168replicatetensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py179function
171169assign_devicetensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py188functionReturns a tensor that has AssignDevice sharding attribute.
172170tiletensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py202functionReturns a tensor that has tiled sharding. Args: tensor: A tf.Tensor to shard. tile_assignment: An np.ndarray describing the topology of the tiling and which device will compute which part of the topology. assign_tuple_sharding: If the sharding type should be a tuple. use_sharding_op: If true, adds a sharding op to set the sharding.
173171splittensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py224functionReturns a tensor that is split along the given dimension. Args: tensor: A tf.Tensor to split. split_dimension: The dimension to split. num_devices: The number of devices to partition the dimension. assign_tuple_sharding: If the sharding type should be a tuple. use_sharding_op: If true, adds a sharding op to set the sharding. input_shape: The full shape of the input tensor.
174172get_op_shardingtensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py248functionReturns sharding attribute of an op. Args: op: a TensorFlow op. Returns: The attribute representing XLA sharding on this op.
175173auto_to_manual_spmd_partitiontensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py260functionSwitches from automatic SPMD partitioning to manual partitioning. Converts a full-shaped tensor (to be automatically partitioned by SPMD partitioner) to a shard-shaped tensor to be consumed by manually partitioned ops. Args: tensor: A tf.Tensor in full shape. manual_sharding: a serialized string of OpSharding to be used in manual partitioning. Returns: A shard-shaped tensor to be consumed by manually partitioned ops.
176174manual_to_auto_spmd_partitiontensorflow/tensorflow/compiler/xla/experimental/xla_sharding/xla_sharding.py279functionSwitches from manual partitioning to automatic SPMD partitioning. Converts a shard-shaped tensor (manually partitioned in SPMD-style) to a full-shaped tensor to be partitioned automatically by the SPMD partitioner. Args: tensor: A tf.Tensor in shard shape. manual_sharding: a serialized string of OpSharding to be used in manual partitioning. full_shape: the shape of tensor before partitioning. Returns: A full-shaped tensor to be partitioned automatically by the SPMD partitioner.
177175numpy_assert_allclosetensorflow/tensorflow/compiler/xla/python/bfloat16_test.py35function
178176register_local_backend_factorytensorflow/tensorflow/compiler/xla/python/xla_client.py101function
179177get_local_backendtensorflow/tensorflow/compiler/xla/python/xla_client.py131functionReturns a local backend. Args: name: the backend name. If `None`, a default local backend is returned, typically `gpu` if one is present, or `cpu` if not. If a string, the named backend is returned or an exception raised. Returns: A LocalBackend object.
180178OpMetadatatensorflow/tensorflow/compiler/xla/python/xla_client.py152classPython representation of a xla.OpMetadata protobuf.
181179CurrentSourceInfoMetadatatensorflow/tensorflow/compiler/xla/python/xla_client.py163functionHelper for use in source mapping that returns an OpMetadata object.
182180dtype_to_etypetensorflow/tensorflow/compiler/xla/python/xla_client.py206functionConvenience function for reading DTYPE_TO_XLA_ELEMENT_TYPE.
183181shape_from_pyvaltensorflow/tensorflow/compiler/xla/python/xla_client.py272functionReturns a Shape that describes a tuple-tree of Numpy arrays.
184182execute_with_python_valuestensorflow/tensorflow/compiler/xla/python/xla_client.py334functionExecute on one replica with Python values as arguments and output.
185183execute_with_python_values_replicatedtensorflow/tensorflow/compiler/xla/python/xla_client.py345functionExecute on many replicas with Python values as arguments and output. Arguments: executable: the program to run. arguments: a list of lists of Python values indexed by `[replica][arg_num]` to pass as inputs. backend: the backend we are targeting. Returns: A list of python values, one per replica.
186184PaddingTypetensorflow/tensorflow/compiler/xla/python/xla_client.py374class
187185window_padding_type_to_pad_valuestensorflow/tensorflow/compiler/xla/python/xla_client.py379functionMaps PaddingType or string to pad values (list of pairs of ints).
188186register_custom_call_targettensorflow/tensorflow/compiler/xla/python/xla_client.py418functionRegisters a custom call target. Args: name: bytes containing the name of the function. fn: a PyCapsule object containing the function pointer. platform: the target platform.
189187PaddingConfigDimensiontensorflow/tensorflow/compiler/xla/python/xla_client.py433classPython representation of a xla.PaddingConfigDimension protobuf.
190188PaddingConfigtensorflow/tensorflow/compiler/xla/python/xla_client.py443classPython representation of a xla.PaddingConfig protobuf.
191189make_padding_configtensorflow/tensorflow/compiler/xla/python/xla_client.py451functionCreate PaddingConfig proto from list of triples of integers. Args: padding_config: either a PaddingConfig or a list of integer triples (edge_padding_low, edge_padding_high, interior_padding) representing the configuration of the padding operation. Returns: A `PaddingConfig` object.
192190DotDimensionNumberstensorflow/tensorflow/compiler/xla/python/xla_client.py476classPython representation of a xla.DotDimensionNumbers protobuf.
193191make_dot_dimension_numberstensorflow/tensorflow/compiler/xla/python/xla_client.py488functionBuilds a DotDimensionNumbers object from a specification. Args: dimension_numbers: either a `DotDimensionNumbers` or a nested tuple `((lhs_contract, rhs_contract), (lhs_batch, rhs_batch))` of lists of integers representing the dimensions to treat as contracting dimensions and batch dimensions on each input operand. Returns: A `DotDimensionNumbers` object.
194192ConvolutionDimensionNumberstensorflow/tensorflow/compiler/xla/python/xla_client.py516classPython representation of a xla.ConvolutionDimensionNumbers protobuf.
195193make_convolution_dimension_numberstensorflow/tensorflow/compiler/xla/python/xla_client.py536functionBuilds a ConvolutionDimensionNumbers object from a specification. Args: dimension_numbers: optional, either a ConvolutionDimensionNumbers object or a tuple (lhs_spec, rhs_spec, out_spec). Each element is a string of length N+2 identifying by position: (1) batch dimensions in lhs, rhs, and the output with the character 'N', (2) feature dimensions in lhs and the output with the character 'C', (3) input and output feature dimensions in rhs with the characters 'I' and 'O' respectively, and (4) spatial dimension correspondences between lhs, rhs, and the output using any distinct characters. For example, to indicate dimension numbers consistent with the Conv operation with two spatial dimensions, one could use ('NCHW', 'OIHW', 'NCHW'). As another example, to indicate dimension numbers consistent with the TensorFlow Conv2D operation, one could use ('NHWC', 'HWIO', 'NHWC'). When using the latter form of convolution dimension specification, window strides are associated with spatial dimension character labels according to the order in which the labels appear in the rhs_spec string, so that window_strides[0] is matched with the dimension corresponding to the first character appearing in rhs_spec that is not 'I' or 'O'. By default, use the same dimension numbering as Conv and ConvWithGeneralPadding. num_spatial_dimensions: the number of spatial dimensions. Returns: A `ConvolutionDimensionNumbers` object.
196194OpShardingtensorflow/tensorflow/compiler/xla/python/xla_client.py600classPython representation of a xla.OpSharding protobuf.
197195PrecisionConfigtensorflow/tensorflow/compiler/xla/python/xla_client.py614classPython representation of a xla.PrecisionConfig protobuf.
198196GatherDimensionNumberstensorflow/tensorflow/compiler/xla/python/xla_client.py624classPython representation of a xla.GatherDimensionNumbers protobuf.
199197ScatterDimensionNumberstensorflow/tensorflow/compiler/xla/python/xla_client.py636classPython representation of a xla.ScatterDimensionNumbers protobuf.
200198ReplicaGrouptensorflow/tensorflow/compiler/xla/python/xla_client.py648classPython representation of a xla.ReplicaGroup protobuf.
201199make_replica_groupstensorflow/tensorflow/compiler/xla/python/xla_client.py662function
202200tracebackstensorflow/tensorflow/compiler/xla/python/xla_client.py677functionContext manager that enables or disables traceback collection.
203201heap_profiletensorflow/tensorflow/compiler/xla/python/xla_client.py687functionReturns a gzipped pprof protocol buffer containing a heap profile.
204202TpuBackendtensorflow/tensorflow/compiler/xla/python/tpu_driver/client/tpu_client.py29classXLA backend implemented using the Tpu driver API.
205203createtensorflow/tensorflow/compiler/xla/python/tpu_driver/client/tpu_client.py36methodConstructs a Cloud TPU backend.
206204ConvertLiteralToNumpyArraytensorflow/tensorflow/compiler/xla/python_api/xla_literal.py28functionConverts a XLA literal to a Numpy array.
207205ConvertNumpyArrayToLiteraltensorflow/tensorflow/compiler/xla/python_api/xla_literal.py85functionConverts a Numpy array or a nested tuple thereof to an XLA literal.
208206Shapetensorflow/tensorflow/compiler/xla/python_api/xla_shape.py29classWraps a xla_data_pb2.ShapeProto message with a convenient Python type. Provides direct access to the underlying xla_data_pb2.ShapeProto message in the message attribute, along with accessor wrappers to the message's fields. Avoid direct access to .message unless interacting directly with protobuf APIs like CopyFrom. In other words, prefer hauling the shape around in a Shape, and only access .message when strictly required by the protobuf API.
209207element_typetensorflow/tensorflow/compiler/xla/python_api/xla_shape.py71method
210208is_tupletensorflow/tensorflow/compiler/xla/python_api/xla_shape.py74method
211209dimensionstensorflow/tensorflow/compiler/xla/python_api/xla_shape.py77method
212210tuple_shapestensorflow/tensorflow/compiler/xla/python_api/xla_shape.py82methodIf this is a tuple, returns its sequence of constituent Shape objects. Returns: Tuple sub-shapes. Raises: ValueError: if this is not a tuple.
213211layouttensorflow/tensorflow/compiler/xla/python_api/xla_shape.py95method
214212from_pyvaltensorflow/tensorflow/compiler/xla/python_api/xla_shape.py99method
215213CreateShapeFromNumpytensorflow/tensorflow/compiler/xla/python_api/xla_shape.py129functionCreate a Shape from a Numpy array or a nested tuple structure thereof. Args: value: Numpy array or (possibly nested) tuple structure that bottoms out in Numpy arrays. Returns: A Shape object.
216214CreateShapeFromDtypeAndTupletensorflow/tensorflow/compiler/xla/python_api/xla_shape.py147functionCreate a shape from a Numpy dtype and a sequence of nonnegative integers. Args: dtype: a numpy dtype, e.g. np.dtype('int32'). shape_tuple: a sequence of nonnegative integers. Returns: A Shape object.
217215load_graphtensorflow/tensorflow/examples/label_image/label_image.py26function
218216read_tensor_from_image_filetensorflow/tensorflow/examples/label_image/label_image.py38function
219217load_labelstensorflow/tensorflow/examples/label_image/label_image.py65function
220218MaybeDistributionScopetensorflow/tensorflow/examples/saved_model/integration_tests/distribution_strategy_utils.py48classProvides a context allowing no distribution strategy.
221219from_nametensorflow/tensorflow/examples/saved_model/integration_tests/distribution_strategy_utils.py52method
222220make_feature_extractortensorflow/tensorflow/examples/saved_model/integration_tests/export_mnist_cnn.py56functionReturns a Keras Model to compute a feature vector from MNIST images.
223221set_feature_extractor_hparamstensorflow/tensorflow/examples/saved_model/integration_tests/export_mnist_cnn.py72function
224222make_classifiertensorflow/tensorflow/examples/saved_model/integration_tests/export_mnist_cnn.py76functionReturns a Keras Model to classify MNIST using feature_extractor.
225223wrap_keras_model_for_exporttensorflow/tensorflow/examples/saved_model/integration_tests/export_mnist_cnn.py87functionWraps `model` for saving and loading as SavedModel.
226224write_vocabulary_filetensorflow/tensorflow/examples/saved_model/integration_tests/export_simple_text_embedding.py34functionWrite temporary vocab file for module construction.
227225TextEmbeddingModeltensorflow/tensorflow/examples/saved_model/integration_tests/export_simple_text_embedding.py44classText embedding model. A text embeddings model that takes a sentences on input and outputs the sentence embedding.
228226TextRnnModeltensorflow/tensorflow/examples/saved_model/integration_tests/export_text_rnn_model.py31classText RNN model. A full generative text RNN model that can train and decode sentences from a starting word.
229227traintensorflow/tensorflow/examples/saved_model/integration_tests/export_text_rnn_model.py81method
230228decode_greedytensorflow/tensorflow/examples/saved_model/integration_tests/export_text_rnn_model.py143method
231229MaybeRunScriptInsteadtensorflow/tensorflow/examples/saved_model/integration_tests/integration_scripts.py62function
232230load_reshaped_datatensorflow/tensorflow/examples/saved_model/integration_tests/mnist_util.py34functionReturns MNIST or Fashion MNIST or fake train and test data.
233231make_feature_extractortensorflow/tensorflow/examples/saved_model/integration_tests/use_mnist_cnn.py72functionLoad a pre-trained feature extractor and wrap it for use in Keras.
234232make_classifiertensorflow/tensorflow/examples/saved_model/integration_tests/use_mnist_cnn.py100functionReturns a Keras Model to classify MNIST using feature_extractor.
235233traintensorflow/tensorflow/examples/saved_model/integration_tests/use_model_in_sequential_keras.py35functionBuild a Keras model and train with mock data.
236234traintensorflow/tensorflow/examples/saved_model/integration_tests/use_text_embedding_in_dataset.py34functionBuild a Keras model and train with mock data.
237235StreamingAccuracyStatstensorflow/tensorflow/examples/speech_commands/accuracy_utils.py24classGet streaming accuracy statistics every time a new command is founded. Attributes: _how_many_gt: How many ground truths. _how_many_gt_matched: How many ground truths have been matched. _how_many_fp: How many commands have been fired as false positive. _how_many_c: How many commands have been fired correctly. _how_many_w: How many commands have been fired wrongly. _gt_occurrence: A list to record which commands and when it occurs in the input audio stream. _previous_c: A variable to record the last status of _how_many_c. _previous_w: A variable to record the last status of _how_many_w. _previous_fp: A variable to record the last status of _how_many_fp.
238236read_ground_truth_filetensorflow/tensorflow/examples/speech_commands/accuracy_utils.py52methodLoad ground truth and timestamp pairs and store it in time order.
239237deltatensorflow/tensorflow/examples/speech_commands/accuracy_utils.py64methodCompute delta of StreamingAccuracyStats against last status.
240238calculate_accuracy_statstensorflow/tensorflow/examples/speech_commands/accuracy_utils.py83methodCalculate accuracy statistics when a new commands is founded. Given ground truth and corresponding predictions founded by model, figure out how many were correct. Take a tolerance time, so that only predictions up to a point in time are considered. Args: found_words: A list of all founded commands up to now. up_to_time_ms: End timestamp of this audio piece. time_tolerance_ms: The tolerance milliseconds before and after up_to_time_ms to match a ground truth.
241239print_accuracy_statstensorflow/tensorflow/examples/speech_commands/accuracy_utils.py137methodWrite a human-readable description of the statistics to stdout.
242240create_inference_graphtensorflow/tensorflow/examples/speech_commands/freeze.py63functionCreates an audio model with the nodes needed for inference. Uses the supplied arguments to create a model, and inserts the input and output nodes that are needed to use the graph for inference. Args: wanted_words: Comma-separated list of the words we're trying to recognize. sample_rate: How many samples per second are in the input audio files. clip_duration_ms: How many samples to analyze for the audio pattern. clip_stride_ms: How often to run recognition. Useful for models with cache. window_size_ms: Time slice duration to estimate frequencies from. window_stride_ms: How far apart time slices should be. feature_bin_count: Number of frequency bands to analyze. model_architecture: Name of the kind of model to generate. preprocess: How the spectrogram is processed to produce features, for example 'mfcc', 'average', or 'micro'. Returns: Input and output tensor objects. Raises: Exception: If the preprocessing mode isn't recognized.
243241save_graph_deftensorflow/tensorflow/examples/speech_commands/freeze.py161functionWrites a graph def file out to disk. Args: file_name: Where to save the file. frozen_graph_def: GraphDef proto object to save.
244242save_saved_modeltensorflow/tensorflow/examples/speech_commands/freeze.py176functionWrites a SavedModel out to disk. Args: file_name: Where to save the file. sess: TensorFlow session containing the graph. input_tensor: Tensor object defining the input's properties. output_tensor: Tensor object defining the output's properties.
245243mix_in_audio_sampletensorflow/tensorflow/examples/speech_commands/generate_streaming_test_wav.py55functionMixes the sample data into the main track at the specified offset. Args: track_data: Numpy array holding main audio data. Modified in-place. track_offset: Where to mix the sample into the main track. sample_data: Numpy array of audio data to mix into the main track. sample_offset: Where to start in the audio sample. clip_duration: How long the sample segment is. sample_volume: Loudness to mix the sample in at. ramp_in: Length in samples of volume increase stage. ramp_out: Length in samples of volume decrease stage.
246244prepare_words_listtensorflow/tensorflow/examples/speech_commands/input_data.py58functionPrepends common tokens to the custom word list. Args: wanted_words: List of strings containing the custom words. Returns: List with the standard silence and unknown tokens added.
247245which_settensorflow/tensorflow/examples/speech_commands/input_data.py70functionDetermines which data partition the file should belong to. We want to keep files in the same training, validation, or testing sets even if new ones are added over time. This makes it less likely that testing samples will accidentally be reused in training when long runs are restarted for example. To keep this stability, a hash of the filename is taken and used to determine which set it should belong to. This determination only depends on the name and the set proportions, so it won't change as other files are added. It's also useful to associate particular files as related (for example words spoken by the same person), so anything after '_nohash_' in a filename is ignored for set determination. This ensures that 'bobby_nohash_0.wav' and 'bobby_nohash_1.wav' are always in the same set, for example. Args: filename: File path of the data sample. validation_percentage: How much of the data set to use for validation. testing_percentage: How much of the data set to use for testing. Returns: String, one of 'training', 'validation', or 'testing'.
248246load_wav_filetensorflow/tensorflow/examples/speech_commands/input_data.py118functionLoads an audio file and returns a float PCM-encoded array of samples. Args: filename: Path to the .wav file to load. Returns: Numpy array holding the sample data as floats between -1.0 and 1.0.
249247save_wav_filetensorflow/tensorflow/examples/speech_commands/input_data.py136functionSaves audio sample data to a .wav audio file. Args: filename: Path to save the file to. wav_data: 2D array of float PCM-encoded audio data. sample_rate: Samples per second to encode in the file.
250248get_features_rangetensorflow/tensorflow/examples/speech_commands/input_data.py160functionReturns the expected min/max for generated features. Args: model_settings: Information about the current model being trained. Returns: Min/max float pair holding the range of features. Raises: Exception: If preprocessing mode isn't recognized.
251249AudioProcessortensorflow/tensorflow/examples/speech_commands/input_data.py190classHandles loading, partitioning, and preparing audio training data.
252250maybe_download_and_extract_datasettensorflow/tensorflow/examples/speech_commands/input_data.py205methodDownload and extract data set tar file. If the data set we're using doesn't already exist, this function downloads it from the TensorFlow.org website and unpacks it into a directory. If the data_url is none, don't download anything and expect the data directory to contain the correct files already. Args: data_url: Web location of the tar file containing the data set. dest_directory: File path to extract data to.
253251prepare_data_indextensorflow/tensorflow/examples/speech_commands/input_data.py247methodPrepares a list of the samples organized by set and label. The training loop needs a list of all the available data, organized by which partition it should belong to, and with ground truth labels attached. This function analyzes the folders below the `data_dir`, figures out the right labels for each file based on the name of the subdirectory it belongs to, and uses a stable hash to assign it to a data set partition. Args: silence_percentage: How much of the resulting data should be background. unknown_percentage: How much should be audio outside the wanted classes. wanted_words: Labels of the classes we want to be able to recognize. validation_percentage: How much of the data set to use for validation. testing_percentage: How much of the data set to use for testing. Returns: Dictionary containing a list of file information for each set partition, and a lookup map for each class to determine its numeric index. Raises: Exception: If expected files are not found.
254252prepare_background_datatensorflow/tensorflow/examples/speech_commands/input_data.py333methodSearches a folder for background noise audio, and loads it into memory. It's expected that the background audio samples will be in a subdirectory named '_background_noise_' inside the 'data_dir' folder, as .wavs that match the sample rate of the training data, but can be much longer in duration. If the '_background_noise_' folder doesn't exist at all, this isn't an error, it's just taken to mean that no background noise augmentation should be used. If the folder does exist, but it's empty, that's treated as an error. Returns: List of raw PCM-encoded audio samples of background noise. Raises: Exception: If files aren't found in the folder.
255253prepare_processing_graphtensorflow/tensorflow/examples/speech_commands/input_data.py369methodBuilds a TensorFlow graph to apply the input distortions. Creates a graph that loads a WAVE file, decodes it, scales the volume, shifts it in time, adds in background noise, calculates a spectrogram, and then builds an MFCC fingerprint from that. This must be called with an active TensorFlow session running, and it creates multiple placeholder inputs, and one output: - wav_filename_placeholder_: Filename of the WAV to load. - foreground_volume_placeholder_: How loud the main clip should be. - time_shift_padding_placeholder_: Where to pad the clip. - time_shift_offset_placeholder_: How much to move the clip in time. - background_data_placeholder_: PCM sample data for background noise. - background_volume_placeholder_: Loudness of mixed-in background. - output_: Output 2D fingerprint of processed audio. Args: model_settings: Information about the current model being trained. summaries_dir: Path to save training summary information to. Raises: ValueError: If the preprocessing mode isn't recognized. Exception: If the preprocessor wasn't compiled in.
256254set_sizetensorflow/tensorflow/examples/speech_commands/input_data.py498methodCalculates the number of samples in the dataset partition. Args: mode: Which partition, must be 'training', 'validation', or 'testing'. Returns: Number of samples in the partition.
257255get_datatensorflow/tensorflow/examples/speech_commands/input_data.py509methodGather samples from the data set, applying transformations as needed. When the mode is 'training', a random selection of samples will be returned, otherwise the first N clips in the partition will be used. This ensures that validation always uses the same samples, reducing noise in the metrics. Args: how_many: Desired number of samples to return. -1 means the entire contents of this partition. offset: Where to start when fetching deterministically. model_settings: Information about the current model being trained. background_frequency: How many clips will have background noise, 0.0 to 1.0. background_volume_range: How loud the background noise will be. time_shift: How much to randomly shift the clips by in time. mode: Which partition to use, must be 'training', 'validation', or 'testing'. sess: TensorFlow session that was active when processor was created. Returns: List of sample data for the transformed samples, and list of label indexes Raises: ValueError: If background samples are too short.
258256get_features_for_wavtensorflow/tensorflow/examples/speech_commands/input_data.py612methodApplies the feature transformation process to the input_wav. Runs the feature generation process (generally producing a spectrogram from the input samples) on the WAV file. This can be useful for testing and verifying implementations being run on other platforms. Args: wav_filename: The path to the input audio file. model_settings: Information about the current model being trained. sess: TensorFlow session that was active when processor was created. Returns: Numpy data array containing the generated features.
259257get_unprocessed_datatensorflow/tensorflow/examples/speech_commands/input_data.py640methodRetrieve sample data for the given partition, with no transformations. Args: how_many: Desired number of samples to return. -1 means the entire contents of this partition. model_settings: Information about the current model being trained. mode: Which partition to use, must be 'training', 'validation', or 'testing'. Returns: List of sample data for the samples, and list of labels in one-hot form.
260258load_graphtensorflow/tensorflow/examples/speech_commands/label_wav.py43functionUnpersists graph from file as default graph.
261259load_labelstensorflow/tensorflow/examples/speech_commands/label_wav.py51functionRead in labels, one label per line.
262260run_graphtensorflow/tensorflow/examples/speech_commands/label_wav.py56functionRuns the audio data through the graph and prints predictions.
263261label_wavtensorflow/tensorflow/examples/speech_commands/label_wav.py77functionLoads the model and labels, and runs the inference to print predictions.
264262load_graphtensorflow/tensorflow/examples/speech_commands/label_wav_dir.py44functionUnpersists graph from file as default graph.
265263load_labelstensorflow/tensorflow/examples/speech_commands/label_wav_dir.py52functionRead in labels, one label per line.
266264run_graphtensorflow/tensorflow/examples/speech_commands/label_wav_dir.py57functionRuns the audio data through the graph and prints predictions.
267265label_wavtensorflow/tensorflow/examples/speech_commands/label_wav_dir.py85functionLoads the model and labels, and runs the inference to print predictions.
268266prepare_model_settingstensorflow/tensorflow/examples/speech_commands/models.py39functionCalculates common settings needed for all models. Args: label_count: How many classes are to be recognized. sample_rate: Number of audio samples per second. clip_duration_ms: Length of each audio clip to be analyzed. window_size_ms: Duration of frequency analysis window. window_stride_ms: How far to move in time between frequency windows. feature_bin_count: Number of frequency bins to use for analysis. preprocess: How the spectrogram is processed to produce features. Returns: Dictionary containing common settings. Raises: ValueError: If the preprocessing mode isn't recognized.
269267create_modeltensorflow/tensorflow/examples/speech_commands/models.py95functionBuilds a model of the requested architecture compatible with the settings. There are many possible ways of deriving predictions from a spectrogram input, so this function provides an abstract interface for creating different kinds of models in a black-box way. You need to pass in a TensorFlow node as the 'fingerprint' input, and this should output a batch of 1D features that describe the audio. Typically this will be derived from a spectrogram that's been run through an MFCC, but in theory it can be any feature vector of the size specified in model_settings['fingerprint_size']. The function will build the graph it needs in the current TensorFlow graph, and return the tensorflow output that will contain the 'logits' input to the softmax prediction process. If training flag is on, it will also return a placeholder node that can be used to control the dropout amount. See the implementations below for the possible model architectures that can be requested. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. model_architecture: String specifying which kind of model to create. is_training: Whether the model is going to be used for training. runtime_settings: Dictionary of information about the runtime. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder. Raises: Exception: If the architecture type isn't recognized.
270268load_variables_from_checkpointtensorflow/tensorflow/examples/speech_commands/models.py153functionUtility function to centralize checkpoint restoration. Args: sess: TensorFlow session. start_checkpoint: Path to saved checkpoint on disk.
271269create_single_fc_modeltensorflow/tensorflow/examples/speech_commands/models.py164functionBuilds a model with a single hidden fully-connected layer. This is a very simple model with just one matmul and bias layer. As you'd expect, it doesn't produce very accurate results, but it is very fast and simple, so it's useful for sanity testing. Here's the layout of the graph: (fingerprint_input) v [MatMul]<-(weights) v [BiasAdd]<-(bias) v Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder.
272270create_conv_modeltensorflow/tensorflow/examples/speech_commands/models.py207functionBuilds a standard convolutional model. This is roughly the network labeled as 'cnn-trad-fpool3' in the 'Convolutional Neural Networks for Small-footprint Keyword Spotting' paper: http://www.isca-speech.org/archive/interspeech_2015/papers/i15_1478.pdf Here's the layout of the graph: (fingerprint_input) v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MaxPool] v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MaxPool] v [MatMul]<-(weights) v [BiasAdd]<-(bias) v This produces fairly good quality results, but can involve a large number of weight parameters and computations. For a cheaper alternative from the same paper with slightly less accuracy, see 'low_latency_conv' below. During training, dropout nodes are introduced after each relu, controlled by a placeholder. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder.
273271create_low_latency_conv_modeltensorflow/tensorflow/examples/speech_commands/models.py333functionBuilds a convolutional model with low compute requirements. This is roughly the network labeled as 'cnn-one-fstride4' in the 'Convolutional Neural Networks for Small-footprint Keyword Spotting' paper: http://www.isca-speech.org/archive/interspeech_2015/papers/i15_1478.pdf Here's the layout of the graph: (fingerprint_input) v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MatMul]<-(weights) v [BiasAdd]<-(bias) v [MatMul]<-(weights) v [BiasAdd]<-(bias) v [MatMul]<-(weights) v [BiasAdd]<-(bias) v This produces slightly lower quality results than the 'conv' model, but needs fewer weight parameters and computations. During training, dropout nodes are introduced after the relu, controlled by a placeholder. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder.
274272create_low_latency_svdf_modeltensorflow/tensorflow/examples/speech_commands/models.py462functionBuilds an SVDF model with low compute requirements. This is based in the topology presented in the 'Compressing Deep Neural Networks using a Rank-Constrained Topology' paper: https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43813.pdf Here's the layout of the graph: (fingerprint_input) v [SVDF]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MatMul]<-(weights) v [BiasAdd]<-(bias) v [MatMul]<-(weights) v [BiasAdd]<-(bias) v [MatMul]<-(weights) v [BiasAdd]<-(bias) v This model produces lower recognition accuracy than the 'conv' model above, but requires fewer weight parameters and, significantly fewer computations. During training, dropout nodes are introduced after the relu, controlled by a placeholder. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. The node is expected to produce a 2D Tensor of shape: [batch, model_settings['fingerprint_width'] * model_settings['spectrogram_length']] with the features corresponding to the same time slot arranged contiguously, and the oldest slot at index [:, 0], and newest at [:, -1]. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. runtime_settings: Dictionary of information about the runtime. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder. Raises: ValueError: If the inputs tensor is incorrectly shaped.
275273create_tiny_conv_modeltensorflow/tensorflow/examples/speech_commands/models.py673functionBuilds a convolutional model aimed at microcontrollers. Devices like DSPs and microcontrollers can have very small amounts of memory and limited processing power. This model is designed to use less than 20KB of working RAM, and fit within 32KB of read-only (flash) memory. Here's the layout of the graph: (fingerprint_input) v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MatMul]<-(weights) v [BiasAdd]<-(bias) v This doesn't produce particularly accurate results, but it's designed to be used as the first stage of a pipeline, running on a low-energy piece of hardware that can always be on, and then wake higher-power chips when a possible utterance has been found, so that more accurate analysis can be done. During training, a dropout node is introduced after the relu, controlled by a placeholder. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder.
276274create_tiny_embedding_conv_modeltensorflow/tensorflow/examples/speech_commands/models.py765functionBuilds a convolutional model aimed at microcontrollers. Devices like DSPs and microcontrollers can have very small amounts of memory and limited processing power. This model is designed to use less than 20KB of working RAM, and fit within 32KB of read-only (flash) memory. Here's the layout of the graph: (fingerprint_input) v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [Conv2D]<-(weights) v [BiasAdd]<-(bias) v [Relu] v [MatMul]<-(weights) v [BiasAdd]<-(bias) v This doesn't produce particularly accurate results, but it's designed to be used as the first stage of a pipeline, running on a low-energy piece of hardware that can always be on, and then wake higher-power chips when a possible utterance has been found, so that more accurate analysis can be done. During training, a dropout node is introduced after the relu, controlled by a placeholder. Args: fingerprint_input: TensorFlow node that will output audio feature vectors. model_settings: Dictionary of information about the model. is_training: Whether the model is going to be used for training. Returns: TensorFlow node outputting logits results, and optionally a dropout placeholder.
277275RecognizeResulttensorflow/tensorflow/examples/speech_commands/recognize_commands.py25classSave recognition result temporarily. Attributes: founded_command: A string indicating the word just founded. Default value is '_silence_' score: An float representing the confidence of founded word. Default value is zero. is_new_command: A boolean indicating if the founded command is a new one against the last one. Default value is False.
278276founded_commandtensorflow/tensorflow/examples/speech_commands/recognize_commands.py43method
279277founded_commandtensorflow/tensorflow/examples/speech_commands/recognize_commands.py47method
280278scoretensorflow/tensorflow/examples/speech_commands/recognize_commands.py51method
281279scoretensorflow/tensorflow/examples/speech_commands/recognize_commands.py55method
282280is_new_commandtensorflow/tensorflow/examples/speech_commands/recognize_commands.py59method
283281is_new_commandtensorflow/tensorflow/examples/speech_commands/recognize_commands.py63method
284282RecognizeCommandstensorflow/tensorflow/examples/speech_commands/recognize_commands.py67classSmooth the inference results by using average window. Maintain a slide window over the audio stream, which adds new result(a pair of the 1.confidences of all classes and 2.the start timestamp of input audio clip) directly the inference produces one and removes the most previous one and other abnormal values. Then it smooth the results in the window to get the most reliable command in this period. Attributes: _label: A list containing commands at corresponding lines. _average_window_duration: The length of average window. _detection_threshold: A confidence threshold for filtering out unreliable command. _suppression_ms: Milliseconds every two reliable founded commands should apart. _minimum_count: An integer count indicating the minimum results the average window should cover. _previous_results: A deque to store previous results. _label_count: The length of label list. _previous_top_label: Last founded command. Initial value is '_silence_'. _previous_top_time: The timestamp of _previous results. Default is -np.inf.
285283load_graphtensorflow/tensorflow/examples/speech_commands/test_streaming_accuracy.py80functionRead a tensorflow model, and creates a default graph object.
286284read_label_filetensorflow/tensorflow/examples/speech_commands/test_streaming_accuracy.py92functionLoad a list of label.
287285read_wav_filetensorflow/tensorflow/examples/speech_commands/test_streaming_accuracy.py101functionLoad a wav file and return sample_rate and numpy data of float64 type.
288286verbosity_argtensorflow/tensorflow/examples/speech_commands/train.py480functionParses verbosity argument. Args: value: A member of tf.logging. Raises: ArgumentTypeError: Not an expected value.
289287requires_contribtensorflow/tensorflow/examples/speech_commands/train_test.py32function
290288DictStructtensorflow/tensorflow/examples/speech_commands/train_test.py44class
291289wav_to_featurestensorflow/tensorflow/examples/speech_commands/wav_to_features.py47functionConverts an audio file into its corresponding feature map. Args: sample_rate: Expected sample rate of the wavs. clip_duration_ms: Expected duration in milliseconds of the wavs. window_size_ms: How long each spectrogram timeslice is. window_stride_ms: How far to move in time between spectrogram timeslices. feature_bin_count: How many bins to use for the feature fingerprint. quantize: Whether to train the model for eight-bit deployment. preprocess: Spectrogram processing mode; "mfcc", "average" or "micro". input_wav: Path to the audio WAV file to read. output_c_file: Where to save the generated C source file.
292290create_modeltensorflow/tensorflow/examples/tf2_showcase/mnist.py69functionModel to recognize digits in the MNIST dataset. Network structure is equivalent to: https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/examples/tutorials/mnist/mnist_deep.py and https://github.com/tensorflow/models/blob/master/tutorials/image/mnist/convolutional.py But uses the tf.keras API. Returns: A tf.keras.Model.
293291mnist_datasetstensorflow/tensorflow/examples/tf2_showcase/mnist.py115function
294292losstensorflow/tensorflow/examples/tf2_showcase/mnist.py125function
295293compute_accuracytensorflow/tensorflow/examples/tf2_showcase/mnist.py131function
296294traintensorflow/tensorflow/examples/tf2_showcase/mnist.py140functionTrains model on `dataset` using `optimizer`.
297295train_and_exporttensorflow/tensorflow/examples/tf2_showcase/mnist.py184functionRun MNIST training and eval loop in eager mode. Args: flags_obj: An object containing parsed flag values.
298296import_and_evaltensorflow/tensorflow/examples/tf2_showcase/mnist.py237function
299297apply_cleantensorflow/tensorflow/examples/tf2_showcase/mnist.py247function
300298placeholder_inputstensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py37functionGenerate placeholder variables to represent the input tensors. These placeholders are used as inputs by the rest of the model building code and will be fed from the downloaded data in the .run() loop, below. Args: batch_size: The batch size will be baked into both placeholders. Returns: images_placeholder: Images placeholder. labels_placeholder: Labels placeholder.
301299fill_feed_dicttensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py59functionFills the feed_dict for training the given step. A feed_dict takes the form of: feed_dict = { <placeholder>: <tensor of values to be passed for placeholder>, .... } Args: data_set: The set of images and labels, from input_data.read_data_sets() images_pl: The images placeholder, from placeholder_inputs(). labels_pl: The labels placeholder, from placeholder_inputs(). Returns: feed_dict: The feed dictionary mapping from placeholders to values.
302300do_evaltensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py87functionRuns one evaluation against the full epoch of data. Args: sess: The session in which the model has been trained. eval_correct: The Tensor that returns the number of correct predictions. images_placeholder: The images placeholder. labels_placeholder: The labels placeholder. data_set: The set of images and labels to evaluate, from input_data.read_data_sets().
303301run_trainingtensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py116functionTrain MNIST for a number of steps.
304302read_data_setstensorflow/tensorflow/examples/tutorials/mnist/input_data.py266function
305303inferencetensorflow/tensorflow/examples/tutorials/mnist/mnist.py45functionBuild the MNIST model up to where it may be used for inference. Args: images: Images placeholder, from inputs(). hidden1_units: Size of the first hidden layer. hidden2_units: Size of the second hidden layer. Returns: softmax_linear: Output tensor with the computed logits.
306304losstensorflow/tensorflow/examples/tutorials/mnist/mnist.py86functionCalculates the loss from the logits and the labels. Args: logits: Logits tensor, float - [batch_size, NUM_CLASSES]. labels: Labels tensor, int32 - [batch_size]. Returns: loss: Loss tensor of type float.
307305trainingtensorflow/tensorflow/examples/tutorials/mnist/mnist.py101functionSets up the training Ops. Creates a summarizer to track the loss over time in TensorBoard. Creates an optimizer and applies the gradients to all trainable variables. The Op returned by this function is what must be passed to the `sess.run()` call to cause the model to train. Args: loss: Loss tensor, from loss(). learning_rate: The learning rate to use for gradient descent. Returns: train_op: The Op for training.
308306evaluationtensorflow/tensorflow/examples/tutorials/mnist/mnist.py130functionEvaluate the quality of the logits at predicting the label. Args: logits: Logits tensor, float - [batch_size, NUM_CLASSES]. labels: Labels tensor, int32 - [batch_size], with values in the range [0, NUM_CLASSES). Returns: A scalar int32 tensor with the number of examples (out of batch_size) that were predicted correctly.
309307traintensorflow/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py38function
310308word2vec_basictensorflow/tensorflow/examples/tutorials/word2vec/word2vec_basic.py49functionExample of building, training and visualizing a word2vec model.
311309suppress_exceptiontensorflow/tensorflow/lite/examples/experimental_new_converter/stack_trace_example.py37function
312310load_labelstensorflow/tensorflow/lite/examples/python/label_image.py29function
313311dynamic_rnntensorflow/tensorflow/lite/experimental/examples/lstm/rnn.py42functionCreates a recurrent neural network specified by RNNCell `cell`. Performs fully dynamic unrolling of `inputs`. Example: ```python # create a BasicRNNCell rnn_cell = tf.compat.v1.nn.rnn_cell.BasicRNNCell(hidden_size) # 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size] # defining initial state initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) # 'state' is a tensor of shape [batch_size, cell_state_size] outputs, state = tf.compat.v1.nn.dynamic_rnn(rnn_cell, input_data, initial_state=initial_state, dtype=tf.float32) ``` ```python # create 2 LSTMCells rnn_layers = [tf.compat.v1.nn.rnn_cell.LSTMCell(size) for size in [128, 256]] # create a RNN cell composed sequentially of a number of RNNCells multi_rnn_cell = tf.compat.v1.nn.rnn_cell.MultiRNNCell(rnn_layers) # 'outputs' is a tensor of shape [batch_size, max_time, 256] # 'state' is a N-tuple where N is the number of LSTMCells containing a # tf.nn.rnn_cell.LSTMStateTuple for each cell outputs, state = tf.compat.v1.nn.dynamic_rnn(cell=multi_rnn_cell, inputs=data, dtype=tf.float32) ``` Args: cell: An instance of RNNCell. inputs: The RNN inputs. If `time_major == False` (default), this must be a `Tensor` of shape: `[batch_size, max_time, ...]`, or a nested tuple of such elements. If `time_major == True`, this must be a `Tensor` of shape: `[max_time, batch_size, ...]`, or a nested tuple of such elements. This may also be a (possibly nested) tuple of Tensors satisfying this property. The first two dimensions must match across all the inputs, but otherwise the ranks and other shape components may differ. In this case, input to `cell` at each time-step will replicate the structure of these tuples, except for the time dimension (from which the time is taken). The input to `cell` at each time step will be a `Tensor` or (possibly nested) tuple of Tensors each with dimensions `[batch_size, ...]`. sequence_length: (optional) An int32/int64 vector sized `[batch_size]`. Used to copy-through state and zero-out outputs when past a batch element's sequence length. So it's more for performance than correctness. initial_state: (optional) An initial state for the RNN. If `cell.state_size` is an integer, this must be a `Tensor` of appropriate type and shape `[batch_size, cell.state_size]`. If `cell.state_size` is a tuple, this should be a tuple of tensors having shapes `[batch_size, s] for s in cell.state_size`. dtype: (optional) The data type for the initial state and expected output. Required if initial_state is not provided or RNN state has a heterogeneous dtype. parallel_iterations: (Default: 32). The number of iterations to run in parallel. Those operations which do not have any temporal dependency and can be run in parallel, will be. This parameter trades off time for space. Values >> 1 use more memory but take less time, while smaller values use less memory but computations take longer. swap_memory: Transparently swap the tensors produced in forward inference but needed for back prop from GPU to CPU. This allows training RNNs which would typically not fit on a single GPU, with very minimal (or no) performance penalty. time_major: The shape format of the `inputs` and `outputs` Tensors. If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. Using `time_major = True` is a bit more efficient because it avoids transposes at the beginning and end of the RNN calculation. However, most TensorFlow data is batch-major, so by default this function accepts input and emits output in batch-major form. scope: VariableScope for the created subgraph; defaults to "rnn". Returns: A pair (outputs, state) where: outputs: The RNN output `Tensor`. If time_major == False (default), this will be a `Tensor` shaped: `[batch_size, max_time, cell.output_size]`. If time_major == True, this will be a `Tensor` shaped: `[max_time, batch_size, cell.output_size]`. Note, if `cell.output_size` is a (possibly nested) tuple of integers or `TensorShape` objects, then `outputs` will be a tuple having the same structure as `cell.output_size`, containing Tensors having shapes corresponding to the shape data in `cell.output_size`. state: The final state. If `cell.state_size` is an int, this will be shaped `[batch_size, cell.state_size]`. If it is a `TensorShape`, this will be shaped `[batch_size] + cell.state_size`. If it is a (possibly nested) tuple of ints or `TensorShape`, this will be a tuple having the corresponding shapes. If cells are `LSTMCells` `state` will be a tuple containing a `LSTMStateTuple` for each cell. Raises: TypeError: If `cell` is not an instance of RNNCell. ValueError: If inputs is None or an empty list. RuntimeError: If not using control flow v2.
314312bidirectional_dynamic_rnntensorflow/tensorflow/lite/experimental/examples/lstm/rnn.py279functionCreates a dynamic version of bidirectional recurrent neural network. Takes input and builds independent forward and backward RNNs. The input_size of forward and backward cell must match. The initial state for both directions is zero by default (but can be set optionally) and no intermediate states are ever returned -- the network is fully unrolled for the given (passed in) length(s) of the sequence(s) or completely unrolled if length(s) is not given. Args: cell_fw: An instance of RNNCell, to be used for forward direction. cell_bw: An instance of RNNCell, to be used for backward direction. inputs: The RNN inputs. If time_major == False (default), this must be a tensor of shape: `[batch_size, max_time, ...]`, or a nested tuple of such elements. If time_major == True, this must be a tensor of shape: `[max_time, batch_size, ...]`, or a nested tuple of such elements. sequence_length: (optional) An int32/int64 vector, size `[batch_size]`, containing the actual lengths for each of the sequences in the batch. If not provided, all batch entries are assumed to be full sequences; and time reversal is applied from time `0` to `max_time` for each sequence. initial_state_fw: (optional) An initial state for the forward RNN. This must be a tensor of appropriate type and shape `[batch_size, cell_fw.state_size]`. If `cell_fw.state_size` is a tuple, this should be a tuple of tensors having shapes `[batch_size, s] for s in cell_fw.state_size`. initial_state_bw: (optional) Same as for `initial_state_fw`, but using the corresponding properties of `cell_bw`. dtype: (optional) The data type for the initial states and expected output. Required if initial_states are not provided or RNN states have a heterogeneous dtype. parallel_iterations: (Default: 32). The number of iterations to run in parallel. Those operations which do not have any temporal dependency and can be run in parallel, will be. This parameter trades off time for space. Values >> 1 use more memory but take less time, while smaller values use less memory but computations take longer. swap_memory: Transparently swap the tensors produced in forward inference but needed for back prop from GPU to CPU. This allows training RNNs which would typically not fit on a single GPU, with very minimal (or no) performance penalty. time_major: The shape format of the `inputs` and `outputs` Tensors. If true, these `Tensors` must be shaped `[max_time, batch_size, depth]`. If false, these `Tensors` must be shaped `[batch_size, max_time, depth]`. Using `time_major = True` is a bit more efficient because it avoids transposes at the beginning and end of the RNN calculation. However, most TensorFlow data is batch-major, so by default this function accepts input and emits output in batch-major form. scope: VariableScope for the created subgraph; defaults to "bidirectional_rnn" Returns: A tuple (outputs, output_states) where: outputs: A tuple (output_fw, output_bw) containing the forward and the backward rnn output `Tensor`. If time_major == False (default), output_fw will be a `Tensor` shaped: `[batch_size, max_time, cell_fw.output_size]` and output_bw will be a `Tensor` shaped: `[batch_size, max_time, cell_bw.output_size]`. If time_major == True, output_fw will be a `Tensor` shaped: `[max_time, batch_size, cell_fw.output_size]` and output_bw will be a `Tensor` shaped: `[max_time, batch_size, cell_bw.output_size]`. It returns a tuple instead of a single concatenated `Tensor`, unlike in the `bidirectional_rnn`. If the concatenated one is preferred, the forward and backward outputs can be concatenated as `tf.concat(outputs, 2)`. output_states: A tuple (output_state_fw, output_state_bw) containing the forward and the backward final states of bidirectional rnn. Raises: TypeError: If `cell_fw` or `cell_bw` is not an instance of `RNNCell`.
315313TfLiteRNNCelltensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py39classThe most basic RNN cell. This is used only for TfLite, it provides hints and it also makes the variables in the desired for the tflite ops.
316314state_sizetensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py88method
317315output_sizetensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py92method
318316buildtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py95methodBuilds the RNN cell. Args: inputs_shape: Rnn input tensor shape. Raises: ValueError: If last dimension of the input shape is not known.
319317calltensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py127methodMost basic RNN: output = new_state = act(W * input + U * state + B).
320318get_configtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py150method
321319add_variable_wrappedtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py110method
322320TFLiteLSTMCelltensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py162classLong short-term memory unit (LSTM) recurrent network cell. This is used only for TfLite, it provides hints and it also makes the variables in the desired for the tflite ops (transposed and separated). The default non-peephole implementation is based on: https://pdfs.semanticscholar.org/1154/0131eae85b2e11d53df7f1360eeb6476e7f4.pdf Felix Gers, Jurgen Schmidhuber, and Fred Cummins. "Learning to forget: Continual prediction with LSTM." IET, 850-855, 1999. The peephole implementation is based on: https://research.google.com/pubs/archive/43905.pdf Hasim Sak, Andrew Senior, and Francoise Beaufays. "Long short-term memory recurrent neural network architectures for large scale acoustic modeling." INTERSPEECH, 2014. The class uses optional peep-hole connections, optional cell clipping, and an optional projection layer. Note that this cell is not optimized for performance. Please use `tf.contrib.cudnn_rnn.CudnnLSTM` for better performance on GPU, or `tf.contrib.rnn.LSTMBlockCell` and `tf.contrib.rnn.LSTMBlockFusedCell` for better performance on CPU.
323321state_sizetensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py284method
324322output_sizetensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py288method
325323buildtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py291methodBuild TfLite LSTM cell graph. Args: inputs_shape: The inputs_shape must be known, and is [batch_size, input_size] shape. Raises: ValueError: if the inputs_shape is invalid.
326324calltensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py392methodRun one step of LSTM. Args: inputs: input Tensor, 2D, `[batch, num_units]`. state: if `state_is_tuple` is False, this must be a state Tensor, `2-D, [batch, state_size]`. If `state_is_tuple` is True, this must be a tuple of state Tensors, both `2-D`, with column sizes `c_state` and `m_state`. Returns: A tuple containing: - A `2-D, [batch, output_dim]`, Tensor representing the output of the LSTM after reading `inputs` when previous state was `state`. Here output_dim is: num_proj if num_proj was set, num_units otherwise. - Tensor(s) representing the new state of LSTM after reading `inputs` when the previous state was `state`. Same type and shape(s) as `state`. Raises: ValueError: If input size cannot be inferred from inputs via static shape inference.
327325get_configtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py519method
328326add_variable_wrappedtensorflow/tensorflow/lite/experimental/examples/lstm/rnn_cell.py317method
329327audio_microfrontendtensorflow/tensorflow/lite/experimental/microfrontend/python/ops/audio_microfrontend_op.py34functionAudio Microfrontend Op. This Op converts a sequence of audio data into one or more feature vectors containing filterbanks of the input. The conversion process uses a lightweight library to perform: 1. A slicing window function 2. Short-time FFTs 3. Filterbank calculations 4. Noise reduction 5. PCAN Auto Gain Control 6. Logarithmic scaling Args: audio: 1D Tensor, int16 audio data in temporal ordering. sample_rate: Integer, the sample rate of the audio in Hz. window_size: Integer, length of desired time frames in ms. window_step: Integer, length of step size for the next frame in ms. num_channels: Integer, the number of filterbank channels to use. upper_band_limit: Float, the highest frequency included in the filterbanks. lower_band_limit: Float, the lowest frequency included in the filterbanks. smoothing_bits: Int, scale up signal by 2^(smoothing_bits) before reduction. even_smoothing: Float, smoothing coefficient for even-numbered channels. odd_smoothing: Float, smoothing coefficient for odd-numbered channels. min_signal_remaining: Float, fraction of signal to preserve in smoothing. enable_pcan: Bool, enable PCAN auto gain control. pcan_strength: Float, gain normalization exponent. pcan_offset: Float, positive value added in the normalization denominator. gain_bits: Int, number of fractional bits in the gain. enable_log: Bool, enable logarithmic scaling of filterbanks. scale_shift: Integer, scale filterbanks by 2^(scale_shift). left_context: Integer, number of preceding frames to attach to each frame. right_context: Integer, number of preceding frames to attach to each frame. frame_stride: Integer, M frames to skip over, where output[n] = frame[n*M]. zero_padding: Bool, if left/right context is out-of-bounds, attach frame of zeroes. Otherwise, frame[0] or frame[size-1] will be copied. out_scale: Integer, divide all filterbanks by this number. out_type: DType, type of the output Tensor, defaults to UINT16. Returns: filterbanks: 2D Tensor, each row is a time frame, each column is a channel. Raises: ValueError: If the audio tensor is not explicitly a vector.
330328SupportedOptensorflow/tensorflow/lite/experimental/tensorboard/ops_util.py26classSpec of supported ops. Args: op: string of op name.
331329get_potentially_supported_opstensorflow/tensorflow/lite/experimental/tensorboard/ops_util.py35functionReturns operations potentially supported by TensorFlow Lite. The potentially support list contains a list of ops that are partially or fully supported, which is derived by simply scanning op names to check whether they can be handled without real conversion and specific parameters. Given that some ops may be partially supported, the optimal way to determine if a model's operations are supported is by converting using the TensorFlow Lite converter. Returns: A list of SupportedOp.
332330time_wrappingtensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_augmentation.py29functionGenerate (molecule/denominator)x speed data.
333331augment_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_augmentation.py43functionPerform data augmentation.
334332DataLoadertensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_load.py35classLoads data and prepares for training.
335333get_data_filetensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_load.py50methodGet train, valid and test data from files.
336334padtensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_load.py66methodGet neighbour padding.
337335format_support_functensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_load.py81methodSupport function for format.(Helps format train, valid and test.)
338336formattensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_load.py98methodFormat data(including padding, etc.) and get the dataset for the model.
339337prepare_original_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_prepare.py46functionRead collected data from files.
340338generate_negative_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_prepare.py86functionGenerate negative data labeled as 'negative6~8'.
341339write_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_prepare.py143function
342340read_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_split.py40function
343341split_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_split.py51functionSplits data into train, validation and test according to ratio.
344342person_splittensorflow/tensorflow/lite/micro/examples/magic_wand/train/data_split_person.py41functionSplit data by person.
345343reshape_functiontensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py37function
346344calculate_model_sizetensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py42function
347345build_cnntensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py51functionBuilds a convolutional neural network in Keras.
348346build_lstmtensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py78functionBuilds an LSTM in Keras.
349347load_datatensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py93function
350348build_nettensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py101function
351349train_nettensorflow/tensorflow/lite/micro/examples/magic_wand/train/train.py111functionTrains the model.
352350to_cctensorflow/tensorflow/lite/micro/examples/micro_speech/CMSIS/create_constants.py26functionWrites table values to a C++ source file.
353351to_htensorflow/tensorflow/lite/micro/examples/micro_speech/CMSIS/create_constants.py44functionWrites a header file for the table values.
354352new_data_to_arraytensorflow/tensorflow/lite/micro/examples/micro_speech/apollo3/captured_data_to_wav.py28function
355353new_data_to_arraytensorflow/tensorflow/lite/micro/examples/micro_speech/apollo3/compare_1k.py29functionConverts file information to an in-memory array.
356354to_floattensorflow/tensorflow/lite/micro/examples/micro_speech/apollo3/compare_1k.py63function
357355check_file_existencetensorflow/tensorflow/lite/micro/examples/person_detection/utils/raw_to_bitmap.py52function
358356show_and_save_bitmapstensorflow/tensorflow/lite/micro/examples/person_detection/utils/raw_to_bitmap.py60functionDisplay and save a list of bitmaps. Args: input_file: input file name bitmap_list: list of numpy arrays to represent bitmap images channels: color channel count
359357reshape_bitmapstensorflow/tensorflow/lite/micro/examples/person_detection/utils/raw_to_bitmap.py87functionReshape flat integer arrays. Args: frame_list: list of 1-D arrays to represent raw image data width: image width in pixels height: image height in pixels channels: color channel count Returns: list of numpy arrays to represent bitmap images
360358parse_filetensorflow/tensorflow/lite/micro/examples/person_detection/utils/raw_to_bitmap.py109functionConvert log file to array of pixels. Args: inputfile: log file to parse width: image width in pixels height: image height in pixels channels: color channel count Returns: list 1-D arrays to represent raw image data.
361359generate_conv_modeltensorflow/tensorflow/lite/micro/testing/generate_test_models.py34functionCreates a basic Keras model and converts to tflite. This model does not make any relevant classifications. It only exists to generate a model that is designed to run on embedded devices.
362360rename_example_subfolder_filestensorflow/tensorflow/lite/micro/tools/make/fix_arduino_subfolders.py29functionMoves source files in example subfolders to equivalents at root.
363361move_person_datatensorflow/tensorflow/lite/micro/tools/make/fix_arduino_subfolders.py41functionMoves the downloaded person model into the examples folder.
364362move_person_data_experimentaltensorflow/tensorflow/lite/micro/tools/make/fix_arduino_subfolders.py61functionMoves the downloaded person model into the examples folder.
365363move_image_data_experimentaltensorflow/tensorflow/lite/micro/tools/make/fix_arduino_subfolders.py83functionMoves the downloaded image detection model into the examples folder.
366364parse_argstensorflow/tensorflow/lite/micro/tools/make/fix_arduino_subfolders.py124functionConverts the raw arguments into accessible flags.
367365sanitize_xmltensorflow/tensorflow/lite/micro/tools/make/generate_keil_project.py29functionUses a allowlist to avoid generating bad XML.
368366parse_argstensorflow/tensorflow/lite/micro/tools/make/generate_keil_project.py82functionConverts the raw arguments into accessible flags.
369367parse_argstensorflow/tensorflow/lite/micro/tools/make/merge_arduino_zips.py39functionConverts the raw arguments into accessible flags.
370368replace_includestensorflow/tensorflow/lite/micro/tools/make/transform_arduino_source.py29functionUpdates any includes to reference the new Arduino library paths.
371369check_ino_functionstensorflow/tensorflow/lite/micro/tools/make/transform_arduino_source.py51functionEnsures the required functions exist.
372370add_example_ino_library_includetensorflow/tensorflow/lite/micro/tools/make/transform_arduino_source.py65functionMakes sure the example includes the header that loads the library.
373371replace_example_includestensorflow/tensorflow/lite/micro/tools/make/transform_arduino_source.py71functionUpdates any includes for local example files.
374372parse_argstensorflow/tensorflow/lite/micro/tools/make/transform_arduino_source.py108functionConverts the raw arguments into accessible flags.
375373replace_arduino_includestensorflow/tensorflow/lite/micro/tools/make/transform_source.py36functionUpdates any includes to reference the new Arduino library paths.
376374check_ino_functionstensorflow/tensorflow/lite/micro/tools/make/transform_source.py58functionEnsures the required functions exist.
377375add_example_ino_library_includetensorflow/tensorflow/lite/micro/tools/make/transform_source.py72functionMakes sure the example includes the header that loads the library.
378376replace_arduino_example_includestensorflow/tensorflow/lite/micro/tools/make/transform_source.py78functionUpdates any includes for local example files.
379377replace_esp_example_includestensorflow/tensorflow/lite/micro/tools/make/transform_source.py92functionUpdates any includes for local example files.
380378transform_arduino_sourcestensorflow/tensorflow/lite/micro/tools/make/transform_source.py109functionTransform sources for the Arduino platform. Args: input_lines: A sequence of lines from the input file to process. flags: Flags indicating which transformation(s) to apply. Returns: The transformed output as a string.
381379transform_esp_sourcestensorflow/tensorflow/lite/micro/tools/make/transform_source.py138functionTransform sources for the ESP-IDF platform. Args: input_lines: A sequence of lines from the input file to process. flags: Flags indicating which transformation(s) to apply. Returns: The transformed output as a string.
382380parse_argstensorflow/tensorflow/lite/micro/tools/make/transform_source.py171functionConverts the raw arguments into accessible flags.
383381OpsSettensorflow/tensorflow/lite/python/convert.py80classEnum class defining the sets of ops available to generate TFLite models. WARNING: Experimental interface, subject to change.
384382get_optionstensorflow/tensorflow/lite/python/convert.py115methodReturns a list of OpsSet options as a list of strings.
385383ConverterErrortensorflow/tensorflow/lite/python/convert.py120classRaised when an error occurs during model conversion.
386384mlir_quantizetensorflow/tensorflow/lite/python/convert.py125functionQuantize `input_data_str` with calibration results. Args: input_data_str: Input data in serialized form (e.g. a TFLITE model with calibration results). disable_per_channel: Bool indicating whether to do per-channel or per-tensor quantization fully_quantize: Bool indicating whether to fully quantize the model. Besides model body, the input/output will be quantized as well. inference_type: Data type for the activations. The default value is int8. Returns: Quantized model in serialized form (e.g. a TFLITE model) with floating-point inputs and outputs.
387385mlir_sparsifytensorflow/tensorflow/lite/python/convert.py150functionSparsify `input_data_str` to encode sparse tensor with proper format. Args: input_data_str: Input data in serialized form (e.g. a TFLITE model). Returns: Sparsified model in serialized form (e.g. a TFLITE model).
388386toco_convert_protostensorflow/tensorflow/lite/python/convert.py162functionConvert `input_data_str` according to model and toco parameters. Unless you know what you are doing consider using the more friendly `tf.compat.v1.lite.toco_convert`. Args: model_flags_str: Serialized proto describing model properties, see `toco/model_flags.proto`. toco_flags_str: Serialized proto describing conversion properties, see `toco/toco_flags.proto`. input_data_str: Input data in serialized form (e.g. a graphdef is common) debug_info_str: Serialized `GraphDebugInfo` proto describing logging information. (default None) enable_mlir_converter: Enables MLIR-based conversion instead of the default TOCO conversion. (default False) Returns: Converted model in serialized form (e.g. a TFLITE model is common). Raises: ConverterError: When conversion fails in TFLiteConverter, usually due to ops not being supported. RuntimeError: When conversion fails, an exception is raised with the error message embedded.
389387build_toco_convert_protostensorflow/tensorflow/lite/python/convert.py291functionBuilds protocol buffers describing a conversion of a model using TOCO. Typically this is to convert from TensorFlow GraphDef to TFLite, in which case the default `input_format` and `output_format` are sufficient. Args: input_tensors: List of input tensors. Type and shape are computed using `foo.shape` and `foo.dtype`. output_tensors: List of output tensors (only .name is used from this). inference_type: Target data type of real-number arrays in the output file. Must be `{tf.float32, tf.uint8, tf.int8}`. (default tf.float32) inference_input_type: Target data type of real-number input arrays. Allows for a different type for input arrays in the case of quantization. Must be `{tf.float32, tf.uint8, tf.int8}`. (default `inference_type`) input_format: Type of data to read Currently must be `{TENSORFLOW_GRAPHDEF}`. (default TENSORFLOW_GRAPHDEF) input_shapes: Input array shape. It needs to be a list of the same length as `input_tensors`, or None. (default None) output_format: Output file format. Currently must be `{TFLITE, GRAPHVIZ_DOT}`. (default TFLITE) quantized_input_stats: List of tuples of floats representing the mean and standard deviation. Each tuple maps to the corresponding input tensor. Only need if `inference_input_type` is `QUANTIZED_UINT8` or `INT8`. real_input_value = (quantized_input_value - mean_value) / std_dev_value. (default None) default_ranges_stats: Tuple of integers representing (min, max) range values for all arrays without a specified range. Intended for experimenting with quantization via "dummy quantization". (default None) drop_control_dependency: Boolean indicating whether to drop control dependencies silently. This is due to TFLite not supporting control dependencies. (default True) reorder_across_fake_quant: Boolean indicating whether to reorder FakeQuant nodes in unexpected locations. Used when the location of the FakeQuant nodes is preventing graph transformations necessary to convert the graph. Results in a graph that differs from the quantized training graph, potentially causing differing arithmetic behavior. (default False) allow_custom_ops: Boolean indicating whether to allow custom operations. When false any unknown operation is an error. When true, custom ops are created for any op that is unknown. The developer will need to provide these to the TensorFlow Lite runtime with a custom resolver. (default False) custom_opdefs: List of strings representing custom ops OpDefs that are included in the GraphDef. Required when using custom operations with the MLIR-based converter. (default None) change_concat_input_ranges: Boolean to change behavior of min/max ranges for inputs and outputs of the concat operator for quantized models. Changes the ranges of concat operator overlap when true. (default False) post_training_quantize: Boolean indicating whether to quantize the weights of the converted float model. Model size will be reduced and there will be latency improvements (at the cost of accuracy). (default False) quantize_to_float16: Boolean indicating whether to convert float buffers to float16. (default False) dump_graphviz_dir: Full filepath of folder to dump the graphs at various stages of processing GraphViz .dot files. Preferred over --output_format=GRAPHVIZ_DOT in order to keep the requirements of the output file. (default None) dump_graphviz_video: Boolean indicating whether to dump the graph after every graph transformation. (default False) target_ops: Experimental flag, subject to change. Set of OpsSet options indicating which converter to use. (default set([OpsSet.TFLITE_BUILTINS])) allow_nonexistent_arrays: Allow specifying array names that don't exist or are unused in the final graph. (default False) debug_info: `GraphDebugInfo` proto containing the stack traces for the original nodes referred by the converted graph. conversion_summary_dir: A string, the path to the generated conversion logs. saved_model_dir: Filepath of the saved model to be converted. This value will be non-empty only when the saved model import path will be used. Otherwises, the graph def-based conversion will be processed. saved_model_version: SavedModel file format version of The saved model file to be converted. This value will be set only when the SavedModel import path will be used. saved_model_tags: Set of string saved model tags, formatted in the comma-separated value. This value will be set only when the SavedModel import path will be used. saved_model_exported_names: Names to be exported (default: export all) when the saved model import path is on. This value will be set only when the SavedModel import path will be used. Returns: model_flags, toco_flags, debug_info: three protocol buffers describing the conversion process and debug information. Raises: ValueError: If the input tensor type is unknown Missing mean_values or std_dev_values RuntimeError: If TOCO fails to convert (in which case the runtime error's error text will contain the TOCO error log)
390388toco_convert_graph_deftensorflow/tensorflow/lite/python/convert.py485function"Convert a model using TOCO. This function is used to convert GraphDefs that cannot be loaded into TensorFlow to TFLite. Conversion can be customized by providing arguments that are forwarded to `build_toco_convert_protos` (see documentation for details). Args: input_data: Input data (i.e. often `sess.graph_def`), input_arrays_with_shape: Tuple of strings representing input tensor names and list of integers representing input shapes (e.g., [("foo" : [1, 16, 16, 3])]). Use only when graph cannot be loaded into TensorFlow and when `input_tensors` is None. (default None) output_arrays: List of output tensors to freeze graph with. Use only when graph cannot be loaded into TensorFlow and when `output_tensors` is None. (default None) enable_mlir_converter: Enables MLIR-based conversion instead of TOCO conversion. *args: See `build_toco_convert_protos`, **kwargs: See `build_toco_convert_protos`. Returns: The converted data. For example if TFLite was the destination, then this will be a tflite flatbuffer in a bytes array. Raises: Defined in `build_toco_convert_protos`.
391389toco_convert_impltensorflow/tensorflow/lite/python/convert.py541function"Convert a model using TOCO. Typically this function is used to convert from TensorFlow GraphDef to TFLite. Conversion can be customized by providing arguments that are forwarded to `build_toco_convert_protos` (see documentation for details). Args: input_data: Input data (i.e. often `sess.graph_def`), input_tensors: List of input tensors. Type and shape are computed using `foo.shape` and `foo.dtype`. output_tensors: List of output tensors (only .name is used from this). enable_mlir_converter: Enables MLIR-based conversion instead of TOCO conversion. *args: See `build_toco_convert_protos`, **kwargs: See `build_toco_convert_protos`. Returns: The converted data. For example if TFLite was the destination, then this will be a tflite flatbuffer in a bytes array. Raises: Defined in `build_toco_convert_protos`.
392390toco_converttensorflow/tensorflow/lite/python/convert.py580functionConvert a model using TOCO. Typically this function is used to convert from TensorFlow GraphDef to TFLite. Conversion can be customized by providing arguments that are forwarded to `build_toco_convert_protos` (see documentation for details). This function has been deprecated. Please use `lite.TFLiteConverter` instead. Args: input_data: Input data (i.e. often `sess.graph_def`), input_tensors: List of input tensors. Type and shape are computed using `foo.shape` and `foo.dtype`. output_tensors: List of output tensors (only .name is used from this). *args: See `build_toco_convert_protos`, **kwargs: See `build_toco_convert_protos`. Returns: The converted data. For example if TFLite was the destination, then this will be a tflite flatbuffer in a bytes array. Raises: Defined in `build_toco_convert_protos`.
393391get_meta_graph_deftensorflow/tensorflow/lite/python/convert_saved_model.py46functionValidate saved_model and extract MetaGraphDef. Args: saved_model_dir: saved_model path to convert. tag_set: Set of tag(s) of the MetaGraphDef to load. Returns: The meta_graph_def used for tflite conversion. Raises: ValueError: No valid MetaGraphDef for given tag_set.
394392get_signature_deftensorflow/tensorflow/lite/python/convert_saved_model.py63functionGet the signature def from meta_graph with given signature_key. Args: meta_graph: meta_graph_def. signature_key: signature_def in the meta_graph_def. Returns: The signature_def used for tflite conversion. Raises: ValueError: Given signature_key is not valid for this meta_graph.
395393get_inputs_outputstensorflow/tensorflow/lite/python/convert_saved_model.py88functionGet inputs and outputs from SignatureDef. Args: signature_def: SignatureDef in the meta_graph_def for conversion. Returns: The inputs and outputs in the graph for conversion.
396394freeze_saved_modeltensorflow/tensorflow/lite/python/convert_saved_model.py155functionConverts a SavedModel to a frozen graph. Args: saved_model_dir: SavedModel directory to convert. input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo": : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. tag_set: Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. signature_key: Key identifying SignatureDef containing inputs and outputs. Returns: frozen_graph_def: Frozen GraphDef. in_tensors: List of input tensors for the graph. out_tensors: List of output tensors for the graph. graph: `Graph` object. Raises: ValueError: SavedModel doesn't contain a MetaGraphDef identified by tag_set. signature_key is not in the MetaGraphDef. assets/ directory is in the MetaGraphDef. input_shapes does not match the length of input_arrays. input_arrays or output_arrays are not valid.
397395Delegatetensorflow/tensorflow/lite/python/interpreter.py42classPython wrapper class to manage TfLiteDelegate objects. The shared library is expected to have two functions: TfLiteDelegate* tflite_plugin_create_delegate( char**, char**, size_t, void (*report_error)(const char *)) void tflite_plugin_destroy_delegate(TfLiteDelegate*) The first one creates a delegate object. It may return NULL to indicate an error (with a suitable error message reported by calling report_error()). The second one destroys delegate object and must be called for every created delegate object. Passing NULL as argument value is allowed, i.e. tflite_plugin_destroy_delegate(tflite_plugin_create_delegate(...)) always works.
398396reporttensorflow/tensorflow/lite/python/interpreter.py101method
399397load_delegatetensorflow/tensorflow/lite/python/interpreter.py132functionReturns loaded Delegate object. Args: library: Name of shared library containing the [TfLiteDelegate](https://www.tensorflow.org/lite/performance/delegates). options: Dictionary of options that are required to load the delegate. All keys and values in the dictionary should be convertible to str. Consult the documentation of the specific delegate for required and legal options. (default None) Returns: Delegate object. Raises: ValueError: Delegate failed to load. RuntimeError: If delegate loading is used on unsupported platform.
400398Interpretertensorflow/tensorflow/lite/python/interpreter.py159classInterpreter interface for TensorFlow Lite Models. This makes the TensorFlow Lite interpreter accessible in Python. It is possible to use this interpreter in a multithreaded Python environment, but you must be sure to call functions of a particular instance from only one thread at a time. So if you want to have 4 threads running different inferences simultaneously, create an interpreter for each one as thread-local data. Similarly, if you are calling invoke() in one thread on a single interpreter but you want to use tensor() on another thread once it is done, you must use a synchronization primitive between the threads to ensure invoke has returned before calling tensor().
401399allocate_tensorstensorflow/tensorflow/lite/python/interpreter.py242method
402400get_tensor_detailstensorflow/tensorflow/lite/python/interpreter.py365methodGets tensor details for every tensor with valid tensor details. Tensors where required information about the tensor is not found are not added to the list. This includes temporary tensors without a name. Returns: A list of dictionaries containing tensor information.
403401get_input_detailstensorflow/tensorflow/lite/python/interpreter.py382methodGets model input details. Returns: A list of input details.
404402set_tensortensorflow/tensorflow/lite/python/interpreter.py392methodSets the value of the input tensor. Note this copies data in `value`. If you want to avoid copying, you can use the `tensor()` function to get a numpy buffer pointing to the input buffer in the tflite interpreter. Args: tensor_index: Tensor index of tensor to set. This value can be gotten from the 'index' field in get_input_details. value: Value of tensor to set. Raises: ValueError: If the interpreter could not set the tensor.
405403resize_tensor_inputtensorflow/tensorflow/lite/python/interpreter.py410methodResizes an input tensor. ``` interpreter = Interpreter(model_content=tflite_model) interpreter.resize_tensor_input(0, [1, 224, 224, 3], strict=True) interpreter.allocate_tensors() interpreter.invoke() ``` Args: input_index: Tensor index of input to set. This value can be gotten from the 'index' field in get_input_details. tensor_size: The tensor_shape to resize the input to. strict: Only unknown dimensions can be resized when `strict` is True. Unknown dimensions are indicated as `-1` in the `shape_signature` attribute of a given tensor. (default False) Raises: ValueError: If the interpreter could not resize the input tensor.
406404get_output_detailstensorflow/tensorflow/lite/python/interpreter.py437methodGets model output details. Returns: A list of output details.
407405get_tensortensorflow/tensorflow/lite/python/interpreter.py447methodGets the value of the input tensor (get a copy). If you wish to avoid the copy, use `tensor()`. This function cannot be used to read intermediate results. Args: tensor_index: Tensor index of tensor to get. This value can be gotten from the 'index' field in get_output_details. Returns: a numpy array.
408406tensortensorflow/tensorflow/lite/python/interpreter.py462methodReturns function that gives a numpy view of the current tensor buffer. This allows reading and writing to this tensors w/o copies. This more closely mirrors the C++ Interpreter class interface's tensor() member, hence the name. Be careful to not hold these output references through calls to `allocate_tensors()` and `invoke()`. This function cannot be used to read intermediate results. Usage: ``` interpreter.allocate_tensors() input = interpreter.tensor(interpreter.get_input_details()[0]["index"]) output = interpreter.tensor(interpreter.get_output_details()[0]["index"]) for i in range(10): input().fill(3.) interpreter.invoke() print("inference %s" % output()) ``` Notice how this function avoids making a numpy array directly. This is because it is important to not hold actual numpy views to the data longer than necessary. If you do, then the interpreter can no longer be invoked, because it is possible the interpreter would resize and invalidate the referenced tensors. The NumPy API doesn't allow any mutability of the the underlying buffers. WRONG: ``` input = interpreter.tensor(interpreter.get_input_details()[0]["index"])() output = interpreter.tensor(interpreter.get_output_details()[0]["index"])() interpreter.allocate_tensors() # This will throw RuntimeError for i in range(10): input.fill(3.) interpreter.invoke() # this will throw RuntimeError since input,output ``` Args: tensor_index: Tensor index of tensor to get. This value can be gotten from the 'index' field in get_output_details. Returns: A function that can return a new numpy array pointing to the internal TFLite tensor state at any point. It is safe to hold the function forever, but it is not safe to hold the numpy array forever.
409407invoketensorflow/tensorflow/lite/python/interpreter.py512methodInvoke the interpreter. Be sure to set the input sizes, allocate tensors and fill values before calling this. Also, note that this function releases the GIL so heavy computation can be done in the background while the Python interpreter continues. No other function on this object should be called while the invoke() call has not finished. Raises: ValueError: When the underlying interpreter fails raise ValueError.
410408reset_all_variablestensorflow/tensorflow/lite/python/interpreter.py527method
411409InterpreterWithCustomOpstensorflow/tensorflow/lite/python/interpreter.py552classInterpreter interface for TensorFlow Lite Models that accepts custom ops. The interface provided by this class is experimental and therefore not exposed as part of the public API. Wraps the tf.lite.Interpreter class and adds the ability to load custom ops by providing the names of functions that take a pointer to a BuiltinOpResolver and add a custom op.
412410Optimizetensorflow/tensorflow/lite/python/lite.py88classEnum defining the optimizations to apply when generating tflite graphs. Some optimizations may come at the cost of accuracy. DEFAULT Default optimization strategy. Converter will do its best to improve size and latency based on the information provided. Enhanced optimizations are gained by providing a representative_dataset. This is recommended, and is currently equivalent to the modes below. Currently, weights will be quantized and if representative_dataset is provided, activations for quantizable operations will also be quantized. OPTIMIZE_FOR_SIZE Deprecated. Does the same as DEFAULT. OPTIMIZE_FOR_LATENCY Deprecated. Does the same as DEFAULT.
413411RepresentativeDatasettensorflow/tensorflow/lite/python/lite.py131classRepresentative dataset to evaluate optimizations. A representative dataset that can be used to evaluate optimizations by the converter. E.g. converter can use these examples to estimate (min, max) ranges by calibrating the model on inputs. This can allow converter to quantize a converted floating point model.
414412TargetSpectensorflow/tensorflow/lite/python/lite.py153classSpecification of target device. Details about target device. Converter optimizes the generated model for specific device. Attributes: supported_ops: Experimental flag, subject to change. Set of OpsSet options supported by the device. (default set([OpsSet.TFLITE_BUILTINS])) supported_types: List of types for constant values on the target device. Supported values are types exported by lite.constants. Frequently, an optimization choice is driven by the most compact (i.e. smallest) type in this list (default [constants.FLOAT])
415413QuantizationModetensorflow/tensorflow/lite/python/lite.py177classQuantizationMode determines the quantized conversion from user options.
416414post_training_int8_no_floattensorflow/tensorflow/lite/python/lite.py189methodPost training int8 quantize, disallow float fallback.
417415post_training_int8_allow_floattensorflow/tensorflow/lite/python/lite.py195methodPost training int8 quantize, allow float fallback.
418416is_post_training_integer_quantizetensorflow/tensorflow/lite/python/lite.py202methodPost training integer quantization.
419417training_time_int8_allow_floattensorflow/tensorflow/lite/python/lite.py207methodTraining-time int8 quantize, allow float fallback.
420418post_training_int16x8_no_floattensorflow/tensorflow/lite/python/lite.py213methodPost training int16x8 quantize, disallow float fallback.
421419post_training_int16x8_allow_floattensorflow/tensorflow/lite/python/lite.py220methodPost training int16x8 quantize, allow float fallback.
422420post_training_dynamic_range_int8tensorflow/tensorflow/lite/python/lite.py224methodPost training int8 const, on-the-fly int8 quantize of dynamic tensors.
423421post_training_fp16tensorflow/tensorflow/lite/python/lite.py233methodPost training fp16 quantize.
424422fp32_executiontensorflow/tensorflow/lite/python/lite.py238methodIf none of the above are true.
425423activations_typetensorflow/tensorflow/lite/python/lite.py248method
426424converter_flagstensorflow/tensorflow/lite/python/lite.py252methodFlags to the converter.
427425quantizer_flagstensorflow/tensorflow/lite/python/lite.py292methodDefault flags to the TFMOT quantizer.
428426contains_training_quant_optensorflow/tensorflow/lite/python/lite.py371methodChecks if the graph contains any training-time quantization ops.
429427TFLiteConverterBasetensorflow/tensorflow/lite/python/lite.py384classConverter subclass to share functionality between V1 and V2 converters.
430428TFLiteConverterBaseV2tensorflow/tensorflow/lite/python/lite.py522classConverter subclass to share functionality between V2 converters. Attributes: allow_custom_ops: Boolean indicating whether to allow custom operations. When False, any unknown operation is an error. When True, custom ops are created for any op that is unknown. The developer needs to provide these to the TensorFlow Lite runtime with a custom resolver. (default False) optimizations: Experimental flag, subject to change. A list of optimizations to apply when converting the model. E.g. `[Optimize.DEFAULT]` representative_dataset: A representative dataset that can be used to generate input and output samples for the model. The converter can use the dataset to evaluate different optimizations. Note that this is an optional attribute but it is necessary if INT8 is the only support builtin ops in target ops. target_spec: Experimental flag, subject to change. Specification of target device. inference_input_type: Data type of the input layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8}) inference_output_type: Data type of the output layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8}) experimental_new_converter: Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)
431429converttensorflow/tensorflow/lite/python/lite.py574methodConverts a TensorFlow GraphDef based on instance variables. Args: graph_def: Frozen TensorFlow GraphDef. input_tensors: List of input tensors. Type and shape are computed using `foo.shape` and `foo.dtype`. output_tensors: List of output tensors (only .name is used from this). Returns: The converted data in serialized format. Raises: ValueError: No concrete functions is specified. Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.
432430TFLiteSavedModelConverterV2tensorflow/tensorflow/lite/python/lite.py652classConverts the given SavedModel into TensorFlow Lite model. Attributes: saved_model_dir: Directory of the SavedModel.
433431converttensorflow/tensorflow/lite/python/lite.py686methodConverts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Raises: ValueError: No concrete functions is specified. Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.
434432TFLiteKerasModelConverterV2tensorflow/tensorflow/lite/python/lite.py719classConverts the given Keras model into TensorFlow Lite model.
435433converttensorflow/tensorflow/lite/python/lite.py781methodConverts a keras model based on instance variables. Returns: The converted data in serialized format. Raises: ValueError: Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.
436434TFLiteFrozenGraphConverterV2tensorflow/tensorflow/lite/python/lite.py840classConverts the given frozen graph into TensorFlow Lite model.
437435converttensorflow/tensorflow/lite/python/lite.py859methodConverts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Raises: ValueError: No concrete functions is specified. Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.
438436TFLiteConverterV2tensorflow/tensorflow/lite/python/lite.py910classConverts a TensorFlow model into TensorFlow Lite model. Attributes: allow_custom_ops: Boolean indicating whether to allow custom operations. When False, any unknown operation is an error. When True, custom ops are created for any op that is unknown. The developer needs to provide these to the TensorFlow Lite runtime with a custom resolver. (default False) optimizations: Experimental flag, subject to change. A list of optimizations to apply when converting the model. E.g. `[Optimize.DEFAULT]` representative_dataset: A representative dataset that can be used to generate input and output samples for the model. The converter can use the dataset to evaluate different optimizations. Note that this is an optional attribute but it is necessary if INT8 is the only support builtin ops in target ops. target_spec: Experimental flag, subject to change. Specification of target device. inference_input_type: Data type of the input layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8}) inference_output_type: Data type of the output layer. Note that integer types (tf.int8 and tf.uint8) are currently only supported for post training integer quantization. (default tf.float32, must be in {tf.float32, tf.int8, tf.uint8}) experimental_new_converter: Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True) Example usage: ```python # Converting a SavedModel to a TensorFlow Lite model. converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) tflite_model = converter.convert() # Converting a tf.Keras model to a TensorFlow Lite model. converter = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert() # Converting ConcreteFunctions to a TensorFlow Lite model. converter = tf.lite.TFLiteConverter.from_concrete_functions([func]) tflite_model = converter.convert() ```
439437from_concrete_functionstensorflow/tensorflow/lite/python/lite.py971methodCreates a TFLiteConverter object from ConcreteFunctions. Args: funcs: List of TensorFlow ConcreteFunctions. The list should not contain duplicate elements. Currently converter can only convert a single ConcreteFunction. Converting multiple functions is under development. Returns: TFLiteConverter object. Raises: Invalid input type.
440438from_saved_modeltensorflow/tensorflow/lite/python/lite.py995methodCreates a TFLiteConverter object from a SavedModel directory. Args: saved_model_dir: SavedModel directory to convert. signature_keys: List of keys identifying SignatureDef containing inputs and outputs. Elements should not be duplicated. By default the `signatures` attribute of the MetaGraphdef is used. (default saved_model.signatures) tags: Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. (default set(SERVING)) Returns: TFLiteConverter object. Raises: Invalid signature keys.
441439from_keras_modeltensorflow/tensorflow/lite/python/lite.py1057methodCreates a TFLiteConverter object from a Keras model. Args: model: tf.Keras.Model Returns: TFLiteConverter object.
442440converttensorflow/tensorflow/lite/python/lite.py1069methodConverts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Raises: ValueError: No concrete functions is specified. Multiple concrete functions are specified. Input shape is not specified. Invalid quantization parameters.
443441TFLiteConverterBaseV1tensorflow/tensorflow/lite/python/lite.py1085classConverter subclass to share functionality between V1 converters. Attributes: inference_type: Target data type of real-number arrays in the output file. Must be `{tf.float32, tf.uint8}`. If `optimzations` are provided, this parameter is ignored. (default tf.float32) inference_input_type: Target data type of real-number input arrays. Allows for a different type for input arrays. If an integer type is provided and `optimizations` are not used, `quantized_inputs_stats` must be provided. If `inference_type` is tf.uint8, signaling conversion to a fully quantized model from a quantization-aware trained input model, then `inference_input_type` defaults to tf.uint8. In all other cases, `inference_input_type` defaults to tf.float32. Must be `{tf.float32, tf.uint8, tf.int8}` inference_output_type: Target data type of real-number output arrays. Allows for a different type for output arrays. If `inference_type` is tf.uint8, signaling conversion to a fully quantized model from a quantization-aware trained output model, then `inference_output_type` defaults to tf.uint8. In all other cases, `inference_output_type` must be tf.float32, an error will be thrown otherwise. Must be `{tf.float32, tf.uint8, tf.int8}` output_format: Output file format. Currently must be `{TFLITE, GRAPHVIZ_DOT}`. (default TFLITE) quantized_input_stats: Dict of strings representing input tensor names mapped to tuple of floats representing the mean and standard deviation of the training data (e.g., {"foo" : (0., 1.)}). Only need if `inference_input_type` is `QUANTIZED_UINT8`. real_input_value = (quantized_input_value - mean_value) / std_dev_value. (default {}) default_ranges_stats: Tuple of integers representing (min, max) range values for all arrays without a specified range. Intended for experimenting with quantization via "dummy quantization". (default None) drop_control_dependency: Boolean indicating whether to drop control dependencies silently. This is due to TFLite not supporting control dependencies. (default True) reorder_across_fake_quant: Boolean indicating whether to reorder FakeQuant nodes in unexpected locations. Used when the location of the FakeQuant nodes is preventing graph transformations necessary to convert the graph. Results in a graph that differs from the quantized training graph, potentially causing differing arithmetic behavior. (default False) change_concat_input_ranges: Boolean to change behavior of min/max ranges for inputs and outputs of the concat operator for quantized models. Changes the ranges of concat operator overlap when true. (default False) allow_custom_ops: Boolean indicating whether to allow custom operations. When false any unknown operation is an error. When true, custom ops are created for any op that is unknown. The developer will need to provide these to the TensorFlow Lite runtime with a custom resolver. (default False) post_training_quantize: Deprecated. Please specify `[Optimize.DEFAULT]` for `optimizations` instead. Boolean indicating whether to quantize the weights of the converted float model. Model size will be reduced and there will be latency improvements (at the cost of accuracy). (default False) dump_graphviz_dir: Full filepath of folder to dump the graphs at various stages of processing GraphViz .dot files. Preferred over --output_format=GRAPHVIZ_DOT in order to keep the requirements of the output file. (default None) dump_graphviz_video: Boolean indicating whether to dump the graph after every graph transformation. (default False) conversion_summary_dir: A string indicating the path to the generated conversion logs. target_ops: Deprecated. Please specify `target_spec.supported_ops` instead. Set of OpsSet options indicating which converter to use. (default set([OpsSet.TFLITE_BUILTINS])) target_spec: Experimental flag, subject to change. Specification of target device. optimizations: Experimental flag, subject to change. A list of optimizations to apply when converting the model. E.g. `[Optimize.DEFAULT]` representative_dataset: A representative dataset that can be used to generate input and output samples for the model. The converter can use the dataset to evaluate different optimizations. experimental_new_converter: Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)
444442converttensorflow/tensorflow/lite/python/lite.py1226methodConverts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Either a TFLite Flatbuffer or a Graphviz graph depending on value in `output_format`. Raises: ValueError: Input shape is not specified. None value for dimension in input_tensor.
445443get_input_arraystensorflow/tensorflow/lite/python/lite.py1352methodReturns a list of the names of the input tensors. Returns: List of strings.
446444TFLiteSavedModelConvertertensorflow/tensorflow/lite/python/lite.py1410classConverts the given SavedModel into TensorFlow Lite model. Attributes: saved_model_dir: Directory of the SavedModel.
447445TFLiteKerasModelConvertertensorflow/tensorflow/lite/python/lite.py1458classConverts the given SavedModel into TensorFlow Lite model.
448446converttensorflow/tensorflow/lite/python/lite.py1567methodConverts a Keras model based on instance variables. Returns: The converted data in serialized format. Either a TFLite Flatbuffer or a Graphviz graph depending on value in `output_format`. Raises: ValueError: Input shape is not specified. None value for dimension in input_tensor.
449447TFLiteFrozenGraphConvertertensorflow/tensorflow/lite/python/lite.py1586classConverts the given frozen graph def into TensorFlow Lite model.
450448TFLiteConvertertensorflow/tensorflow/lite/python/lite.py1634classConvert a TensorFlow model into `output_format`. This is used to convert from a TensorFlow GraphDef, SavedModel or tf.keras model into either a TFLite FlatBuffer or graph visualization. Attributes: inference_type: Target data type of real-number arrays in the output file. Must be `{tf.float32, tf.uint8}`. If `optimzations` are provided, this parameter is ignored. (default tf.float32) inference_input_type: Target data type of real-number input arrays. Allows for a different type for input arrays. If an integer type is provided and `optimizations` are not used, `quantized_inputs_stats` must be provided. If `inference_type` is tf.uint8, signaling conversion to a fully quantized model from a quantization-aware trained input model, then `inference_input_type` defaults to tf.uint8. In all other cases, `inference_input_type` defaults to tf.float32. Must be `{tf.float32, tf.uint8, tf.int8}` inference_output_type: Target data type of real-number output arrays. Allows for a different type for output arrays. If `inference_type` is tf.uint8, signaling conversion to a fully quantized model from a quantization-aware trained output model, then `inference_output_type` defaults to tf.uint8. In all other cases, `inference_output_type` must be tf.float32, an error will be thrown otherwise. Must be `{tf.float32, tf.uint8, tf.int8}` output_format: Output file format. Currently must be `{TFLITE, GRAPHVIZ_DOT}`. (default TFLITE) quantized_input_stats: Dict of strings representing input tensor names mapped to tuple of floats representing the mean and standard deviation of the training data (e.g., {"foo" : (0., 1.)}). Only need if `inference_input_type` is `QUANTIZED_UINT8`. real_input_value = (quantized_input_value - mean_value) / std_dev_value. (default {}) default_ranges_stats: Tuple of integers representing (min, max) range values for all arrays without a specified range. Intended for experimenting with quantization via "dummy quantization". (default None) drop_control_dependency: Boolean indicating whether to drop control dependencies silently. This is due to TFLite not supporting control dependencies. (default True) reorder_across_fake_quant: Boolean indicating whether to reorder FakeQuant nodes in unexpected locations. Used when the location of the FakeQuant nodes is preventing graph transformations necessary to convert the graph. Results in a graph that differs from the quantized training graph, potentially causing differing arithmetic behavior. (default False) change_concat_input_ranges: Boolean to change behavior of min/max ranges for inputs and outputs of the concat operator for quantized models. Changes the ranges of concat operator overlap when true. (default False) allow_custom_ops: Boolean indicating whether to allow custom operations. When false any unknown operation is an error. When true, custom ops are created for any op that is unknown. The developer will need to provide these to the TensorFlow Lite runtime with a custom resolver. (default False) post_training_quantize: Deprecated. Please specify `[Optimize.DEFAULT]` for `optimizations` instead. Boolean indicating whether to quantize the weights of the converted float model. Model size will be reduced and there will be latency improvements (at the cost of accuracy). (default False) dump_graphviz_dir: Full filepath of folder to dump the graphs at various stages of processing GraphViz .dot files. Preferred over --output_format=GRAPHVIZ_DOT in order to keep the requirements of the output file. (default None) dump_graphviz_video: Boolean indicating whether to dump the graph after every graph transformation. (default False) conversion_summary_dir: A string indicating the path to the generated conversion logs. target_ops: Deprecated. Please specify `target_spec.supported_ops` instead. Set of OpsSet options indicating which converter to use. (default set([OpsSet.TFLITE_BUILTINS])) target_spec: Experimental flag, subject to change. Specification of target device. optimizations: Experimental flag, subject to change. A list of optimizations to apply when converting the model. E.g. `[Optimize.DEFAULT]` representative_dataset: A representative dataset that can be used to generate input and output samples for the model. The converter can use the dataset to evaluate different optimizations. experimental_new_converter: Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True) Example usage: ```python # Converting a GraphDef from session. converter = tf.compat.v1.TFLiteConverter.from_session( sess, in_tensors, out_tensors) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model) # Converting a GraphDef from file. converter = tf.compat.v1.TFLiteConverter.from_frozen_graph( graph_def_file, input_arrays, output_arrays) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model) # Converting a SavedModel. converter = tf.compat.v1.TFLiteConverter.from_saved_model(saved_model_dir) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model) # Converting a tf.keras model. converter = tf.compat.v1.TFLiteConverter.from_keras_model_file(keras_model) tflite_model = converter.convert() open("converted_model.tflite", "wb").write(tflite_model) ```
451449from_sessiontensorflow/tensorflow/lite/python/lite.py1776methodCreates a TFLiteConverter class from a TensorFlow Session. Args: sess: TensorFlow Session. input_tensors: List of input tensors. Type and shape are computed using `foo.shape` and `foo.dtype`. output_tensors: List of output tensors (only .name is used from this). Returns: TFLiteConverter class.
452450from_frozen_graphtensorflow/tensorflow/lite/python/lite.py1796methodCreates a TFLiteConverter class from a file containing a frozen GraphDef. Args: graph_def_file: Full filepath of file containing frozen GraphDef. input_arrays: List of input tensors to freeze graph with. output_arrays: List of output tensors to freeze graph with. input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) Returns: TFLiteConverter class. Raises: IOError: File not found. Unable to parse input file. ValueError: The graph is not frozen. input_arrays or output_arrays contains an invalid tensor name. input_shapes is not correctly defined when required
453451from_saved_modeltensorflow/tensorflow/lite/python/lite.py1889methodCreates a TFLiteConverter class from a SavedModel. Args: saved_model_dir: SavedModel directory to convert. input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None) input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None) tag_set: Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. (default set("serve")) signature_key: Key identifying SignatureDef containing inputs and outputs. (default DEFAULT_SERVING_SIGNATURE_DEF_KEY) Returns: TFLiteConverter class.
454452from_keras_model_filetensorflow/tensorflow/lite/python/lite.py1936methodCreates a TFLiteConverter class from a tf.keras model file. Args: model_file: Full filepath of HDF5 file containing the tf.keras model. input_arrays: List of input tensors to freeze graph with. Uses input arrays from SignatureDef when none are provided. (default None) input_shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo" : [1, 16, 16, 3]}). Automatically determined when input shapes is None (e.g., {"foo" : None}). (default None) output_arrays: List of output tensors to freeze graph with. Uses output arrays from SignatureDef when none are provided. (default None) custom_objects: Dict mapping names (strings) to custom classes or functions to be considered during model deserialization. (default None) Returns: TFLiteConverter class.
455453converttensorflow/tensorflow/lite/python/lite.py1964methodConverts a TensorFlow GraphDef based on instance variables. Returns: The converted data in serialized format. Either a TFLite Flatbuffer or a Graphviz graph depending on value in `output_format`. Raises: ValueError: Input shape is not specified. None value for dimension in input_tensor.
456454TocoConvertertensorflow/tensorflow/lite/python/lite.py1979classConvert a TensorFlow model into `output_format` using TOCO. This class has been deprecated. Please use `lite.TFLiteConverter` instead.
457455from_sessiontensorflow/tensorflow/lite/python/lite.py1988methodCreates a TocoConverter class from a TensorFlow Session.
458456from_frozen_graphtensorflow/tensorflow/lite/python/lite.py1995methodCreates a TocoConverter class from a file containing a frozen graph.
459457from_saved_modeltensorflow/tensorflow/lite/python/lite.py2007methodCreates a TocoConverter class from a SavedModel.
460458from_keras_model_filetensorflow/tensorflow/lite/python/lite.py2022methodCreates a TocoConverter class from a tf.keras model file.
461459FromConstructortensorflow/tensorflow/lite/python/lite_test.py76class
462460FromFrozenGraphFiletensorflow/tensorflow/lite/python/lite_test.py1464class
463461FromFrozenGraphObjectDetectiontensorflow/tensorflow/lite/python/lite_test.py1650class
464462MyAddLayertensorflow/tensorflow/lite/python/lite_test.py1897class
465463calltensorflow/tensorflow/lite/python/lite_test.py1903method
466464get_configtensorflow/tensorflow/lite/python/lite_test.py1906method
467465FromKerasFiletensorflow/tensorflow/lite/python/lite_test.py1912class
468466setUptensorflow/tensorflow/lite/python/lite_test.py1914method
469467tearDowntensorflow/tensorflow/lite/python/lite_test.py1921method
470468UnknownShapestensorflow/tensorflow/lite/python/lite_v2_test.py1047class
471469modeltensorflow/tensorflow/lite/python/lite_v2_test.py1056method
472470modeltensorflow/tensorflow/lite/python/lite_v2_test.py1077method
473471calibration_gentensorflow/tensorflow/lite/python/lite_v2_test.py1093method
474472modeltensorflow/tensorflow/lite/python/lite_v2_test.py1157method
475473modeltensorflow/tensorflow/lite/python/lite_v2_test.py1178method
476474OpHinttensorflow/tensorflow/lite/python/op_hint.py97classA class that helps build tflite function invocations. It allows you to take a bunch of TensorFlow ops and annotate the construction such that toco knows how to convert it to tflite. This embeds a pseudo function in a TensorFlow graph. This allows embedding high-level API usage information in a lower level TensorFlow implementation so that an alternative implementation can be substituted later. Essentially, any "input" into this pseudo op is fed into an identity, and attributes are added to that input before being used by the constituent ops that make up the pseudo op. A similar process is done to any output that is to be exported from the current op.
477475add_inputtensorflow/tensorflow/lite/python/op_hint.py388methodAdd a wrapped input argument to the hint. Args: *args: The input tensor. **kwargs: "name" label "tag" a tag to group multiple arguments that will be aggregated. I.e. a string like 'cool_input'. Basically multiple inputs can be added to the same hint for parallel operations that will eventually be combined. An example would be static_rnn which creates multiple copies of state or inputs. "aggregate" aggregation strategy that is valid only for tag non None. Acceptable values are OpHint.AGGREGATE_FIRST, OpHint.AGGREGATE_LAST, and OpHint.AGGREGATE_STACK. "index_override" The global index to use. This corresponds to the argument order in the final stub that will be generated. Returns: The wrapped input tensor.
478476add_outputtensorflow/tensorflow/lite/python/op_hint.py410methodAdd a wrapped output argument to the hint. Args: *args: The output tensor. **kwargs: "name" label "tag" a tag to group multiple arguments that will be aggregated. I.e. a string like 'cool_input'. Basically multiple inputs can be added to the same hint for parallel operations that will eventually be combined. An example would be static_rnn which creates multiple copies of state or inputs. "aggregate" aggregation strategy that is valid only for tag non None. Acceptable values are OpHint.AGGREGATE_FIRST, OpHint.AGGREGATE_LAST, and OpHint.AGGREGATE_STACK. "index_override" The global index to use. This corresponds to the argument order in the final stub that will be generated. Returns: The wrapped output tensor.
479477add_inputstensorflow/tensorflow/lite/python/op_hint.py432methodAdd a sequence of inputs to the function invocation. Args: *args: List of inputs to be converted (should be Tf.Tensor). **kwargs: This allows 'names' which should be a list of names. Returns: Wrapped inputs (identity standins that have additional metadata). These are also are also tf.Tensor's.
480478add_outputstensorflow/tensorflow/lite/python/op_hint.py451methodAdd a sequence of outputs to the function invocation. Args: *args: List of outputs to be converted (should be tf.Tensor). **kwargs: See Returns: Wrapped outputs (identity standins that have additional metadata). These are also tf.Tensor's.
481479addtensorflow/tensorflow/lite/python/op_hint.py229methodReturn a wrapped tensor of an input tensor as an argument. Args: arg: A TensorFlow tensor that should be considered an argument. tag: String tag to identify arguments that should be packed. name: Name of argument. This is included in the Identity hint op names. aggregate: Strategy to aggregate. Acceptable values are OpHint.AGGREGATE_FIRST, OpHint.AGGREGATE_LAST, and OpHint.AGGREGATE_STACK. Note, aggregate is only valid if tag is specified. index_override: Specify what input/output index should this be in the final stub. i.e. add(arg0, index=1); add(arg1, index=0) will make the final stub be as stub_func(inputs[arg1, arg0], outputs=[]) rather than the default call order based ordering. Returns: A tensor representing the wrapped argument. Raises: ValueError: When indices are not consistent.
482480assert_dictlist_has_keystensorflow/tensorflow/lite/python/op_hint.py365method
483481find_all_hinted_output_nodestensorflow/tensorflow/lite/python/op_hint.py1257functionFind all Ophints output nodes in the graph. This is used to get all the output nodes those are ophinted, it is important for operation like convert_variables_to_constants keep all ophints structure. Note: only one of session or graph_def should be used, not both. Why this can be useful? Some TensorFlow ops (e.g. bidirectional rnn), can generate multiple outputs for unfused subgraph. If not all output nodes are consumed, graph optimization can potentially drop the unused nodes and cause ophints in an invalid states (due to missing ophinted output nodes). So it's important for us to find all those hinted output nodes and make sure they're not discarded away. Args: session: A TensorFlow session that contains the graph to convert. graph_def: A graph def that we should convert. Returns: A list of OpHints output nodes. Raises: ValueError: If both session and graph_def are provided.
484482is_ophint_convertedtensorflow/tensorflow/lite/python/op_hint.py1292function
485483convert_op_hints_to_stubstensorflow/tensorflow/lite/python/op_hint.py1305functionConverts a graphdef with LiteOp hints into stub operations. This is used to prepare for toco conversion of complex intrinsic usages. Note: only one of session or graph_def should be used, not both. Args: session: A TensorFlow session that contains the graph to convert. graph_def: A graph def that we should convert. write_callback: A function pointer that can be used to write intermediate steps of graph transformation (optional). Returns: A new graphdef with all ops contained in OpHints being replaced by a single op call with the right parameters. Raises: ValueError: If both session and graph_def are provided.
486484convert_dtype_to_tflite_typetensorflow/tensorflow/lite/python/util.py59functionConverts tf.dtype to TFLite proto type. Args: tf_dtype: tf.dtype Raises: ValueError: Unsupported tf.dtype. Returns: types_flag_pb2.
487485get_tensor_nametensorflow/tensorflow/lite/python/util.py77functionReturns name of the input tensor. Args: tensor: tf.Tensor Returns: str
488486get_tensors_from_tensor_namestensorflow/tensorflow/lite/python/util.py98functionGets the Tensors associated with the `tensor_names` in the provided graph. Args: graph: TensorFlow Graph. tensor_names: List of strings that represent names of tensors in the graph. Returns: A list of Tensor objects in the same order the names are provided. Raises: ValueError: tensor_names contains an invalid tensor name.
489487set_tensor_shapestensorflow/tensorflow/lite/python/util.py141functionSets Tensor shape for each tensor if the shape is defined. Args: tensors: TensorFlow ops.Tensor. shapes: Dict of strings representing input tensor names to list of integers representing input shapes (e.g., {"foo": : [1, 16, 16, 3]}). Raises: ValueError: `shapes` contains an invalid tensor. `shapes` contains an invalid shape for a valid tensor.
490488get_grappler_configtensorflow/tensorflow/lite/python/util.py172functionCreates a tf.compat.v1.ConfigProto for configuring Grappler. Args: optimizers_list: List of strings that represents the list of optimizers. Returns: tf.ConfigProto.
491489run_graph_optimizationstensorflow/tensorflow/lite/python/util.py188functionApply standard TensorFlow optimizations to the graph_def. Args: graph_def: Frozen GraphDef to be optimized. input_arrays: List of arrays that are considered inputs of the graph. output_arrays: List of arrays that are considered outputs of the graph. config: tf.ConfigProto. graph: TensorFlow Graph. Required when Eager mode is enabled. (default None) Returns: A new, optimized GraphDef.
492490freeze_graphtensorflow/tensorflow/lite/python/util.py241functionReturns a frozen GraphDef. Runs a Grappler pass and freezes a graph with Variables in it. Otherwise the existing GraphDef is returned. The Grappler pass is only run on models that are frozen in order to inline the functions in the graph. If OpHints is present, it will try to convert the OpHint graph. Args: sess: TensorFlow Session. input_tensors: List of input tensors. output_tensors: List of output tensors (only .name is used from this). Returns: Frozen GraphDef.
493491is_frozen_graphtensorflow/tensorflow/lite/python/util.py281functionDetermines if the graph is frozen. Determines if a graph has previously been frozen by checking for any operations of type Variable*. If variables are found, the graph is not frozen. Args: sess: TensorFlow Session. Returns: Bool.
494492build_debug_info_functensorflow/tensorflow/lite/python/util.py300functionReturns a method to retrieve the `GraphDebugInfo` from the original graph. Args: original_graph: The original `Graph` containing all the op stack traces. Returns: A function which retrieves the stack traces from the original graph and converts them to a `GraphDebugInfo` for a given set of nodes.
495493convert_debug_info_functensorflow/tensorflow/lite/python/util.py339functionReturns a method to retrieve the `GraphDebugInfo` from the original graph. Args: saved_debug_info: The `GraphDebugInfo` containing all the debug info. Returns: A function which retrieves the stack traces from the original graph and converts them to a `GraphDebugInfo` for a given set of nodes.
496494get_debug_infotensorflow/tensorflow/lite/python/util.py368functionReturns the debug info for the original nodes in the `converted_graph`. Args: nodes_to_debug_info_func: The method to collect the op debug info for the nodes. converted_graph: A `GraphDef` after optimization and transformation. Returns: `GraphDebugInfo` for all the original nodes in `converted_graph`.
497495convert_bytes_to_c_sourcetensorflow/tensorflow/lite/python/util.py399functionReturns strings representing a C constant array containing `data`. Args: data: Byte array that will be converted into a C constant. array_name: String to use as the variable name for the constant array. max_line_width: The longest line length, for formatting purposes. include_guard: Name to use for the include guard macro definition. include_path: Optional path to include in the source file. use_tensorflow_license: Whether to include the standard TensorFlow Apache2 license in the generated files. Returns: Text that can be compiled as a C source file to link in the data as a literal array of values. Text that can be used as a C header file to reference the literal array.
498496wrapped_toco_converttensorflow/tensorflow/lite/python/wrap_toco.py29functionWraps TocoConvert with lazy loader.
499497wrapped_get_potentially_supported_opstensorflow/tensorflow/lite/python/wrap_toco.py41functionWraps TocoGetPotentiallySupportedOps with lazy loader.
500498wrapped_experimental_mlir_quantizetensorflow/tensorflow/lite/python/wrap_toco.py46functionWraps experimental mlir quantize model.
501499wrapped_experimental_mlir_sparsifytensorflow/tensorflow/lite/python/wrap_toco.py55functionWraps experimental mlir sparsify model.
502500Calibratortensorflow/tensorflow/lite/python/optimize/calibrator.py33classCalibrates a floating point model and then quantizes it. This is an internal class, not a public interface.
503501calibrate_and_quantizetensorflow/tensorflow/lite/python/optimize/calibrator.py58methodCalibrates the model with specified generator and then quantizes it. The input shapes of the calibrator are resized with the calibration data if `resize_input` is set. Returns: A quantized model. Args: dataset_gen: A generator that generates calibration samples. input_type: A tf.dtype representing the desired real-value input type. output_type: A tf.dtype representing the desired real-value output type. allow_float: A boolean. False if the resulting model cannot perform float computation, useful when targeting an integer-only backend. If False, an error will be thrown if an operation cannot be quantized, otherwise the model will fallback to float ops. activations_type: A tf.dtype representing the desired type for activations. resize_input: A boolean. True if the shape of the sample data is different from the input.
504502calibrate_and_quantize_singletensorflow/tensorflow/lite/python/optimize/calibrator.py100methodCalibrates the model with specified generator and then quantizes it. Only the single op with output op_output_name will be quantized. The input shapes of the calibrator are resized with the calibration data. Returns: A quantized model. Args: dataset_gen: A generator that generates calibration samples. input_type: A tf.dtype representing the desired real-value input type. output_type: A tf.dtype representing the desired real-value output type. allow_float: A boolean. False if the resulting model cannot perform float computation, useful when targeting an integer-only backend. If False, an error will be thrown if an operation cannot be quantized, otherwise the model will fallback to float ops. op_output_name: A string, only this op will be quantized. resize_input: A boolean. True if the shape of the sample data is different from the input.
505503calibratetensorflow/tensorflow/lite/python/optimize/calibrator.py140methodCalibrates the model with specified generator. Returns: A model with min and max calibration stats. Args: dataset_gen: A generator that generates calibration samples.
506504TemporaryDirectoryResourcetensorflow/tensorflow/lite/schema/upgrade_schema.py57function
507505Convertertensorflow/tensorflow/lite/schema/upgrade_schema.py65classConverts TensorFlow flatbuffer models from old to new version of schema. This can convert between any version to the latest version. It uses an incremental upgrade strategy to go from version to version. Usage: converter = Converter() converter.Convert("a.tflite", "a.json") converter.Convert("b.json", "b.tflite")
508506Converttensorflow/tensorflow/lite/schema/upgrade_schema.py309methodPerform schema conversion from input_file to output_file. Args: input_file: Filename of TensorFlow Lite data to convert from. Must be `.json` or `.bin` extension files for JSON or Binary forms of the TensorFlow FlatBuffer schema. output_file: Filename to write to. Extension also must be `.json` or `.bin`. Raises: RuntimeError: Generated when none of the upgrader supported schemas matche the `input_file` data.
509507FindSchematensorflow/tensorflow/lite/schema/upgrade_schema.py88method
510508RemapOperatortensorflow/tensorflow/lite/schema/upgrade_schema.py209methodGo from old schema op name to new schema op name. Args: opcode_name: String representing the ops (see :schema.fbs). Returns: Converted opcode_name from V1 to V2.
511509RemapOperatorTypetensorflow/tensorflow/lite/schema/upgrade_schema.py232methodRemap operator structs from old names to new names. Args: operator_type: String representing the builtin operator data type string. (see :schema.fbs). Raises: ValueError: When the model has consistency problems. Returns: Upgraded builtin operator data type as a string.
512510JsonDumpAndFlushtensorflow/tensorflow/lite/schema/upgrade_schema_test.py242functionWrite the dictionary `data` to a JSON file `fp` (and flush). Args: data: in a dictionary that is JSON serializable. fp: File-like object
513511MultiGenStatetensorflow/tensorflow/lite/testing/generate_examples_lib.py176classState of multiple set generation process. This state class stores the information needed when generating the examples for multiple test set. The stored informations are open archive object to be shared, information on test target for current iteration of generation, accumulated generation results.
514512Optionstensorflow/tensorflow/lite/testing/generate_examples_lib.py203classAll options for example generation.
515513generate_examplestensorflow/tensorflow/lite/testing/generate_examples_lib.py256functionGenerate examples for a test set. Args: options: Options containing information to generate examples. Raises: RuntimeError: if the test function cannot be found.
516514generate_multi_set_examplestensorflow/tensorflow/lite/testing/generate_examples_lib.py294functionGenerate examples for test sets. Args: options: Options containing information to generate examples. test_sets: List of the name of test sets to generate examples.
517515make_report_tabletensorflow/tensorflow/lite/testing/generate_examples_report.py32functionMake an HTML report of the success/failure reports. Args: fp: File-like object in which to put the html. title: "Title of the zip file this pertains to." reports: a list of conversion attempts. (report_args, report_vals) i.e. ({"shape": [1,2,3], "type": "tf.float32"}, {"tf": "SUCCESS", "toco": "FAILURE", "toco_log": "Unsupported type.", "tf_log": ""})
518516toco_optionstensorflow/tensorflow/lite/testing/toco_convert.py31functionCreate TOCO options to process a model. Args: data_types: input and inference types used by TOCO. input_arrays: names of the input tensors output_arrays: name of the output tensors shapes: shapes of the input tensors extra_toco_options: additional toco options Returns: the options in a string.
519517toco_converttensorflow/tensorflow/lite/testing/toco_convert.py78functionConvert a model's graph def into a tflite model. NOTE: this currently shells out to the toco binary, but we would like convert to Python API tooling in the future. Args: options: An Options instance. graph_def: A GraphDef object. input_tensors: List of input tensor tuples `(name, shape, type)`. output_tensors: List of output tensors (names). **kwargs: Extra options to be passed. Returns: output tflite model, log_txt from conversion or None, log_txt if it did not convert properly.
520518ExtraTocoOptionstensorflow/tensorflow/lite/testing/zip_test_utils.py88classAdditional toco options besides input, output, shape.
521519create_tensor_datatensorflow/tensorflow/lite/testing/zip_test_utils.py106functionBuild tensor data spreading the range [min_value, max_value).
522520create_scalar_datatensorflow/tensorflow/lite/testing/zip_test_utils.py126functionBuild scalar tensor data range from min_value to max_value exclusively.
523521freeze_graphtensorflow/tensorflow/lite/testing/zip_test_utils.py144functionFreeze the current graph. Args: session: Tensorflow sessions containing the graph outputs: List of output tensors Returns: The frozen graph_def.
524522format_resulttensorflow/tensorflow/lite/testing/zip_test_utils.py158functionConvert a tensor to a format that can be used in test specs.
525523write_examplestensorflow/tensorflow/lite/testing/zip_test_utils.py168functionGiven a list `examples`, write a text format representation. The file format is csv like with a simple repeated pattern. We would ike to use proto here, but we can't yet due to interfacing with the Android team using this format. Args: fp: File-like object to write to. examples: Example dictionary consisting of keys "inputs" and "outputs"
526524get_input_shapes_maptensorflow/tensorflow/lite/testing/zip_test_utils.py225functionGets a map of input names to shapes. Args: input_tensors: List of input tensor tuples `(name, shape, type)`. Returns: {string : list of integers}.
527525get_filepathtensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py47functionReturns the full path of the filename. Args: filename: Subdirectory and name of the model file. base_dir: Base directory containing model file. Returns: str.
528526get_imagetensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py63functionReturns an image loaded into an np.ndarray with dims [1, size, size, 3]. Args: size: Size of image. Returns: np.ndarray.
529527evaluate_frozen_graphtensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py222functionReturns a function that evaluates the frozen graph on input data. Args: filename: Full filepath of file containing frozen GraphDef. input_arrays: List of input tensors to freeze graph with. output_arrays: List of output tensors to freeze graph with. Returns: Lambda function ([np.ndarray data] : [np.ndarray result]).
530528evaluate_saved_modeltensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py260functionReturns a function that evaluates the SavedModel on input data. Args: directory: SavedModel directory to convert. tag_set: Set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags in the tag set must be present. signature_key: Key identifying SignatureDef containing inputs and outputs. Returns: Lambda function ([np.ndarray data] : [np.ndarray result]).
531529evaluate_keras_modeltensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py286functionReturns a function that evaluates the tf.keras model on input data. Args: filename: Full filepath of HDF5 file containing the tf.keras model. Returns: Lambda function ([np.ndarray data] : [np.ndarray result]).
532530compare_modelstensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py299functionCompares TensorFlow and TFLite models. Unless the input data is provided, the models are compared with random data. Args: tflite_model: Serialized TensorFlow Lite model. tf_eval_func: Lambda function that takes in input data and outputs the results of the TensorFlow model ([np.ndarray data] : [np.ndarray result]). input_shapes_resize: A map where the key is the input tensor name and the value is the shape of the input tensor. This resize happens after model conversion, prior to calling allocate tensors. (default None) input_data: np.ndarray to pass into models during inference. (default None) input_data_range: A map where the key is the input tensor name and the value is a tuple (min_val, max_val) which specifies the value range of the corresponding input tensor. For example, '{'input1': (1, 5)}' means to generate a random value for tensor `input1` within range [1.0, 5.0) (half-inclusive). (default None) tolerance: Decimal place to check accuracy to. (default 5).
533531compare_models_v2tensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib.py336functionCompares TensorFlow and TFLite models for TensorFlow 2.0. Unless the input data is provided, the models are compared with random data. Currently only 1 input and 1 output are supported by this function. Args: tflite_model: Serialized TensorFlow Lite model. tf_eval_func: Function to evaluate TensorFlow model. Either a lambda function that takes in input data and outputs the results or a TensorFlow ConcreteFunction. input_data: np.ndarray to pass into models during inference. (default None). input_data_range: A map where the key is the input tensor name and the value is a tuple (min_val, max_val) which specifies the value range of the corresponding input tensor. For example, '{'input1': (1, 5)}' means to generate a random value for tensor `input1` within range [1.0, 5.0) (half-inclusive). (default None) tolerance: Decimal place to check accuracy to. (default 5)
534532EvaluateFrozenGraphtensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib_test.py42class
535533plus_placeholdertensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib_test.py92method
536534EvaluateSavedModeltensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib_test.py142class
537535EvaluateKerasModeltensorflow/tensorflow/lite/testing/model_coverage/model_coverage_lib_test.py160class
538536relu1tensorflow/tensorflow/lite/testing/op_tests/conv_activation.py143function
539537make_l2_pooltensorflow/tensorflow/lite/testing/op_tests/pool.py119functionGiven an input perform a sequence of TensorFlow ops to produce l2pool.
540538html_escapetensorflow/tensorflow/lite/toco/logging/gen_html.py37function
541539get_input_type_from_signaturetensorflow/tensorflow/lite/toco/logging/gen_html.py41functionParses op_signature and returns a string denoting the input tensor type. Args: op_signature: a string specifying the signature of a particular operator. The signature of an operator contains the input tensor's shape and type, output tensor's shape and type, operator's name and its version. It has the following schema: INPUT:input_1_shape::input_1_type::input_2_shape::input_2_type::.. ::OUTPUT:output_1_shape::output_1_type::output_2_shape::output_2_type:: ..::NAME:operator_name ::VERSION:operator_version An example of an operator signature is: INPUT:[1,73,73,160]::float::[64,1,1,160]::float::[64]::float:: OUTPUT:[1,73,73,64]::float::NAME:Conv::VERSION:1 Returns: A string denoting the input tensors' type. In the form of shape/type separated by comma. For example: shape:[1,73,73,160],type:float,shape:[64,1,1,160],type:float,shape:[64], type:float
542540get_operator_typetensorflow/tensorflow/lite/toco/logging/gen_html.py78function
543541HTMLGeneratortensorflow/tensorflow/lite/toco/logging/gen_html.py87classUtility class to generate an HTML report.
544542generatetensorflow/tensorflow/lite/toco/logging/gen_html.py111methodGenerates the HTML report and writes it to local directory. This function uses the fields in `toco_conversion_log_before` and `toco_conversion_log_after` to populate the HTML content. Certain markers (placeholders) in the HTML template are then substituted with the fields from the protos. Once finished it will write the HTML file to the specified local file path. Args: toco_conversion_log_before: A `TocoConversionLog` protobuf generated before the model is converted by TOCO. toco_conversion_log_after: A `TocoConversionLog` protobuf generated after the model is converted by TOCO. post_training_quant_enabled: A boolean, whether post-training quantization is enabled. dot_before: A string, the dot representation of the model before the conversion. dot_after: A string, the dot representation of the model after the conversion. toco_err_log: A string, the logs emitted by TOCO during conversion. Caller need to ensure that this string is properly anonymized (any kind of user data should be eliminated). tflite_graph_path: A string, the filepath to the converted TFLite model. Raises: RuntimeError: When error occurs while generating the template.
545543gen_conversion_log_htmltensorflow/tensorflow/lite/toco/logging/gen_html.py208functionGenerates an HTML report about the conversion process. Args: conversion_log_dir: A string specifying the file directory of the conversion logs. It's required that before calling this function, the `conversion_log_dir` already contains the following files: `toco_log_before.pb`, `toco_log_after.pb`, `toco_tf_graph.dot`, `toco_tflite_graph.dot`. quantization_enabled: A boolean, passed from the tflite converter to indicate whether post-training quantization is enabled during conversion. tflite_graph_path: A string, the filepath to the converted TFLite model. Raises: IOError: When any of the required files doesn't exist.
546544executetensorflow/tensorflow/lite/toco/python/toco_from_protos.py32functionRuns the converter.
547545TensorNametensorflow/tensorflow/lite/toco/python/toco_from_protos_test.py30functionGet the canonical (non foo:0 name).
548546get_imagetensorflow/tensorflow/lite/tools/convert_image_to_csv.py41functionReturns an image loaded into an np.ndarray with dims [height, width, (3 or 1)]. Args: width: Width to rescale the image to. height: Height to rescale the image to. want_grayscale: Whether the result should be converted to grayscale. filepath: Path of the image file.. Returns: np.ndarray of shape (height, width, channels) where channels is 1 if want_grayscale is true, otherwise 3.
549547array_to_int_csvtensorflow/tensorflow/lite/tools/convert_image_to_csv.py65functionConverts all elements in a numerical array to a comma-separated string. Args: array_data: Numerical array to convert. Returns: String containing array values as integers, separated by commas.
550548convert_bytearray_to_objecttensorflow/tensorflow/lite/tools/flatbuffer_utils.py38functionConverts a tflite model from a bytearray to an object for parsing.
551549read_modeltensorflow/tensorflow/lite/tools/flatbuffer_utils.py44functionReads a tflite model as a python object. Args: input_tflite_file: Full path name to the input tflite file Raises: RuntimeError: If input_tflite_file path is invalid. IOError: If input_tflite_file cannot be opened. Returns: A python object corresponding to the input tflite file.
552550read_model_with_mutable_tensorstensorflow/tensorflow/lite/tools/flatbuffer_utils.py64functionReads a tflite model as a python object with mutable tensors. Similar to read_model() with the addition that the returned object has mutable tensors (read_model() returns an object with immutable tensors). Args: input_tflite_file: Full path name to the input tflite file Raises: RuntimeError: If input_tflite_file path is invalid. IOError: If input_tflite_file cannot be opened. Returns: A mutable python object corresponding to the input tflite file.
553551convert_object_to_bytearraytensorflow/tensorflow/lite/tools/flatbuffer_utils.py83functionConverts a tflite model from an object to a bytearray.
554552write_modeltensorflow/tensorflow/lite/tools/flatbuffer_utils.py93functionWrites the tflite model, a python object, into the output file. Args: model_object: A tflite model as a python object output_tflite_file: Full path name to the output tflite file. Raises: IOError: If output_tflite_file path is invalid or cannot be opened.
555553strip_stringstensorflow/tensorflow/lite/tools/flatbuffer_utils.py108functionStrips all nonessential strings from the model to reduce model size. We remove the following strings: (find strings by searching ":string" in the tensorflow lite flatbuffer schema) 1. Model description 2. SubGraph name 3. Tensor names We retain OperatorCode custom_code and Metadata name. Args: model: The model from which to remove nonessential strings.
556554randomize_weightstensorflow/tensorflow/lite/tools/flatbuffer_utils.py130functionRandomize weights in a model. Args: model: The model in which to randomize weights. random_seed: The input to the random number generator (default value is 0).
557555build_mock_flatbuffer_modeltensorflow/tensorflow/lite/tools/test_utils.py30functionCreates a flatbuffer containing an example model.
558556load_model_from_flatbuffertensorflow/tensorflow/lite/tools/test_utils.py211functionLoads a model as a python object from a flatbuffer model.
559557build_mock_modeltensorflow/tensorflow/lite/tools/test_utils.py218functionCreates an object containing an example model.
560558TensorTypeToNametensorflow/tensorflow/lite/tools/visualize.py202functionConverts a numerical enum to a readable tensor type.
561559BuiltinCodeToNametensorflow/tensorflow/lite/tools/visualize.py210functionConverts a builtin op code enum to a readable name.
562560NameListToStringtensorflow/tensorflow/lite/tools/visualize.py218functionConverts a list of integers to the equivalent ASCII string.
563561OpCodeMappertensorflow/tensorflow/lite/tools/visualize.py229classMaps an opcode index to an op name.
564562DataSizeMappertensorflow/tensorflow/lite/tools/visualize.py245classFor buffers, report the number of bytes.
565563TensorMappertensorflow/tensorflow/lite/tools/visualize.py255classMaps a list of tensor indices to a tooltip hoverable indicator of more.
566564GenerateGraphtensorflow/tensorflow/lite/tools/visualize.py278functionProduces the HTML required to have a d3 visualization of the dag.
567565GenerateTableHtmltensorflow/tensorflow/lite/tools/visualize.py337functionGiven a list of object values and keys to print, make an HTML table. Args: items: Items to print an array of dicts. keys_to_print: (key, display_fn). `key` is a key in the object. i.e. items[0][key] should exist. display_fn is the mapping function on display. i.e. the displayed html cell will have the string returned by `mapping_fn(items[0][key])`. display_index: add a column which is the index of each row in `items`. Returns: An html table.
568566CamelCaseToSnakeCasetensorflow/tensorflow/lite/tools/visualize.py375functionConverts an identifier in CamelCase to snake_case.
569567FlatbufferToDicttensorflow/tensorflow/lite/tools/visualize.py381functionConverts a hierarchy of FB objects into a nested dict. We avoid transforming big parts of the flat buffer into python arrays. This speeds conversion from ten minutes to a few seconds on big graphs. Args: fb: a flat buffer structure. (i.e. ModelT) preserve_as_numpy: true if all downstream np.arrays should be preserved. false if all downstream np.array should become python arrays Returns: A dictionary representing the flatbuffer rather than a flatbuffer object.
570568CreateDictFromFlatbuffertensorflow/tensorflow/lite/tools/visualize.py413function
571569CreateHtmlFiletensorflow/tensorflow/lite/tools/visualize.py419functionGiven a tflite model in `tflite_input` file, produce html description.
572570modify_model_interfacetensorflow/tensorflow/lite/tools/optimize/python/modify_model_interface_lib.py52functionModify a quantized model's interface (input/output) from float to integer. Args: input_file: Full path name to the input tflite file. output_file: Full path name to the output tflite file. input_type: Final input interface type. output_type: Final output interface type. Raises: RuntimeError: If the modification of the model interface was unsuccessful. ValueError: If the input_type or output_type is unsupported.
573571build_tflite_model_with_full_integer_quantizationtensorflow/tensorflow/lite/tools/optimize/python/modify_model_interface_lib_test.py31function
574572get_build_cpustensorflow/tensorflow/lite/tools/pip_package/setup.py72function
575573make_argstensorflow/tensorflow/lite/tools/pip_package/setup.py80functionConstruct make command line.
576574make_outputtensorflow/tensorflow/lite/tools/pip_package/setup.py94functionInvoke make on the target and return output.
577575maketensorflow/tensorflow/lite/tools/pip_package/setup.py99functionInvoke make to build tflite C++ sources. Build dependencies: apt-get install swig libjpeg-dev zlib1g-dev python3-dev python3-nump
578576download_dependenciestensorflow/tensorflow/lite/tools/pip_package/setup.py108functionDownload build dependencies if haven't done yet.
579577CustomBuildExttensorflow/tensorflow/lite/tools/pip_package/setup.py114classCustomized build extension.
580578get_ext_filenametensorflow/tensorflow/lite/tools/pip_package/setup.py117method
581579runtensorflow/tensorflow/lite/tools/pip_package/setup.py123method
582580CustomBuildPytensorflow/tensorflow/lite/tools/pip_package/setup.py130class
583581runtensorflow/tensorflow/lite/tools/pip_package/setup.py132method
584582get_pybind_includetensorflow/tensorflow/lite/tools/pip_package/setup.py137functionpybind11 include directory is not correctly resolved. This fixes include directory to /usr/local/pythonX.X Returns: include directories to find pybind11
585583set_signature_defstensorflow/tensorflow/lite/tools/signature/signature_def_utils.py25functionSets SignatureDefs to the Metadata of a TfLite flatbuffer buffer. Args: tflite_model: Binary TFLite model (bytes or bytes-like object) to which to add signature_def. signature_def_map: dict containing SignatureDefs to store in metadata. Returns: buffer: A TFLite model binary identical to model buffer with metadata field containing SignatureDef. Raises: ValueError: tflite_model buffer does not contain a valid TFLite model. signature_def_map is empty or does not contain a SignatureDef.
586584get_signature_defstensorflow/tensorflow/lite/tools/signature/signature_def_utils.py51functionGet SignatureDef dict from the Metadata of a TfLite flatbuffer buffer. Args: tflite_model: TFLite model buffer to get the signature_def. Returns: dict containing serving names to SignatureDefs if exists, otherwise, empty dict. Raises: ValueError: tflite_model buffer does not contain a valid TFLite model. DecodeError: SignatureDef cannot be parsed from TfLite SignatureDef metadata.
587585clear_signature_defstensorflow/tensorflow/lite/tools/signature/signature_def_utils.py78functionClears SignatureDefs from the Metadata of a TfLite flatbuffer buffer. Args: tflite_model: TFLite model buffer to remove signature_defs. Returns: buffer: A TFLite model binary identical to model buffer with no SignatureDef metadata. Raises: ValueError: tflite_model buffer does not contain a valid TFLite model.
588586read32tensorflow/tensorflow/lite/tutorials/dataset.py35functionRead 4 bytes from bytestream as an unsigned 32-bit integer.
589587check_image_file_headertensorflow/tensorflow/lite/tutorials/dataset.py41functionValidate that filename corresponds to images for the MNIST dataset.
590588check_labels_file_headertensorflow/tensorflow/lite/tutorials/dataset.py57functionValidate that filename corresponds to labels for the MNIST dataset.
591589downloadtensorflow/tensorflow/lite/tutorials/dataset.py67functionDownload (and unzip) a file from the MNIST dataset if not already done.
592590datasettensorflow/tensorflow/lite/tutorials/dataset.py86functionDownload and parse MNIST dataset.
593591traintensorflow/tensorflow/lite/tutorials/dataset.py114functiontf.data.Dataset object for MNIST training data.
594592run_evaltensorflow/tensorflow/lite/tutorials/mnist_tflite.py47functionPerforms evaluation for input image over specified model. Args: interpreter: TFLite interpreter initialized with model to execute. input_image: Image input to the model. Returns: output: output tensor of model being executed.
595593set_dlopen_flagstensorflow/tensorflow/python/pywrap_dlopen_global_flags.py43function
596594reset_dlopen_flagstensorflow/tensorflow/python/pywrap_dlopen_global_flags.py48function
597595import_graphdeftensorflow/tensorflow/python/pywrap_mlir.py26function
598596experimental_convert_saved_model_to_mlirtensorflow/tensorflow/python/pywrap_mlir.py32function
599597experimental_convert_saved_model_v1_to_mlirtensorflow/tensorflow/python/pywrap_mlir.py39function
600598experimental_run_pass_pipelinetensorflow/tensorflow/python/pywrap_mlir.py48function
601599enabletensorflow/tensorflow/python/tf2.py30function
602600disabletensorflow/tensorflow/python/tf2.py36function
603601enabledtensorflow/tensorflow/python/tf2.py42function
604602AssertTransformertensorflow/tensorflow/python/autograph/converters/asserts.py27classTransforms Assert nodes to Call so they can be handled as functions.
605603visit_Asserttensorflow/tensorflow/python/autograph/converters/asserts.py30method
606604transformtensorflow/tensorflow/python/autograph/converters/asserts.py50function
607605BreakTransformertensorflow/tensorflow/python/autograph/converters/break_statements.py39classCanonicalizes break statements into additional conditionals.
608606visit_Breaktensorflow/tensorflow/python/autograph/converters/break_statements.py42method
609607visit_Whiletensorflow/tensorflow/python/autograph/converters/break_statements.py75method
610608visit_Fortensorflow/tensorflow/python/autograph/converters/break_statements.py121method
611609transformtensorflow/tensorflow/python/autograph/converters/break_statements.py183function
612610CallTreeTransformertensorflow/tensorflow/python/autograph/converters/call_trees.py96classTransforms the call tree by renaming transformed symbols.
613611visit_Lambdatensorflow/tensorflow/python/autograph/converters/call_trees.py99method
614612visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/call_trees.py108method
615613visit_Withtensorflow/tensorflow/python/autograph/converters/call_trees.py126method
616614visit_Calltensorflow/tensorflow/python/autograph/converters/call_trees.py160method
617615transformtensorflow/tensorflow/python/autograph/converters/call_trees.py211functionTransform function call to the compiled counterparts. Args: node: AST ctx: EntityContext Returns: A tuple (node, new_names): node: The transformed AST new_names: set(string), containing any newly-generated names
618616MockConvertedCalltensorflow/tensorflow/python/autograph/converters/call_trees_test.py30class
619617ConditionalExpressionTransformertensorflow/tensorflow/python/autograph/converters/conditional_expressions.py28classConverts conditional expressions to functional form.
620618visit_IfExptensorflow/tensorflow/python/autograph/converters/conditional_expressions.py31method
621619transformtensorflow/tensorflow/python/autograph/converters/conditional_expressions.py48function
622620ContinueCanonicalizationTransformertensorflow/tensorflow/python/autograph/converters/continue_statements.py60classCanonicalizes continue statements into additional conditionals.
623621visit_Continuetensorflow/tensorflow/python/autograph/converters/continue_statements.py63method
624622visit_Whiletensorflow/tensorflow/python/autograph/converters/continue_statements.py125method
625623visit_Fortensorflow/tensorflow/python/autograph/converters/continue_statements.py132method
626624visit_Iftensorflow/tensorflow/python/autograph/converters/continue_statements.py140method
627625visit_Withtensorflow/tensorflow/python/autograph/converters/continue_statements.py145method
628626visit_Trytensorflow/tensorflow/python/autograph/converters/continue_statements.py150method
629627visit_ExceptHandlertensorflow/tensorflow/python/autograph/converters/continue_statements.py158method
630628transformtensorflow/tensorflow/python/autograph/converters/continue_statements.py163function
631629ControlFlowTransformertensorflow/tensorflow/python/autograph/converters/control_flow.py46classTransforms control flow structures like loops an conditionals.
632630visit_Lambdatensorflow/tensorflow/python/autograph/converters/control_flow.py49method
633631visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/control_flow.py54method
634632visit_Iftensorflow/tensorflow/python/autograph/converters/control_flow.py206method
635633visit_Whiletensorflow/tensorflow/python/autograph/converters/control_flow.py261method
636634visit_Fortensorflow/tensorflow/python/autograph/converters/control_flow.py309method
637635AnnotatedDeftensorflow/tensorflow/python/autograph/converters/control_flow.py395class
638636transformtensorflow/tensorflow/python/autograph/converters/control_flow.py402function
639637ControlFlowTransformertensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py44classTransforms control flow structures like loops an conditionals.
640638visit_Iftensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py200method
641639visit_Whiletensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py400method
642640visit_Fortensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py490method
643641AnnotatedDeftensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py623class
644642transformtensorflow/tensorflow/python/autograph/converters/control_flow_deprecated_py2.py630function
645643DirectivesTransformertensorflow/tensorflow/python/autograph/converters/directives.py90classParses compiler directives and converts them into AST annotations.
646644visit_Nametensorflow/tensorflow/python/autograph/converters/directives.py117method
647645visit_Attributetensorflow/tensorflow/python/autograph/converters/directives.py126method
648646visit_Assigntensorflow/tensorflow/python/autograph/converters/directives.py134method
649647visit_AugAssigntensorflow/tensorflow/python/autograph/converters/directives.py138method
650648visit_Exprtensorflow/tensorflow/python/autograph/converters/directives.py142method
651649visit_Whiletensorflow/tensorflow/python/autograph/converters/directives.py173method
652650visit_Fortensorflow/tensorflow/python/autograph/converters/directives.py176method
653651transformtensorflow/tensorflow/python/autograph/converters/directives.py180function
654652FunctionTransformertensorflow/tensorflow/python/autograph/converters/functions.py38classWraps function bodies around autograph-specific boilerplate.
655653visit_Lambdatensorflow/tensorflow/python/autograph/converters/functions.py53method
656654visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/functions.py81method
657655transformtensorflow/tensorflow/python/autograph/converters/functions.py134function
658656FunctionTransformertensorflow/tensorflow/python/autograph/converters/functions_test.py31class
659657ftensorflow/tensorflow/python/autograph/converters/functions_test.py35methodDocstring.
660658ftensorflow/tensorflow/python/autograph/converters/functions_test.py49methodFirst sentence. Second sentence. Returns: Something.
661659ftensorflow/tensorflow/python/autograph/converters/functions_test.py68method
662660inner_fn_calleetensorflow/tensorflow/python/autograph/converters/functions_test.py85method
663661ftensorflow/tensorflow/python/autograph/converters/functions_test.py89method
664662ftensorflow/tensorflow/python/autograph/converters/functions_test.py121method
665663inner_fntensorflow/tensorflow/python/autograph/converters/functions_test.py70method
666664inner_fntensorflow/tensorflow/python/autograph/converters/functions_test.py90method
667665ftensorflow/tensorflow/python/autograph/converters/functions_test.py104method
668666inner_fntensorflow/tensorflow/python/autograph/converters/functions_test.py106method
669667ListCompTransformertensorflow/tensorflow/python/autograph/converters/list_comprehensions.py42classLowers list comprehensions into standard control flow.
670668visit_Assigntensorflow/tensorflow/python/autograph/converters/list_comprehensions.py45method
671669transformtensorflow/tensorflow/python/autograph/converters/list_comprehensions.py81function
672670ListTransformertensorflow/tensorflow/python/autograph/converters/lists.py51classConverts lists and related operations to their TF counterpart.
673671visit_Listtensorflow/tensorflow/python/autograph/converters/lists.py54method
674672visit_Calltensorflow/tensorflow/python/autograph/converters/lists.py131method
675673visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/lists.py209method
676674visit_Fortensorflow/tensorflow/python/autograph/converters/lists.py215method
677675visit_Whiletensorflow/tensorflow/python/autograph/converters/lists.py221method
678676visit_Iftensorflow/tensorflow/python/autograph/converters/lists.py227method
679677visit_Withtensorflow/tensorflow/python/autograph/converters/lists.py233method
680678transformtensorflow/tensorflow/python/autograph/converters/lists.py239function
681679LogicalExpressionTransformertensorflow/tensorflow/python/autograph/converters/logical_expressions.py49classConverts logical expressions to corresponding TF calls.
682680visit_Comparetensorflow/tensorflow/python/autograph/converters/logical_expressions.py83method
683681visit_UnaryOptensorflow/tensorflow/python/autograph/converters/logical_expressions.py114method
684682visit_BoolOptensorflow/tensorflow/python/autograph/converters/logical_expressions.py123method
685683transformtensorflow/tensorflow/python/autograph/converters/logical_expressions.py135function
686684ConditionalReturnRewritertensorflow/tensorflow/python/autograph/converters/return_statements.py43classRewrites a a pattern where it's unobvious that all paths return a value. This rewrite allows avoiding intermediate None return values. The following pattern: if cond: <block 1> return else: <block 2> <block 3> is converted to: if cond: <block 1> return else: <block 2> <block 3> and vice-versa (if the else returns, subsequent statements are moved under the if branch).
687685visit_Returntensorflow/tensorflow/python/autograph/converters/return_statements.py70method
688686visit_Whiletensorflow/tensorflow/python/autograph/converters/return_statements.py100method
689687visit_Fortensorflow/tensorflow/python/autograph/converters/return_statements.py106method
690688visit_Withtensorflow/tensorflow/python/autograph/converters/return_statements.py113method
691689visit_Trytensorflow/tensorflow/python/autograph/converters/return_statements.py120method
692690visit_ExceptHandlertensorflow/tensorflow/python/autograph/converters/return_statements.py130method
693691visit_Iftensorflow/tensorflow/python/autograph/converters/return_statements.py135method
694692visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/return_statements.py153method
695693ReturnStatementsTransformertensorflow/tensorflow/python/autograph/converters/return_statements.py183classLowers return statements into variables and conditionals. Specifically, the following pattern: <block 1> return val <block 2> is converted to: do_return = False retval = None <block 1> do_return = True retval = val if not do_return: <block 2> return retval The conversion adjusts loops as well: <block 1> while cond: <block 2> return retval is converted to: <block 1> while not do_return and cond: <block 2> do_return = True retval = val
696694visit_Returntensorflow/tensorflow/python/autograph/converters/return_statements.py227method
697695visit_Whiletensorflow/tensorflow/python/autograph/converters/return_statements.py283method
698696visit_Fortensorflow/tensorflow/python/autograph/converters/return_statements.py297method
699697visit_Withtensorflow/tensorflow/python/autograph/converters/return_statements.py319method
700698visit_Trytensorflow/tensorflow/python/autograph/converters/return_statements.py324method
701699visit_ExceptHandlertensorflow/tensorflow/python/autograph/converters/return_statements.py331method
702700visit_Iftensorflow/tensorflow/python/autograph/converters/return_statements.py335method
703701visit_FunctionDeftensorflow/tensorflow/python/autograph/converters/return_statements.py341method
704702transformtensorflow/tensorflow/python/autograph/converters/return_statements.py392functionEnsure a function has only a single return, at the end.
705703SliceTransformertensorflow/tensorflow/python/autograph/converters/slices.py28classConverts slicing operations to their TF counterpart. Currently, relying on the default slice operator that Tensor uses is insufficient, because TensorArray and tensor lists use dedicated index read and write functions.
706704visit_Assigntensorflow/tensorflow/python/autograph/converters/slices.py48method
707705visit_Subscripttensorflow/tensorflow/python/autograph/converters/slices.py58method
708706transformtensorflow/tensorflow/python/autograph/converters/slices.py84function
709707VariableAccessTransformertensorflow/tensorflow/python/autograph/converters/variables.py28classRewrites basic symbol reads. This transformer rewrites variable reads with a "read" operator which allows tracking activity. Example: For a basic statement: a = b + c This is translated to: a = ld(b) + ld(c) Augmented assignment operations also introduce a `ld` operator: a += b The assignment target also receives an operator to properly represent the read: a = ld(a) a += ld(b)
710708visit_Nametensorflow/tensorflow/python/autograph/converters/variables.py55method
711709visit_Deletetensorflow/tensorflow/python/autograph/converters/variables.py63method
712710visit_AugAssigntensorflow/tensorflow/python/autograph/converters/variables.py88method
713711transformtensorflow/tensorflow/python/autograph/converters/variables.py100function
714712control_status_ctxtensorflow/tensorflow/python/autograph/core/ag_ctx.py35function
715713Statustensorflow/tensorflow/python/autograph/core/ag_ctx.py40class
716714ControlStatusCtxtensorflow/tensorflow/python/autograph/core/ag_ctx.py46classA context that tracks whether autograph is enabled by the user.
717715NullCtxtensorflow/tensorflow/python/autograph/core/ag_ctx.py66classHelper substitute for contextlib.nullcontext.
718716Ruletensorflow/tensorflow/python/autograph/core/config_lib.py27classBase class for conversion rules.
719717matchestensorflow/tensorflow/python/autograph/core/config_lib.py33method
720718Actiontensorflow/tensorflow/python/autograph/core/config_lib.py38class
721719DoNotConverttensorflow/tensorflow/python/autograph/core/config_lib.py44classIndicates that this module should be not converted.
722720get_actiontensorflow/tensorflow/python/autograph/core/config_lib.py50method
723721Converttensorflow/tensorflow/python/autograph/core/config_lib.py56classIndicates that this module should be converted.
724722get_actiontensorflow/tensorflow/python/autograph/core/config_lib.py62method
725723Featuretensorflow/tensorflow/python/autograph/core/converter.py83classThis enumeration represents optional conversion options. These conversion options are experimental. They are subject to change without notice and offer no guarantees. _Example Usage_ ```python optionals= tf.autograph.experimental.Feature.EQUALITY_OPERATORS @tf.function(experimental_autograph_options=optionals) def f(i): if i == 0: # EQUALITY_OPERATORS allows the use of == here. tf.print('i is zero') ``` Attributes: ALL: Enable all features. AUTO_CONTROL_DEPS: Insert of control dependencies in the generated code. ASSERT_STATEMENTS: Convert Tensor-dependent assert statements to tf.Assert. BUILTIN_FUNCTIONS: Convert builtin functions applied to Tensors to their TF counterparts. EQUALITY_OPERATORS: Whether to convert the comparison operators, like equality. This is soon to be deprecated as support is being added to the Tensor class. LISTS: Convert list idioms, like initializers, slices, append, etc. NAME_SCOPES: Insert name scopes that name ops according to context, like the function they were defined in.
726724alltensorflow/tensorflow/python/autograph/core/converter.py123methodReturns a tuple that enables all options.
727725all_buttensorflow/tensorflow/python/autograph/core/converter.py128methodReturns a tuple that enables all but the excluded options.
728726ConversionOptionstensorflow/tensorflow/python/autograph/core/converter.py138classImmutable container for global conversion flags. Attributes: recursive: bool, whether to recursively convert any user functions or classes that the converted function may use. user_requested: bool, whether the conversion was explicitly requested by the user, as opposed to being performed as a result of other logic. This value always auto-resets resets to False in child conversions. optional_features: Union[Feature, Set[Feature]], controls the use of optional features in the conversion process. See Feature for available options.
729727as_tupletensorflow/tensorflow/python/autograph/core/converter.py169method
730728usestensorflow/tensorflow/python/autograph/core/converter.py183method
731729call_optionstensorflow/tensorflow/python/autograph/core/converter.py187methodReturns the corresponding options to be used for recursive conversion.
732730to_asttensorflow/tensorflow/python/autograph/core/converter.py195methodReturns a representation of this object as an AST node. The AST node encodes a constructor that would create an object with the same contents. Returns: ast.Node
733731list_of_featurestensorflow/tensorflow/python/autograph/core/converter.py215method
734732ProgramContexttensorflow/tensorflow/python/autograph/core/converter.py236classProgramContext keeps track of converting function hierarchies. Attributes: options: ConversionOptions autograph_module: Deprecated. Do not use.
735733Basetensorflow/tensorflow/python/autograph/core/converter.py249classAll converters should inherit from this class. Attributes: ctx: EntityContext
736734get_definition_directivetensorflow/tensorflow/python/autograph/core/converter.py262methodReturns the unique directive argument for a symbol. See lang/directives.py for details on directives. Example: # Given a directive in the code: ag.foo_directive(bar, baz=1) # One can write for an AST node Name(id='bar'): get_definition_directive(node, ag.foo_directive, 'baz') Args: node: ast.AST, the node representing the symbol for which the directive argument is needed. directive: Callable[..., Any], the directive to search. arg: str, the directive argument to return. default: Any Raises: ValueError: if conflicting annotations have been found
737735visittensorflow/tensorflow/python/autograph/core/converter.py311method
738736allowlisttensorflow/tensorflow/python/autograph/core/converter_testing.py35functionHelper that marks a callable as whtelitisted.
739737is_inside_generated_codetensorflow/tensorflow/python/autograph/core/converter_testing.py47functionTests whether the caller is generated code. Implementation-specific.
740738FunctionScopetensorflow/tensorflow/python/autograph/core/function_wrappers.py33classContext manager that wraps the body of a converted function. This context manager handles various operations related to the scope of a function: * optional TF name scopes - these name scopes match the name of the function, for easy visualization in tensorBoard; * optional automatic control dependencies - this adds the same mechanism for control dependencies that is used by `@tf.function`; it can be optionally enabled when using `tf.autograph.to_graph`; * tracking of autograph conversion state (whether it's enabled by the user, conversion options;
741739rettensorflow/tensorflow/python/autograph/core/function_wrappers.py91methodMarks a value as returned from the function guarded by the scope.
742740with_function_scopetensorflow/tensorflow/python/autograph/core/function_wrappers.py114functionInline version of the FunctionScope context manager.
743741UnsupportedFeaturesCheckertensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py26classQuick check for Python features we know we don't support. Any features detected will cause AutoGraph to not compile a function.
744742visit_Attributetensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py32method
745743visit_Fortensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py39method
746744visit_Whiletensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py45method
747745visit_Yieldtensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py53method
748746visit_YieldFromtensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py56method
749747verifytensorflow/tensorflow/python/autograph/core/unsupported_features_checker.py60function
750748is_autograph_strict_conversion_modetensorflow/tensorflow/python/autograph/impl/api.py72function
751749AutoGraphErrortensorflow/tensorflow/python/autograph/impl/api.py82classBase class for all AutoGraph exceptions.
752750ConversionErrortensorflow/tensorflow/python/autograph/impl/api.py87classRaised during the conversion process.
753751StagingErrortensorflow/tensorflow/python/autograph/impl/api.py92classRaised during the staging (i.e. Python execution) of converted code.
754752StackTraceMappertensorflow/tensorflow/python/autograph/impl/api.py166classRemaps generated code to code it originated from.
755753get_effective_source_maptensorflow/tensorflow/python/autograph/impl/api.py172method
756754PyToTFtensorflow/tensorflow/python/autograph/impl/api.py203classThe TensorFlow AutoGraph transformer.
757755get_transformed_nametensorflow/tensorflow/python/autograph/impl/api.py228method
758756get_extra_localstensorflow/tensorflow/python/autograph/impl/api.py231method
759757get_caching_keytensorflow/tensorflow/python/autograph/impl/api.py234method
760758initial_analysistensorflow/tensorflow/python/autograph/impl/api.py237method
761759transform_asttensorflow/tensorflow/python/autograph/impl/api.py250method
762760autograph_artifacttensorflow/tensorflow/python/autograph/impl/api.py298function
763761is_autograph_artifacttensorflow/tensorflow/python/autograph/impl/api.py303function
764762converted_calltensorflow/tensorflow/python/autograph/impl/api.py307functionConverts a function call inline. For internal use only. Note: The argument list is optimized for readability of generated code, which may look like this: ag__.converted_call(f, (arg1, arg2), None, fscope) ag__.converted_call(f, (), dict(arg1=val1, **kwargs), fscope) ag__.converted_call(f, (arg1, arg2) + varargs, dict(**kwargs), lscope) Args: f: The function to convert. args: Tuple, the original positional arguments of f kwargs: Optional[Dict], the original keyword arguments of f caller_fn_scope: Optional[function_wrappers.FunctionScope], the function scope of the converted function in which this call was originally made. options: Optional[converter.ConversionOptions], conversion options. If not specified, the value of caller_fn_scope.callopts is used. Either options or caller_fn_scope must be present. Returns: Any, the result of executing a possibly-converted `f` with the given arguments.
765763tf_converttensorflow/tensorflow/python/autograph/impl/api.py506functionDecorator that applies AutoGraph to a function. Use in internal APIs. This API is suitable for high order functions internal to the TensorFlow API, and more generally any function to which Autograph is not applied. Guidance: convert was a decorator meant for use directly by developers, and will be soon deprecated in favor of tf.function. tf_convert is to be called from high order functions internal to TF. Args: f: Callable. ctx: ag_ctx.ControlStatusCtx, the Autograph context in which `f` is used. convert_by_default: bool, whether to use AutoGraph when the context doesn't specify. user_requested: bool, whether to ignore the conversion allowlist. See ConversionOptions.user_requested. Returns: Either `f or the converted version of `f`.
766764call_with_unspecified_conversion_statustensorflow/tensorflow/python/autograph/impl/api.py565functionDecorator that resets the conversion context to the unspecified status.
767765do_not_converttensorflow/tensorflow/python/autograph/impl/api.py599functionDecorator that suppresses the conversion of a function. Args: func: function to decorate. Returns: If `func` is not None, returns a `Callable` which is equivalent to `func`, but is not converted by AutoGraph. If `func` is None, returns a decorator that, when invoked with a single `func` argument, returns a `Callable` equivalent to the above case.
768766converttensorflow/tensorflow/python/autograph/impl/api.py626functionDecorator that compiles a function to use TensorFlow ops. The decorator is dynamic - it recompiles the target whenever the decorated function is called. This means the parameter values are known at conversion. It also means that repeated calls with different types of parameters will be correctly processed. Args: recursive: bool, whether to recursively convert any functions or classes that the converted function may use. optional_features: converted.Feature, allows toggling optional or experimental features. When set to None, only the core features are enabled. user_requested: bool, whether this is a function that the user explicitly asked to be converted. See ConversionOptions.user_requested. conversion_ctx: Optional ag_ctx.ControlStatusCtx, the Autograph context in which `f` is used. Returns: Callable, a decorator that converts the given function into an equivalent function that uses TensorFlow ops.
769767to_graphtensorflow/tensorflow/python/autograph/impl/api.py682functionConverts a Python entity into a TensorFlow graph. Also see: `tf.autograph.to_code`, `tf.function`. Unlike `tf.function`, `to_graph` is a low-level transpiler that converts Python code to TensorFlow graph code. It does not implement any caching, variable management or create any actual ops, and is best used where greater control over the generated TensorFlow graph is desired. Another difference from `tf.function` is that `to_graph` will not wrap the graph into a TensorFlow function or a Python callable. Internally, `tf.function` uses `to_graph`. Example usage: >>> def f(x): ... if x > 0: ... y = x * x ... else: ... y = -x ... return y ... >>> converted_f = to_graph(f) >>> x = tf.constant(2) >>> converted_f(x) # converted_foo is like a TensorFlow Op. <tf.Tensor: shape=(), dtype=int32, numpy=4> Supported Python entities include: * functions * classes * object methods Functions are converted into new functions with converted code. Classes are converted by generating a new class whose methods use converted code. Methods are converted into unbound function that have an additional first argument called `self`. For a tutorial, see the [tf.function and AutoGraph guide](https://www.tensorflow.org/guide/function). For more detailed information, see the [AutoGraph reference documentation](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/index.md). Args: entity: Python callable or class to convert. recursive: Whether to recursively convert any functions that the converted function may call. experimental_optional_features: `None`, a tuple of, or a single `tf.autograph.experimental.Feature` value. Returns: Same as `entity`, the converted Python function or class. Raises: ValueError: If the entity could not be converted.
770768to_graph_v1tensorflow/tensorflow/python/autograph/impl/api.py754functionConverts a Python entity into a TensorFlow graph. Also see: `tf.autograph.to_code`, `tf.function`. Unlike `tf.function`, `to_graph` is a low-level transpiler that converts Python code to TensorFlow graph code. It does not implement any caching, variable management or create any actual ops, and is best used where greater control over the generated TensorFlow graph is desired. Another difference from `tf.function` is that `to_graph` will not wrap the graph into a TensorFlow function or a Python callable. Internally, `tf.function` uses `to_graph`. _Example Usage_ ```python def foo(x): if x > 0: y = x * x else: y = -x return y converted_foo = to_graph(foo) x = tf.constant(1) y = converted_foo(x) # converted_foo is a TensorFlow Op-like. assert is_tensor(y) ``` Supported Python entities include: * functions * classes * object methods Functions are converted into new functions with converted code. Classes are converted by generating a new class whose methods use converted code. Methods are converted into unbound function that have an additional first argument called `self`. Args: entity: Python callable or class to convert. recursive: Whether to recursively convert any functions that the converted function may call. arg_values: Deprecated. arg_types: Deprecated. experimental_optional_features: `None`, a tuple of, or a single `tf.autograph.experimental.Feature` value. Returns: Same as `entity`, the converted Python function or class. Raises: ValueError: If the entity could not be converted.
771769to_code_v1tensorflow/tensorflow/python/autograph/impl/api.py825functionReturns the source code generated by AutoGraph, as a string. Example usage: >>> def f(x): ... if x < 0: ... x = -x ... return x >>> tf.autograph.to_code(f) "...def tf__f(x):..." Also see: `tf.autograph.to_graph`. Note: If a function has been decorated with `tf.function`, pass its underlying Python function, rather than the callable that `tf.function creates: >>> @tf.function ... def f(x): ... if x < 0: ... x = -x ... return x >>> tf.autograph.to_code(f.python_function) "...def tf__f(x):..." Args: entity: Python callable or class. recursive: Whether to recursively convert any functions that the converted function may call. arg_values: Deprecated. arg_types: Deprecated. indentation: Deprecated. experimental_optional_features: `None`, a tuple of, or a single `tf.autograph.experimental.Feature` value. Returns: The converted code as string.
772770to_codetensorflow/tensorflow/python/autograph/impl/api.py879functionReturns the source code generated by AutoGraph, as a string. Example usage: >>> def f(x): ... if x < 0: ... x = -x ... return x >>> tf.autograph.to_code(f) "...def tf__f(x):..." Also see: `tf.autograph.to_graph`. Note: If a function has been decorated with `tf.function`, pass its underlying Python function, rather than the callable that `tf.function creates: >>> @tf.function ... def f(x): ... if x < 0: ... x = -x ... return x >>> tf.autograph.to_code(f.python_function) "...def tf__f(x):..." Args: entity: Python callable or class to convert. recursive: Whether to recursively convert any functions that the converted function may call. experimental_optional_features: `None`, a tuple of, or a single `tf.autograph.experimental.Feature` value. Returns: The converted code as string.
773771is_unsupportedtensorflow/tensorflow/python/autograph/impl/conversion.py73functionChecks whether an entity is supported by AutoGraph at all.
774772is_allowlistedtensorflow/tensorflow/python/autograph/impl/conversion.py116functionChecks whether an entity is allowed for use in graph mode. Examples of allowed entities include all members of the tensorflow package. Args: o: A Python entity. check_call_override: Reserved for internal use. When set to `False`, it disables the rule according to which classes are allowed if their __call__ method is allowed. allow_namedtuple_subclass: Reserved for internal use. When `True`, namedtuple subclasses are not allowed. Returns: Boolean
775773is_in_allowlist_cachetensorflow/tensorflow/python/autograph/impl/conversion.py221function
776774cache_allowlistedtensorflow/tensorflow/python/autograph/impl/conversion.py229function
777775set_element_typetensorflow/tensorflow/python/autograph/lang/directives.py33functionIndicates that the entity is expected hold items of specified type/shape. The staged TensorFlow ops will reflect and assert this data type. Ignored otherwise. Args: entity: The entity to annotate. dtype: TensorFlow dtype value to assert for entity. shape: Optional shape to assert for entity.
778776set_loop_optionstensorflow/tensorflow/python/autograph/lang/directives.py50functionSpecifies additional arguments to be passed to the enclosing while_loop. The parameters apply to and only to the immediately enclosing loop. It only has effect if the loop is staged as a TF while_loop; otherwise the parameters have no effect. Usage: >>> @tf.function(autograph=True) ... def f(): ... n = 0 ... for i in tf.range(10): ... tf.autograph.experimental.set_loop_options(maximum_iterations=3) ... n += 1 ... return n >>> @tf.function(autograph=True) ... def f(): ... v = tf.constant((0,)) ... for i in tf.range(3): ... tf.autograph.experimental.set_loop_options( ... shape_invariants=[(v, tf.TensorShape([None]))] ... ) ... v = tf.concat((v, [i]), 0) ... return v Also see tf.while_loop. Args: parallel_iterations: The maximum number of iterations allowed to run in parallel at any given time. Note that this does not guarantee parallel execution. swap_memory: Whether to store intermediate values needed for gradients on the CPU instead of GPU. maximum_iterations: Allows limiting the total number of iterations executed by the loop. shape_invariants: Allows controlling the argument with the same name passed to tf.while_loop. Unlike tf.while_loop, this is a list of `(tensor, shape)` pairs.
779777match_staging_leveltensorflow/tensorflow/python/autograph/lang/special_functions.py50functionCasts a value to be staged at the same level as another.
780778tensor_listtensorflow/tensorflow/python/autograph/lang/special_functions.py57functionCreates an tensor list and populates it with the given elements. This function provides a more uniform access to tensor lists and tensor arrays, and allows optional initialization. Note: this function is a simplified wrapper. If you need greater control, it is recommended to use the underlying implementation directly. Args: elements: Iterable[tf.Tensor, ...], the elements to initially fill the list with element_dtype: Optional[tf.DType], data type for the elements in the list; required if the list is empty element_shape: Optional[tf.TensorShape], shape for the elements in the list; required if the list is empty use_tensor_array: bool, whether to use the more compatible but restrictive tf.TensorArray implementation Returns: Union[tf.Tensor, tf.TensorArray], the new list. Raises: ValueError: for invalid arguments
781779stacktensorflow/tensorflow/python/autograph/lang/special_functions.py92functionStacks the input, if it admits the notion of stacking. For example, a list of tensors can be stacked into a larger tensor. This function is similar to tf.stack, but it accepts non-lists and lists of non-tensors as arguments. In the latter case, the function does nothing. Args: list_or_tensor: Any element_dtype: tf.DType, optional dtypedtype for the elements in the list. Required if the input is stackable, and the list is untyped. strict: bool, if True an error is raised if the input is not stackable. Otherwise the function is a no-op. Returns: Any, if the input is stackable, the result will be a tf.Tensor. Otherwise, if strict=False, the result will be list_or_tensor. Raises: ValueError: if strict=True and the input is not stackable.
782780if_exptensorflow/tensorflow/python/autograph/operators/conditional_expressions.py27function
783781verify_single_cond_vartensorflow/tensorflow/python/autograph/operators/control_flow.py233functionVerifies whether body_var and orelse_var are consistent.
784782for_stmttensorflow/tensorflow/python/autograph/operators/control_flow.py291functionFunctional form of a for statement. The loop operates on a state, which includes all symbols that are variant across loop iterations, excluding the variables local to the loop. For example, given the loop below that calculates the geometric and arithmetic means or some numbers: ``` geo_mean = 1 arith_mean = 0 for i in range(n): a = numbers[i] geo_mean *= a arith_mean += a ``` The state is represented by the variables geo_mean and arith_mean. The `extra_test`, `body`, `get_state` and `set_state` functions must bind to the original `geo_mean` and `arith_mean` symbols, using `nonlocal`. The inputs and outputs of the callables representing the loop blocks are not explicit - instead, these functions must use nonlocal/global for side effects. The inputs and outputs are instead controlled by the set_state/get_state functions. Args: iter_: The entity being iterated over. extra_test: Callable with boolean return type. An additional loop condition. body: Callable representing the actual loop body. get_state: Additional callable which can capture additional state (such as the values of composite symbols). This is only useful when staging the loop. set_state: Additional callable which save values captured by get_state back into the Python environment. This is only useful when staging the loop. symbol_names: Tuple containing names of the loop variables returned by get_state. opts: Optional dict of extra loop parameters. Returns: Tuple containing the final state.
785783while_stmttensorflow/tensorflow/python/autograph/operators/control_flow.py727functionFunctional form of a while statement. The loop operates on a so-called state, which includes all symbols that are variant across loop iterations. In what follows we refer to state as either a tuple of entities that represent an actual state, or a list of arguments of the corresponding types. The inputs and outputs of the callables representing the loop blocks are not explicit - instead, these functions must use nonlocal/global for side effects. The inputs and outputs are instead controlled by the set_state/get_state functions. Args: test: Callable with boolean return type. The loop condition. body: Callable representing the actual loop body. get_state: Additional callable which can capture additional state (such as the values of composite symbols). This is only useful when staging the loop. set_state: Additional callable which save values captured by get_state back into the Python environment. This is only useful when staging the loop. symbol_names: Tuple containing the names of all loop variables. opts: Optional dict of extra loop parameters. Returns: Tuple containing the final state.
786784if_stmttensorflow/tensorflow/python/autograph/operators/control_flow.py915functionFunctional form of an if statement. The conditional operates on a state, which includes all symbols whose values are a function of the branch taken. For example, given the code below that calculates the abs function: ``` x = 1 if x > 0: x = -x ``` The state is represented by the variable `x`. The `body, `orelse` and `set_state` functions must bind to the original `x` symbol, using `nonlocal`. The inputs and outputs of the callables representing the loop blocks are not explicit - instead, these functions must use nonlocal/global for side effects. The inputs and outputs are instead controlled by the set_state/get_state functions. Args: cond: Boolean. body: Callable representing the main block of the conditional. orelse: Callable representing the else block of the conditional. get_state: Function that returns a tuple containing the values of all composite symbols modified within the conditional. This allows access to state that branches may mutate through side effects. This function is not needed and should not be called when dispatching to code matching Python's default semantics. This is useful for checkpointing to avoid unintended side-effects when staging requires evaluating all code-paths. set_state: Function to set the values of all composite symbols modified within the conditional. This is the complement to get_state, used to restore checkpointed values. The single argument a tuple containing values for each composite symbol that may be modified in a branch of the conditional. The is usually the result of a call to get_state. symbol_names: Tuple containing basic loop var names. nouts: Number of variables output by the statement. Vars which are not outputs will not be passed through staged control flow such as tf.cond. This includes variables that are defined before the conditional, but are not used after it.
787785for_stmttensorflow/tensorflow/python/autograph/operators/control_flow_deprecated_py2.py279functionFunctional form of a for statement. The loop operates on a state, which includes all symbols that are variant across loop iterations, excluding the iterate as well as the variables local to the loop. For example, given the loop below that calculates the geometric and arithmetic means or some numbers: geo_mean = 1 arith_mean = 0 for i in range(n): a = numbers[i] geo_mean *= a arith_mean += a The state is represented by the variables geo_mean and arith_mean. The argument for initial_state may contain the tuple (1, 0), the body will include the arguments geo_mean and arith_mean and will return a tuple representing the new values for geo_mean and respectively arith_mean. Args: iter_: The entity being iterated over. extra_test: Callable with the state as arguments, and boolean return type. An additional loop condition. body: Callable with the iterate and the state as arguments, and state as return type. The actual loop body. get_state: Additional callable which can capture additional state (such as the values of composite symbols). This is only useful when staging the loop. set_state: Additional callable which save values captured by get_state back into the Python environment. This is only useful when staging the loop. init_vars: Tuple containing the initial state. basic_symbol_names: Tuple containing basic loop var names. composite_symbol_names: Tuple containing composite loop var names. opts: Optional dict of extra loop parameters. Returns: Tuple containing the final state.
788786while_stmttensorflow/tensorflow/python/autograph/operators/control_flow_deprecated_py2.py817functionFunctional form of a while statement. The loop operates on a so-called state, which includes all symbols that are variant across loop iterations. In what follows we refer to state as either a tuple of entities that represent an actual state, or a list of arguments of the corresponding types. Args: test: Callable with the state as arguments, and boolean return type. The loop condition. body: Callable with the state as arguments, and state as return type. The actual loop body. get_state: Additional callable which can capture additional state (such as the values of composite symbols). This is only useful when staging the loop. set_state: Additional callable which save values captured by get_state back into the Python environment. This is only useful when staging the loop. init_vars: Tuple containing the initial state. basic_symbol_names: Tuple containing basic loop var names. composite_symbol_names: Tuple containing composite loop var names. opts: Optional dict of extra loop parameters. Returns: Tuple containing the final state.
789787if_stmttensorflow/tensorflow/python/autograph/operators/control_flow_deprecated_py2.py1008functionFunctional form of an if statement. Args: cond: Boolean. body: Callable with no arguments, and outputs of the positive (if) branch as return type. orelse: Callable with no arguments, and outputs of the negative (else) branch as return type. get_state: Function that returns a tuple containing the values of all composite symbols modified within the conditional. This allows access to state that branches may mutate through side effects. This function is not needed and should not be called when dispatching to code matching Python's default semantics. This is useful for checkpointing to avoid unintended side-effects when staging requires evaluating all code-paths. set_state: Function to set the values of all composite symbols modified within the conditional. This is the complement to get_state, used to restore checkpointed values. The single argument a tuple containing values for each composite symbol that may be modified in a branch of the conditional. The is usually the result of a call to get_state. basic_symbol_names: Tuple containing basic loop var names. composite_symbol_names: Tuple containing composite loop var names. Returns: Tuple containing the statement outputs.
790788tf_if_stmttensorflow/tensorflow/python/autograph/operators/control_flow_deprecated_py2.py1048functionOverload of if_stmt that stages a TF cond.
791789new_listtensorflow/tensorflow/python/autograph/operators/data_structures.py36functionThe list constructor. Args: iterable: Optional elements to fill the list with. Returns: A list-like object. The exact return value depends on the initial elements.
792790tf_tensor_array_newtensorflow/tensorflow/python/autograph/operators/data_structures.py57functionOverload of new_list that stages a Tensor list creation.
793791tf_tensor_list_newtensorflow/tensorflow/python/autograph/operators/data_structures.py107functionOverload of new_list that stages a Tensor list creation.
794792list_appendtensorflow/tensorflow/python/autograph/operators/data_structures.py171functionThe list append function. Note: it is unspecified where list_ will be mutated or not. If list_ is a TensorFlow entity, it will not be typically mutated. If list_ is a plain list, it will be. In general, if the list is mutated then the return value should point to the original entity. Args: list_: An entity that supports append semantics. x: The element to append. Returns: Same as list_, after the append was performed. Raises: ValueError: if list_ is not of a known list-like type.
795793ListPopOptstensorflow/tensorflow/python/autograph/operators/data_structures.py230class
796794list_poptensorflow/tensorflow/python/autograph/operators/data_structures.py235functionThe list pop function. Note: it is unspecified where list_ will be mutated or not. If list_ is a TensorFlow entity, it will not be typically mutated. If list_ is a plain list, it will be. In general, if the list is mutated then the return value should point to the original entity. Args: list_: An entity that supports pop semantics. i: Optional index to pop from. May be None. opts: A ListPopOpts. Returns: Tuple (x, out_list_): out_list_: same as list_, after the removal was performed. x: the removed element value. Raises: ValueError: if list_ is not of a known list-like type or the operation is not supported for that type.
797795ListStackOptstensorflow/tensorflow/python/autograph/operators/data_structures.py299class
798796list_stacktensorflow/tensorflow/python/autograph/operators/data_structures.py305functionThe list stack function. This does not have a direct correspondent in Python. The closest idiom to this is tf.append or np.stack. It's different from those in the sense that it accepts a Tensor list, rather than a list of tensors. It can also accept TensorArray. When the target is anything else, the dispatcher will rely on ctx.original_call for fallback. Args: list_: An entity that supports append semantics. opts: A ListStackOpts object. Returns: The output of the stack operation, typically a Tensor.
799797DispatchContexttensorflow/tensorflow/python/autograph/operators/dispatch_context.py27classAllows passing additional parameters to the specific implementations. Attributes: options: Optional dict of extra arguments that may be required by specific implementations.
800798optiontensorflow/tensorflow/python/autograph/operators/dispatch_context.py37method
801799assert_stmttensorflow/tensorflow/python/autograph/operators/exceptions.py26functionFunctional form of an assert statement. This follows the semantics of the Python assert statement, however the concrete implementations may deviate from it. See the respective implementation for details. In general, the assert statement should not be used for control flow. Furthermore, it is encouraged that the assertion expressions should not have side effects. Args: expression1: Any expression2: Callable[[], Any], returns the expression to include in the error message when expression1 evaluates to False. When expression1 is True, the result of expression2 will not be evaluated, however, expression2 itself may be evaluated in some implementations. Returns: Any, implementation-dependent. Raises: ValueError: if any arguments are illegal.
802800not_tensorflow/tensorflow/python/autograph/operators/logical.py26functionFunctional form of "not".
803801and_tensorflow/tensorflow/python/autograph/operators/logical.py43functionFunctional form of "and". Uses lazy evaluation semantics.
804802or_tensorflow/tensorflow/python/autograph/operators/logical.py62functionFunctional form of "or". Uses lazy evaluation semantics.
805803eqtensorflow/tensorflow/python/autograph/operators/logical.py81functionFunctional form of "equal".
806804not_eqtensorflow/tensorflow/python/autograph/operators/logical.py98functionFunctional form of "not-equal".
807805overload_oftensorflow/tensorflow/python/autograph/operators/py_builtins.py65function
808806locals_in_original_contexttensorflow/tensorflow/python/autograph/operators/py_builtins.py92functionExecutes the locals function in the context of a specified function.
809807globals_in_original_contexttensorflow/tensorflow/python/autograph/operators/py_builtins.py97functionExecutes the locals function in the context of a specified function.
810808eval_in_original_contexttensorflow/tensorflow/python/autograph/operators/py_builtins.py102functionExecutes the eval function in the context of a specified function.
811809super_in_original_contexttensorflow/tensorflow/python/autograph/operators/py_builtins.py117functionExecutes the super function in the context of a specified function. See https://docs.python.org/3/library/functions.html#super for the exact details Args: f: Callable, typically the super builtin args: List[Any], the original call arguments caller_fn_scope: Optional[function_wrappers.FunctionScope], the function scope of the converted function in which this call was originally made Returns: The result of calling `f` as if it was called in the frame indicated by `caller_fn_scope`.
812810abs_tensorflow/tensorflow/python/autograph/operators/py_builtins.py179function
813811float_tensorflow/tensorflow/python/autograph/operators/py_builtins.py202function
814812int_tensorflow/tensorflow/python/autograph/operators/py_builtins.py219function
815813len_tensorflow/tensorflow/python/autograph/operators/py_builtins.py241function
816814print_tensorflow/tensorflow/python/autograph/operators/py_builtins.py318functionOverload of the print builtin.
817815range_tensorflow/tensorflow/python/autograph/operators/py_builtins.py360function
818816enumerate_tensorflow/tensorflow/python/autograph/operators/py_builtins.py391function
819817zip_tensorflow/tensorflow/python/autograph/operators/py_builtins.py409function
820818map_tensorflow/tensorflow/python/autograph/operators/py_builtins.py423function
821819next_tensorflow/tensorflow/python/autograph/operators/py_builtins.py437function
822820next_tf_iteratortensorflow/tensorflow/python/autograph/operators/py_builtins.py509function
823821next_pytensorflow/tensorflow/python/autograph/operators/py_builtins.py521function
824822filter_tensorflow/tensorflow/python/autograph/operators/py_builtins.py527function
825823any_tensorflow/tensorflow/python/autograph/operators/py_builtins.py541function
826824all_tensorflow/tensorflow/python/autograph/operators/py_builtins.py570function
827825sorted_tensorflow/tensorflow/python/autograph/operators/py_builtins.py596function
828826GetItemOptstensorflow/tensorflow/python/autograph/operators/slices.py34class
829827get_itemtensorflow/tensorflow/python/autograph/operators/slices.py38functionThe slice read operator (i.e. __getitem__). Note: it is unspecified whether target will be mutated or not. In general, if target is mutable (like Python lists), it will be mutated. Args: target: An entity that supports getitem semantics. i: Index to read from. opts: A GetItemOpts object. Returns: The read element. Raises: ValueError: if target is not of a supported type.
830828set_itemtensorflow/tensorflow/python/autograph/operators/slices.py100functionThe slice write operator (i.e. __setitem__). Note: it is unspecified whether target will be mutated or not. In general, if target is mutable (like Python lists), it will be mutated. Args: target: An entity that supports setitem semantics. i: Index to modify. x: The new element value. Returns: Same as target, after the update was performed. Raises: ValueError: if target is not of a supported type.
831829ldtensorflow/tensorflow/python/autograph/operators/variables.py22functionLoad variable operator.
832830ldutensorflow/tensorflow/python/autograph/operators/variables.py29functionLoad variable operator that returns Undefined when failing to evaluate. Note: the name ("load or return undefined") is abbreviated to minimize the amount of clutter in generated code. This variant of `ld` is useful when loading symbols that may be undefined at runtime, such as composite symbols, and whether they are defined or not cannot be determined statically. For example `d['a']` is undefined when `d` is an empty dict. Args: load_v: Lambda that executes the actual read. name: Human-readable name of the symbol being read. Returns: Either the value of the symbol, or Undefined, if the symbol is not fully defined.
833831Undefinedtensorflow/tensorflow/python/autograph/operators/variables.py54classRepresents an undefined symbol in Python. This is used to reify undefined symbols, which is required to use the functional form of loops. Example: while n > 0: n = n - 1 s = n return s # Runtime error if n == 0 This is valid Python code and will not result in an error as long as n is positive. The use of this class is to stay as close to Python semantics as possible for staged code of this nature. Converted version of the above showing the possible usage of this class: s = Undefined('s') init_state = (s,) s = while_loop(cond, body, init_state) return s # s is an instance of Undefined if the loop never runs Attributes: symbol_name: Text, identifier for the undefined symbol
834832readtensorflow/tensorflow/python/autograph/operators/variables.py86method
835833UndefinedReturnValuetensorflow/tensorflow/python/autograph/operators/variables.py106classRepresents a return value that is undefined.
836834NoValuetensorflow/tensorflow/python/autograph/pyct/anno.py37class
837835Basictensorflow/tensorflow/python/autograph/pyct/anno.py43classContainer for basic annotation keys. The enum values are used strictly for documentation purposes.
838836Statictensorflow/tensorflow/python/autograph/pyct/anno.py67classContainer for static analysis annotation keys. The enum values are used strictly for documentation purposes.
839837keystensorflow/tensorflow/python/autograph/pyct/anno.py110function
840838getannotensorflow/tensorflow/python/autograph/pyct/anno.py116function
841839hasannotensorflow/tensorflow/python/autograph/pyct/anno.py123function
842840setannotensorflow/tensorflow/python/autograph/pyct/anno.py127function
843841delannotensorflow/tensorflow/python/autograph/pyct/anno.py137function
844842copyannotensorflow/tensorflow/python/autograph/pyct/anno.py145function
845843duptensorflow/tensorflow/python/autograph/pyct/anno.py154functionRecursively copies annotations in an AST tree. Args: node: ast.AST copy_map: Dict[Hashable, Hashable], maps a source anno key to a destination key. All annotations with the source key will be copied to identical annotations with the destination key. field_name: str
846844CleanCopiertensorflow/tensorflow/python/autograph/pyct/ast_util.py30classNodeTransformer-like visitor that copies an AST.
847845copytensorflow/tensorflow/python/autograph/pyct/ast_util.py37methodReturns a deep copy of node (excluding some fields, see copy_clean).
848846copy_cleantensorflow/tensorflow/python/autograph/pyct/ast_util.py63functionCreates a deep copy of an AST. The copy will not include fields that are prefixed by '__', with the exception of user-specified annotations. Args: node: ast.AST preserve_annos: Optional[Set[Hashable]], annotation keys to include in the copy Returns: ast.AST
849847SymbolRenamertensorflow/tensorflow/python/autograph/pyct/ast_util.py79classTransformer that can rename symbols to a simple names.
850848visit_Nonlocaltensorflow/tensorflow/python/autograph/pyct/ast_util.py106method
851849visit_Globaltensorflow/tensorflow/python/autograph/pyct/ast_util.py110method
852850visit_Nametensorflow/tensorflow/python/autograph/pyct/ast_util.py114method
853851visit_Attributetensorflow/tensorflow/python/autograph/pyct/ast_util.py117method
854852visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/ast_util.py123method
855853rename_symbolstensorflow/tensorflow/python/autograph/pyct/ast_util.py130functionRenames symbols in an AST. Requires qual_names annotations.
856854keywords_to_dicttensorflow/tensorflow/python/autograph/pyct/ast_util.py140functionConverts a list of ast.keyword objects to a dict.
857855PatternMatchertensorflow/tensorflow/python/autograph/pyct/ast_util.py150classMatches a node against a pattern represented by a node.
858856compare_and_visittensorflow/tensorflow/python/autograph/pyct/ast_util.py158method
859857no_matchtensorflow/tensorflow/python/autograph/pyct/ast_util.py164method
860858is_wildcardtensorflow/tensorflow/python/autograph/pyct/ast_util.py168method
861859generic_visittensorflow/tensorflow/python/autograph/pyct/ast_util.py177method
862860matchestensorflow/tensorflow/python/autograph/pyct/ast_util.py214functionBasic pattern matcher for AST. The pattern may contain wildcards represented by the symbol '_'. A node matches a pattern if for every node in the tree, either there is a node of the same type in pattern, or a Name node with id='_'. Args: node: ast.AST pattern: ast.AST Returns: bool
863861apply_to_single_assignmentstensorflow/tensorflow/python/autograph/pyct/ast_util.py236functionApplies a function to each individual assignment. This function can process a possibly-unpacked (e.g. a, b = c, d) assignment. It tries to break down the unpacking if possible. In effect, it has the same effect as passing the assigned values in SSA form to apply_fn. Examples: The following will result in apply_fn(a, c), apply_fn(b, d): a, b = c, d The following will result in apply_fn(a, c[0]), apply_fn(b, c[1]): a, b = c The following will result in apply_fn(a, (b, c)): a = b, c It uses the visitor pattern to allow subclasses to process single assignments individually. Args: targets: Union[List[ast.AST, ...], Tuple[ast.AST, ...], ast.AST, should be used with the targets field of an ast.Assign node values: ast.AST apply_fn: Callable[[ast.AST, ast.AST], None], called with the respective nodes of each single assignment
864862parallel_walktensorflow/tensorflow/python/autograph/pyct/ast_util.py283functionWalks two ASTs in parallel. The two trees must have identical structure. Args: node: Union[ast.AST, Iterable[ast.AST]] other: Union[ast.AST, Iterable[ast.AST]] Yields: Tuple[ast.AST, ast.AST] Raises: ValueError: if the two trees don't have identical structure.
865863CodeObjectCachetensorflow/tensorflow/python/autograph/pyct/cache.py63classA function cache based on code objects. Code objects are good proxies for the source code of a function. This cache efficiently handles functions that share code objects, such as functions defined in a loop, bound methods, etc. The cache falls back to the function object, if it doesn't have a code object.
866864UnboundInstanceCachetensorflow/tensorflow/python/autograph/pyct/cache.py81classA function cache based on unbound function objects. Using the function for the cache key allows efficient handling of object methods. Unlike the _CodeObjectCache, this discriminates between different functions even if they have the same code. This is needed for decorators that may masquerade as another function.
867865Nodetensorflow/tensorflow/python/autograph/pyct/cfg.py54classA node in the CFG. Although new instances of this class are mutable, the objects that a user finds in the CFG are typically not. The nodes represent edges in the CFG graph, and maintain pointers to allow efficient walking in both forward and reverse order. The following property holds for all nodes: "child in node.next" iff "node in child.prev". Attributes: next: FrozenSet[Node, ...], the nodes that follow this node, in control flow order prev: FrozenSet[Node, ...], the nodes that precede this node, in reverse control flow order ast_node: ast.AST, the AST node corresponding to this CFG node
868866freezetensorflow/tensorflow/python/autograph/pyct/cfg.py77method
869867Graphtensorflow/tensorflow/python/autograph/pyct/cfg.py95classA Control Flow Graph. The CFG maintains an index to allow looking up a CFG node by the AST node to which it is associated. The index can also be enumerated in top-down, depth first order. Walking the graph in forward or reverse order is supported by double parent-child links. Note: the error nodes are not wired to their corresponding finally guards, because these are shared, and wiring them would create a reverse path from normal control flow into the error nodes, which we want to avoid. The graph also maintains edges corresponding to higher level statements like for-else loops. A node is considered successor of a statement if there is an edge from a node that is lexically a child of that statement to a node that is not. Statement predecessors are analogously defined. Attributes: entry: Node, the entry node exit: FrozenSet[Node, ...], the exit nodes error: FrozenSet[Node, ...], nodes that exit due to an explicitly raised error (errors propagated from function calls are not accounted) index: Dict[ast.Node, Node], mapping AST nodes to the respective CFG node stmt_prev: Dict[ast.Node, FrozenSet[Node, ...]], mapping statement AST nodes to their predecessor CFG nodes stmt_next: Dict[ast.Node, FrozenSet[Node, ...]], mapping statement AST nodes to their successor CFG nodes
870868as_dottensorflow/tensorflow/python/autograph/pyct/cfg.py133methodPrint CFG in DOT format.
871869GraphVisitortensorflow/tensorflow/python/autograph/pyct/cfg.py152classBase class for a CFG visitors. This implementation is not thread safe. The visitor has some facilities to simplify dataflow analyses. In particular, it allows revisiting the nodes at the decision of the subclass. This can be used to visit the graph until the state reaches a fixed point. For more details on dataflow analysis, see https://www.seas.harvard.edu/courses/cs252/2011sp/slides/Lec02-Dataflow.pdf Note: the literature generally suggests visiting successor nodes only when the state of the current node changed, regardless of whether that successor has ever been visited. This implementation visits every successor at least once. Attributes: graph: Graph in_: Dict[Node, Any], stores node-keyed state during a visit out: Dict[Node, Any], stores node-keyed state during a visit
872870init_statetensorflow/tensorflow/python/autograph/pyct/cfg.py178methodState initialization function. Optional to overload. An in/out state slot will be created for each node in the graph. Subclasses must overload this to control what that is initialized to. Args: node: Node
873871visit_nodetensorflow/tensorflow/python/autograph/pyct/cfg.py190methodVisitor function. Args: node: Node Returns: bool, whether the node should be revisited; subclasses can visit every reachable node exactly once by always returning False
874872resettensorflow/tensorflow/python/autograph/pyct/cfg.py201method
875873can_ignoretensorflow/tensorflow/python/autograph/pyct/cfg.py209methodReturns True if the node can safely be assumed not to touch variables.
876874visit_forwardtensorflow/tensorflow/python/autograph/pyct/cfg.py245method
877875visit_reversetensorflow/tensorflow/python/autograph/pyct/cfg.py248method
878876GraphBuildertensorflow/tensorflow/python/autograph/pyct/cfg.py252classBuilder that constructs a CFG from a given AST. This GraphBuilder facilitates constructing the DAG that forms the CFG when nodes are supplied in lexical order (i.e., top-down, depth first). Under these conditions, it supports building patterns found in typical structured programs. This builder ignores the flow generated by exceptions, which are assumed to always be catastrophic and present purely for diagnostic purposes (e.g. to print debug information). Statements like raise and try/catch sections are allowed and will generate control flow edges, but ordinary statements are assumed not to raise exceptions. Finally sections are also correctly interleaved between break/continue/return nodes and their subsequent statements. Important concepts: * nodes - nodes refer refer to CFG nodes; AST nodes are qualified explicitly * leaf set - since the graph is constructed gradually, a leaf set maintains the CFG nodes that will precede the node that the builder expects to receive next; when an ordinary node is added, it is connected to the existing leaves and it in turn becomes the new leaf * jump nodes - nodes that should generate edges other than what ordinary nodes would; these correspond to break, continue and return statements * sections - logical delimiters for subgraphs that require special edges; there are various types of nodes, each admitting various types of jump nodes; sections are identified by their corresponding AST node
879877resettensorflow/tensorflow/python/autograph/pyct/cfg.py292methodResets the state of this factory.
880878begin_statementtensorflow/tensorflow/python/autograph/pyct/cfg.py372methodMarks the beginning of a statement. Args: stmt: Hashable, a key by which the statement can be identified in the CFG's stmt_prev and stmt_next attributes
881879end_statementtensorflow/tensorflow/python/autograph/pyct/cfg.py381methodMarks the end of a statement. Args: stmt: Hashable, a key by which the statement can be identified in the CFG's stmt_prev and stmt_next attributes; must match a key previously passed to begin_statement.
882880add_ordinary_nodetensorflow/tensorflow/python/autograph/pyct/cfg.py391methodGrows the graph by adding an ordinary CFG node. Ordinary nodes are followed by the next node, in lexical order, that is, they become the new leaf set. Args: ast_node: ast.AST Returns: Node
883881add_exit_nodetensorflow/tensorflow/python/autograph/pyct/cfg.py438methodGrows the graph by adding an exit node. This node becomes an exit for the current section. Args: ast_node: ast.AST section_id: Hashable, the node for which ast_node should be considered to be an exit node guards: Tuple[ast.AST, ...], the finally sections that guard ast_node Returns: Node
884882add_continue_nodetensorflow/tensorflow/python/autograph/pyct/cfg.py455methodGrows the graph by adding a reentry node. This node causes control flow to go back to the loop section's entry. Args: ast_node: ast.AST section_id: Hashable, the node for which ast_node should be considered to be an exit node guards: Tuple[ast.AST, ...], the finally sections that guard ast_node
885883connect_raise_nodetensorflow/tensorflow/python/autograph/pyct/cfg.py469methodAdds extra connection between a raise node and containing except guards. The node is a graph node, not an ast node. Args: node: Node except_guards: Tuple[ast.AST, ...], the except sections that guard node
886884enter_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py484methodEnters a regular section. Regular sections admit exit jumps, which end the section. Args: section_id: Hashable, the same node that will be used in calls to the ast_node arg passed to add_exit_node
887885exit_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py496methodExits a regular section.
888886enter_loop_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py505methodEnters a loop section. Loop sections define an entry node. The end of the section always flows back to the entry node. These admit continue jump nodes which also flow to the entry node. Args: section_id: Hashable, the same node that will be used in calls to the ast_node arg passed to add_continue_node entry_node: ast.AST, the entry node into the loop (e.g. the test node for while loops)
889887exit_loop_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py524methodExits a loop section.
890888enter_cond_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py539methodEnters a conditional section. Conditional sections define an entry node, and one or more branches. Args: section_id: Hashable, the same node that will be used in calls to the section_id arg passed to new_cond_branch
891889new_cond_branchtensorflow/tensorflow/python/autograph/pyct/cfg.py553methodBegins a new branch in a cond section.
892890exit_cond_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py567methodExits a conditional section.
893891enter_except_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py574methodEnters an except section.
894892enter_finally_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py579methodEnters a finally section.
895893exit_finally_sectiontensorflow/tensorflow/python/autograph/pyct/cfg.py589methodExits a finally section.
896894buildtensorflow/tensorflow/python/autograph/pyct/cfg.py599methodReturns the CFG accumulated so far and resets the builder. Returns: Graph
897895AstToCfgtensorflow/tensorflow/python/autograph/pyct/cfg.py647classConverts an AST to CFGs. A separate CFG will be constructed for each function.
898896visit_ClassDeftensorflow/tensorflow/python/autograph/pyct/cfg.py714method
899897visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/cfg.py767method
900898visit_Lambdatensorflow/tensorflow/python/autograph/pyct/cfg.py770method
901899visit_Returntensorflow/tensorflow/python/autograph/pyct/cfg.py773method
902900visit_Importtensorflow/tensorflow/python/autograph/pyct/cfg.py776method
903901visit_ImportFromtensorflow/tensorflow/python/autograph/pyct/cfg.py779method
904902visit_Exprtensorflow/tensorflow/python/autograph/pyct/cfg.py782method
905903visit_Assigntensorflow/tensorflow/python/autograph/pyct/cfg.py785method
906904visit_AnnAssigntensorflow/tensorflow/python/autograph/pyct/cfg.py788method
907905visit_AugAssigntensorflow/tensorflow/python/autograph/pyct/cfg.py791method
908906visit_Passtensorflow/tensorflow/python/autograph/pyct/cfg.py794method
909907visit_Globaltensorflow/tensorflow/python/autograph/pyct/cfg.py797method
910908visit_Nonlocaltensorflow/tensorflow/python/autograph/pyct/cfg.py800method
911909visit_Printtensorflow/tensorflow/python/autograph/pyct/cfg.py803method
912910visit_Raisetensorflow/tensorflow/python/autograph/pyct/cfg.py806method
913911visit_Asserttensorflow/tensorflow/python/autograph/pyct/cfg.py811method
914912visit_Deletetensorflow/tensorflow/python/autograph/pyct/cfg.py815method
915913visit_Iftensorflow/tensorflow/python/autograph/pyct/cfg.py818method
916914visit_Whiletensorflow/tensorflow/python/autograph/pyct/cfg.py840method
917915visit_Fortensorflow/tensorflow/python/autograph/pyct/cfg.py863method
918916visit_Breaktensorflow/tensorflow/python/autograph/pyct/cfg.py894method
919917visit_Continuetensorflow/tensorflow/python/autograph/pyct/cfg.py897method
920918visit_ExceptHandlertensorflow/tensorflow/python/autograph/pyct/cfg.py900method
921919visit_Trytensorflow/tensorflow/python/autograph/pyct/cfg.py914method
922920visit_Withtensorflow/tensorflow/python/autograph/pyct/cfg.py956method
923921buildtensorflow/tensorflow/python/autograph/pyct/cfg.py964function
924922CountingVisitortensorflow/tensorflow/python/autograph/pyct/cfg_test.py28class
925923init_statetensorflow/tensorflow/python/autograph/pyct/cfg_test.py34method
926924visit_nodetensorflow/tensorflow/python/autograph/pyct/cfg_test.py37method
927925FrameInfotensorflow/tensorflow/python/autograph/pyct/error_utils.py26class
928926MultilineMessageKeyErrortensorflow/tensorflow/python/autograph/pyct/error_utils.py141class
929927ErrorMetadataBasetensorflow/tensorflow/python/autograph/pyct/error_utils.py153classContainer objects attached to exceptions raised in user code. This metadata allows re-raising exceptions that occur in generated code, with a custom error message that includes a stack trace relative to user-readable code from which the generated code originated.
930928get_messagetensorflow/tensorflow/python/autograph/pyct/error_utils.py177methodReturns the message for the underlying exception.
931929create_exceptiontensorflow/tensorflow/python/autograph/pyct/error_utils.py211method
932930to_exceptiontensorflow/tensorflow/python/autograph/pyct/error_utils.py221method
933931PyCTErrortensorflow/tensorflow/python/autograph/pyct/errors.py22classBase class for all exceptions.
934932UnsupportedLanguageElementErrortensorflow/tensorflow/python/autograph/pyct/errors.py27classRaised for code patterns that AutoGraph does not support.
935933is_literaltensorflow/tensorflow/python/autograph/pyct/gast_util.py40functionTests whether node represents a Python literal.
936934islambdatensorflow/tensorflow/python/autograph/pyct/inspect_utils.py60function
937935isnamedtupletensorflow/tensorflow/python/autograph/pyct/inspect_utils.py68functionReturns True if the argument is a namedtuple-like.
938936isbuiltintensorflow/tensorflow/python/autograph/pyct/inspect_utils.py82functionReturns True if the argument is a built-in function.
939937isconstructortensorflow/tensorflow/python/autograph/pyct/inspect_utils.py96functionReturns True if the argument is an object constructor. In general, any object of type class is a constructor, with the exception of classes created using a callable metaclass. See below for why a callable metaclass is not a trivial combination: https://docs.python.org/2.7/reference/datamodel.html#customizing-class-creation Args: cls: Any Returns: Bool
940938getimmediatesourcetensorflow/tensorflow/python/autograph/pyct/inspect_utils.py143functionA variant of inspect.getsource that ignores the __wrapped__ property.
941939getnamespacetensorflow/tensorflow/python/autograph/pyct/inspect_utils.py151functionReturns the complete namespace of a function. Namespace is defined here as the mapping of all non-local variables to values. This includes the globals and the closure variables. Note that this captures the entire globals collection of the function, and may contain extra symbols that it does not actually use. Args: f: User defined function. Returns: A dict mapping symbol names to values.
942940getqualifiednametensorflow/tensorflow/python/autograph/pyct/inspect_utils.py177functionReturns the name by which a value can be referred to in a given namespace. If the object defines a parent module, the function attempts to use it to locate the object. This function will recurse inside modules, but it will not search objects for attributes. The recursion depth is controlled by max_depth. Args: namespace: Dict[str, Any], the namespace to search into. object_: Any, the value to search. max_depth: Optional[int], a limit to the recursion depth when searching inside modules. visited: Optional[Set[int]], ID of modules to avoid visiting. Returns: Union[str, None], the fully-qualified name that resolves to the value o, or None if it couldn't be found.
943941getdefiningclasstensorflow/tensorflow/python/autograph/pyct/inspect_utils.py250functionResolves the class (e.g. one of the superclasses) that defined a method.
944942getmethodclasstensorflow/tensorflow/python/autograph/pyct/inspect_utils.py265functionResolves a function's owner, e.g. a method's class. Note that this returns the object that the function was retrieved from, not necessarily the class where it was defined. This function relies on Python stack frame support in the interpreter, and has the same limitations that inspect.currentframe. Limitations. This function will only work correctly if the owned class is visible in the caller's global or local variables. Args: m: A user defined function Returns: The class that this function was retrieved from, or None if the function is not an object or class method, or the class that owns the object or method is not visible to m. Raises: ValueError: if the class could not be resolved for any unexpected reason.
945943getfutureimportstensorflow/tensorflow/python/autograph/pyct/inspect_utils.py339functionDetects what future imports are necessary to safely execute entity source. Args: entity: Any object Returns: A tuple of future strings
946944decoratortensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py37function
947945function_decoratortensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py41function
948946wrapping_decoratortensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py47function
949947free_functiontensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py85function
950948factorytensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py89function
951949free_factorytensorflow/tensorflow/python/autograph/pyct/inspect_utils_test.py93function
952950load_sourcetensorflow/tensorflow/python/autograph/pyct/loader.py50functionLoads the given source code as a Python module.
953951load_asttensorflow/tensorflow/python/autograph/pyct/loader.py70functionLoads the given AST as a Python module. Compiling the AST code this way ensures that the source code is readable by e.g. `pdb` or `inspect`. Args: nodes: Union[ast.AST, Iterable[ast.AST]], the code to compile, as an AST object. indentation: Text, the string to use for indentation. include_source_map: bool, whether return a source map. delete_on_exit: bool, whether to delete the temporary file used for compilation on exit. Returns: Tuple[module, Text, Dict[LineLocation, OriginInfo]], containing: the module containing the unparsed nodes, the source code corresponding to nodes, and the source map. Is include_source_map is False, the source map will be None.
954952load_sourcetensorflow/tensorflow/python/autograph/pyct/loader_deprecated_py2.py40functionLoads the given source code as a Python module.
955953load_asttensorflow/tensorflow/python/autograph/pyct/loader_deprecated_py2.py58functionLoads the given AST as a Python module. Compiling the AST code this way ensures that the source code is readable by e.g. `pdb` or `inspect`. Args: nodes: Union[ast.AST, Iterable[ast.AST]], the code to compile, as an AST object. indentation: Text, the string to use for indentation. include_source_map: bool, whether return a source map. delete_on_exit: bool, whether to delete the temporary file used for compilation on exit. Returns: Tuple[module, Text, Dict[LineLocation, OriginInfo]], containing: the module containing the unparsed nodes, the source code corresponding to nodes, and the source map. Is include_source_map is False, the source map will be None.
956954Namertensorflow/tensorflow/python/autograph/pyct/naming.py24classSymbol name generator.
957955new_symboltensorflow/tensorflow/python/autograph/pyct/naming.py31methodSee control_flow.SymbolNamer.new_symbol.
958956LineLocationtensorflow/tensorflow/python/autograph/pyct/origin_info.py35classSimilar to Location, but without column information. Attributes: filename: Text lineno: int, 1-based
959957Locationtensorflow/tensorflow/python/autograph/pyct/origin_info.py46classEncodes code location information. Attributes: filename: Text lineno: int, 1-based col_offset: int line_loc: LineLocation
960958line_loctensorflow/tensorflow/python/autograph/pyct/origin_info.py58method
961959OriginInfotensorflow/tensorflow/python/autograph/pyct/origin_info.py62classContainer for information about the source code before conversion. Attributes: loc: Location function_name: Optional[Text] source_code_line: Text comment: Optional[Text]
962960as_frametensorflow/tensorflow/python/autograph/pyct/origin_info.py75methodReturns a 4-tuple consistent with the return of traceback.extract_tb.
963961create_source_maptensorflow/tensorflow/python/autograph/pyct/origin_info.py89functionCreates a source map between an annotated AST and the code it compiles to. Note: this function assumes nodes nodes, code and filepath correspond to the same code. Args: nodes: Iterable[ast.AST, ...], one or more AST modes. code: Text, the source code in which nodes are found. filepath: Text Returns: Dict[LineLocation, OriginInfo], mapping locations in code to locations indicated by origin annotations in node.
964962OriginResolvertensorflow/tensorflow/python/autograph/pyct/origin_info.py166classAnnotates an AST with additional source information like file name.
965963visittensorflow/tensorflow/python/autograph/pyct/origin_info.py212method
966964resolvetensorflow/tensorflow/python/autograph/pyct/origin_info.py226functionAdds origin information to an AST, based on the source it was loaded from. This allows us to map the original source code line numbers to generated source code. Note: the AST may be a part of a larger context (e.g. a function is part of a module that may contain other things). However, this function does not assume the source argument contains the entire context, nor that it contains only code corresponding to node itself. However, it assumes that node was parsed from the given source code. For this reason, two extra arguments are required, and they indicate the location of the node in the original context. Args: node: gast.AST, the AST to annotate. source: Text, the source code representing node. context_filepath: Text context_lineno: int context_col_offset: int
967965resolve_entitytensorflow/tensorflow/python/autograph/pyct/origin_info.py271functionLike resolve, but extracts the context information from an entity.
968966dedent_blocktensorflow/tensorflow/python/autograph/pyct/parser.py65functionDedents a code so that its first line starts at row zero.
969967parse_entitytensorflow/tensorflow/python/autograph/pyct/parser.py133functionReturns the AST and source code of given entity. Args: entity: Any, Python function/method/class future_features: Iterable[Text], future features to use (e.g. 'print_statement'). See https://docs.python.org/2/reference/simple_stmts.html#future Returns: gast.AST, Text: the parsed AST node; the source code that was parsed to generate the AST (including any prefixes that this function may have added).
970968parsetensorflow/tensorflow/python/autograph/pyct/parser.py323functionReturns the AST of given piece of code. Args: src: Text preamble_len: Int, indicates leading nodes in the parsed AST which should be dropped. single_node: Bool, whether `src` is assumed to be represented by exactly one AST node. Returns: ast.AST
971969parse_expressiontensorflow/tensorflow/python/autograph/pyct/parser.py347functionReturns the AST of given identifier. Args: src: A piece of code that represents a single Python expression Returns: A gast.AST object. Raises: ValueError: if src does not consist of a single Expression.
972970unparsetensorflow/tensorflow/python/autograph/pyct/parser.py366functionReturns the source code of given AST. Args: node: The code to compile, as an AST object. indentation: Unused, deprecated. The returning code will always be indented at 4 spaces. include_encoding_marker: Bool, thether to include a comment on the first line to explicitly specify UTF-8 encoding. Returns: code: The source code generated from the AST object source_mapping: A mapping between the user and AutoGraph generated code.
973971PrettyPrintertensorflow/tensorflow/python/autograph/pyct/pretty_printer.py26classPrint AST nodes.
974972generic_visittensorflow/tensorflow/python/autograph/pyct/pretty_printer.py59method
975973fmttensorflow/tensorflow/python/autograph/pyct/pretty_printer.py128function
976974CallerMustSetThistensorflow/tensorflow/python/autograph/pyct/qual_names.py36class
977975Symboltensorflow/tensorflow/python/autograph/pyct/qual_names.py40classRepresents a Python symbol.
978976Literaltensorflow/tensorflow/python/autograph/pyct/qual_names.py44classRepresents a Python numeric literal.
979977QNtensorflow/tensorflow/python/autograph/pyct/qual_names.py57classRepresents a qualified name.
980978is_symboltensorflow/tensorflow/python/autograph/pyct/qual_names.py95method
981979is_simpletensorflow/tensorflow/python/autograph/pyct/qual_names.py98method
982980is_compositetensorflow/tensorflow/python/autograph/pyct/qual_names.py101method
983981has_subscripttensorflow/tensorflow/python/autograph/pyct/qual_names.py104method
984982has_attrtensorflow/tensorflow/python/autograph/pyct/qual_names.py107method
985983parenttensorflow/tensorflow/python/autograph/pyct/qual_names.py111method
986984owner_settensorflow/tensorflow/python/autograph/pyct/qual_names.py117methodReturns all the symbols (simple or composite) that own this QN. In other words, if this symbol was modified, the symbols in the owner set may also be affected. Examples: 'a.b[c.d]' has two owners, 'a' and 'a.b'
987985support_settensorflow/tensorflow/python/autograph/pyct/qual_names.py133methodReturns the set of simple symbols that this QN relies on. This would be the smallest set of symbols necessary for the QN to statically resolve (assuming properties and index ranges are verified at runtime). Examples: 'a.b' has only one support symbol, 'a' 'a[i]' has two support symbols, 'a' and 'i'
988986ssftensorflow/tensorflow/python/autograph/pyct/qual_names.py175methodSimple symbol form.
989987asttensorflow/tensorflow/python/autograph/pyct/qual_names.py187methodAST representation.
990988QnResolvertensorflow/tensorflow/python/autograph/pyct/qual_names.py210classAnnotates nodes with QN information. Note: Not using NodeAnnos to avoid circular dependencies.
991989visit_Nametensorflow/tensorflow/python/autograph/pyct/qual_names.py216method
992990visit_Attributetensorflow/tensorflow/python/autograph/pyct/qual_names.py221method
993991visit_Subscripttensorflow/tensorflow/python/autograph/pyct/qual_names.py228method
994992resolvetensorflow/tensorflow/python/autograph/pyct/qual_names.py251function
995993from_strtensorflow/tensorflow/python/autograph/pyct/qual_names.py255function
996994ContextAdjustertensorflow/tensorflow/python/autograph/pyct/templates.py35classAdjusts the ctx field of nodes to ensure consistency. This transformer can change the ctx fields of a variable, tuple and other AST elements that allow one, based on whether the element is being read or written.
997995visittensorflow/tensorflow/python/autograph/pyct/templates.py46method
998996visit_Attributetensorflow/tensorflow/python/autograph/pyct/templates.py58method
999997visit_Tupletensorflow/tensorflow/python/autograph/pyct/templates.py64method
1000998visit_Listtensorflow/tensorflow/python/autograph/pyct/templates.py68method
1001999visit_Nametensorflow/tensorflow/python/autograph/pyct/templates.py72method
10021000visit_Calltensorflow/tensorflow/python/autograph/pyct/templates.py76method
10031001visit_Dicttensorflow/tensorflow/python/autograph/pyct/templates.py83method
10041002visit_Subscripttensorflow/tensorflow/python/autograph/pyct/templates.py89method
10051003visit_comprehensiontensorflow/tensorflow/python/autograph/pyct/templates.py95method
10061004visit_Lambdatensorflow/tensorflow/python/autograph/pyct/templates.py101method
10071005ReplaceTransformertensorflow/tensorflow/python/autograph/pyct/templates.py108classReplace AST nodes.
10081006visit_Exprtensorflow/tensorflow/python/autograph/pyct/templates.py146method
10091007visit_keywordtensorflow/tensorflow/python/autograph/pyct/templates.py154method
10101008visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/templates.py172method
10111009visit_Attributetensorflow/tensorflow/python/autograph/pyct/templates.py185method
10121010visit_Nametensorflow/tensorflow/python/autograph/pyct/templates.py197method
10131011replacetensorflow/tensorflow/python/autograph/pyct/templates.py234functionReplaces placeholders in a Python template. AST Name and Tuple nodes always receive the context that inferred from the template. However, when replacing more complex nodes (that can potentially contain Name children), then the caller is responsible for setting the appropriate context. Args: template: A string representing Python code. Any symbol name can be used that appears in the template code can be used as placeholder. **replacements: A mapping from placeholder names to (lists of) AST nodes that these placeholders will be replaced by. String values are also supported as a shorthand for AST Name nodes with the respective ID. Returns: An AST node or list of AST nodes with the replacements made. If the template was a function, a list will be returned. If the template was a node, the same node will be returned. If the template was a string, an AST node will be returned (a `Module` node in the case of a multi-line string, an `Expr` node otherwise). Raises: ValueError: if the arguments are incorrect.
10141012replace_as_expressiontensorflow/tensorflow/python/autograph/pyct/templates.py279functionVariant of replace that generates expressions, instead of code blocks.
10151013AnalysisLeveltensorflow/tensorflow/python/autograph/pyct/transformer.py32class
10161014Contexttensorflow/tensorflow/python/autograph/pyct/transformer.py41classContains information about a source code transformation. This object is mutable, and is updated during conversion. Not thread safe. Attributes: info: EntityInfo, immutable. namer: naming.Namer. current_origin: origin_info.OriginInfo, holds the OriginInfo of the last AST node to be processed successfully. Useful for error handling. user: An user-supplied context object. The object is opaque to the infrastructure, but will pe passed through to all custom transformations.
10171015EntityInfotensorflow/tensorflow/python/autograph/pyct/transformer.py63classContains information about a Python entity. Immutable. Examples of entities include functions and classes. Attributes: name: The name that identifies this entity. source_code: The entity's source code. source_file: The entity's source file. future_features: Tuple[Text], the future features that this entity was compiled with. See https://docs.python.org/2/reference/simple_stmts.html#future. namespace: Dict[str, ], containing symbols visible to the entity (excluding parameters).
10181016NodeStateTrackertensorflow/tensorflow/python/autograph/pyct/transformer.py200classBase class for general-purpose Python code transformation. This abstract class provides helpful functions, like state tracking within the scope of arbitrary node, helpers for processing code blocks, debugging, mapping of transformed code to original code, and others. Scope-local state tracking: to keep state across nodes, at the level of (possibly nested) scopes, use enter/exit_local_scope and set/get_local. You must call enter/exit_local_scope manually, but the transformer detects when they are not properly paired. The transformer allows keeping state across calls that is local to arbitrary nodes and their descendants, using the self.state attribute. Multiple independent scopes are allowed and automatically constructed. For example, to keep track of the `If` node that encloses any `Name` node, one can write: ``` class FooType(object): def __init__(self): self.foo_property = None class DummyTransformer(NodeStateTracker, ast.NodeTransformer): def visit_If(self, node): self.state[FooType].enter() self.state[FooType].foo_property = node node = self.veneric_visit(node) self.state[FooType].exit() return node def visit_Name(self, node): self.state[FooType].foo_property # will hold the innermost enclosing if ``` Alternatively, the `enter()`/`exit()` calls can be managed by a `with` statement: ``` def visit_If(self, node): with self.state[FooType] as foo: foo.foo_property = node return self.generic_visit(node) ```
10191017debug_printtensorflow/tensorflow/python/autograph/pyct/transformer.py270methodHelper method useful for debugging. Prints the AST.
10201018debug_print_srctensorflow/tensorflow/python/autograph/pyct/transformer.py276methodHelper method useful for debugging. Prints the AST as code.
10211019visit_blocktensorflow/tensorflow/python/autograph/pyct/transformer.py282methodA more powerful version of generic_visit for statement blocks. An example of a block is the body of an if statement. This function allows specifying a postprocessing callback (the after_visit argument) argument which can be used to move nodes to a new destination. This is done by after_visit by returning a non-null second return value, e.g. return new_node, new_destination. For example, a transformer could perform the following move: foo() bar() baz() foo() if cond: bar() baz() The above could be done with a postprocessor of this kind: def after_visit(node): if node_is_function_call(bar): new_container_node = build_cond() new_container_node.body.append(node) return new_container_node, new_container_node.body else: # Once we set a new destination, all subsequent items will be # moved to it, so we don't need to explicitly handle baz. return node, None Args: nodes: enumerable of AST node objects. If None, the function returns None. before_visit: optional callable that is called before visiting each item in nodes after_visit: optional callable that takes in an AST node and returns a tuple (new_node, new_destination). It is called after visiting each item in nodes. Is used in the same was as the visit_* methods: new_node will replace the node; if not None, new_destination must be a list, and subsequent nodes will be placed in this list instead of the list returned by visit_block. Returns: A list of AST node objects containing the transformed items fron nodes, except those nodes that have been relocated using after_visit.
10221020Basetensorflow/tensorflow/python/autograph/pyct/transformer.py360classBase class for general-purpose Python-to-Python code transformation. This is an extension of ast.NodeTransformer that provides the additional functions offered by NodeStateTracker.
10231021create_assignmenttensorflow/tensorflow/python/autograph/pyct/transformer.py367method
10241022apply_to_single_assignmentstensorflow/tensorflow/python/autograph/pyct/transformer.py374methodApplies a function to each individual assignment. This function can process a possibly-unpacked (e.g. a, b = c, d) assignment. It tries to break down the unpacking if possible. In effect, it has the same effect as passing the assigned values in SSA form to apply_fn. Examples: The following will result in apply_fn(a, c), apply_fn(b, d): a, b = c, d The following will result in apply_fn(a, c[0]), apply_fn(b, c[1]): a, b = c The following will result in apply_fn(a, (b, c)): a = b, c It uses the visitor pattern to allow subclasses to process single assignments individually. Args: targets: list, tuple of or individual AST node. Should be used with the targets field of an ast.Assign node. values: an AST node. apply_fn: a function of a single argument, which will be called with the respective nodes of each single assignment. The signature is apply_fn(target, value), no return value.
10251023visittensorflow/tensorflow/python/autograph/pyct/transformer.py421method
10261024CodeGeneratortensorflow/tensorflow/python/autograph/pyct/transformer.py478classBase class for general-purpose Python-to-string code transformation. Similar to Base, but outputs arbitrary strings instead of a Python AST. This uses the same visitor mechanism that the standard NodeVisitor uses, meaning that subclasses write handlers for the different kinds of nodes. New code is generated using the emit method, which appends to a code buffer that can be afterwards obtained from code_buffer. Example: class SimpleCodeGen(CodeGenerator): def visitIf(self, node): self.emit('if ') self.visit(node.test) self.emit(' { ') self.visit(node.body) self.emit(' } else { ') self.visit(node.orelse) self.emit(' } ') node = ast.parse(...) gen = SimpleCodeGen() gen.visit(node) # gen.code_buffer contains the resulting code
10271025emittensorflow/tensorflow/python/autograph/pyct/transformer.py513method
10281026code_buffertensorflow/tensorflow/python/autograph/pyct/transformer.py517method
10291027visittensorflow/tensorflow/python/autograph/pyct/transformer.py520method
10301028GenericTranspilertensorflow/tensorflow/python/autograph/pyct/transpiler.py227classA generic transpiler for Python functions. Its interface is the `transform` API, which can process Python function objects. Internally, it handles parsing. Users typically subclass this, customizing the `transform_ast` method. The output of transformed_ast is returned directly by `transform`. Existing methods like `transform_function` may also be overloaded. Example: class MyTransformer(GenericTranspiler): def transform_ast(self, node, ctx): result = <<transform node>> return result transformer = MyTransfomer() result = transformer.transform(f, ...) # result is the output
10311029get_transformed_nametensorflow/tensorflow/python/autograph/pyct/transpiler.py251methodReturns a name for the output function. Subclasses may override this.
10321030transform_asttensorflow/tensorflow/python/autograph/pyct/transpiler.py259methodPerforms an actual transformation of a function's AST. Subclasses must implement this method, and do not usually call it. Args: node: One or more ast.AST nodes representing the AST to be transformed. ctx: transformer.Context.
10331031transformtensorflow/tensorflow/python/autograph/pyct/transpiler.py270methodTransforms a Python object. Users typically call this method. Args: obj: A Python object, function, type, etc. user_context: An opaque object (may be None) that is forwarded to transform_ast, through the ctx.user_context argument. Returns: Tre result of calling transform_function. Raises: NotImplementedError: if the type of obj is not handled.
10341032transform_moduletensorflow/tensorflow/python/autograph/pyct/transpiler.py300methodTransforms a module. Subclasses may override this method. The return value is opaque. The method receives the original AST. The result is passed as-is to the output of `transform`. Args: mod: A Python module. user_context: An opaque object (may be None) that is forwarded to transform_ast, through the ctx.user_context argument. Returns: List[Tuple[Any, Any]]. By default it returns the output of transform_ast, evaluated on each supported member, other than modules, together with a `transformer.Context` containing information about the transformation process.
10351033transform_functiontensorflow/tensorflow/python/autograph/pyct/transpiler.py328methodTransforms a function. Subclasses may override this method. The return value is opaque. The method receives the original AST. The result is passed as-is to the output of `transform`. Args: fn: A function or lambda. user_context: An opaque object (may be None) that is forwarded to transform_ast, through the ctx.user_context argument. Returns: Tuple[Any, Any]. By default it returns the output of transform_ast, together with a `transformer.Context` containing information about the transformation process.
10361034PyToPytensorflow/tensorflow/python/autograph/pyct/transpiler.py368classA generic Python-to-Python transpiler. Its `transform` method offers a function-in, function-out interface. Internally, it takes care of parsing, caching and loading of the translated code. Users typically subclass this, overriding `transform_ast`. Usually, instances of this class are singletons, since each instance manages its own cache. The caching can be controlled by overriding `get_caching_key`. Example: class MyTransformer(PyToPy): def transform_ast(self, node, ctx): node = <<transform node, usually using ast.NodeTransformer classes>> return node transformer = MyTransfomer() new_f, module, source_map = transformer.transform_function(f, ...) # new_f is a function with signature identical to f The transformed function has access to the same namespace as the original function. To allow access to internal APIs, users may inject additional symbols by overriding `get_extra_locals`.
10371035get_extra_localstensorflow/tensorflow/python/autograph/pyct/transpiler.py402methodReturns extra static local variables to be made to transformed code. Subclasses must override this. Returns: extra_locals: A Dict[Text, Any] containing additional variables to make available to the transformed code.
10381036get_caching_keytensorflow/tensorflow/python/autograph/pyct/transpiler.py413methodReturns a unique key to use for caching. Subclasses must override this. Calls made to `transform_function` with functions that have the same code object and caching key will return a cached instance on subsequent invocations. Args: user_context: The context object which was passed to `transform`. Returns: extra_locals: A hashable.
10391037transform_functiontensorflow/tensorflow/python/autograph/pyct/transpiler.py436methodTransforms a function. See GenericTranspiler.trasnform_function. This overload wraps the parent's `transform_function`, adding caching and facilities to instantiate the output as a Python object. It also adds facilities to make new symbols available to the generated Python code, visible as local variables - see `get_extra_locals`. Args: fn: A function or lambda. user_context: An opaque object (may be None) that is forwarded to transform_ast, through the ctx.user_context argument. Returns: A tuple: * A function or lambda with the same signature and closure as `fn` * The temporary module into which the transformed function was loaded * The source map as a Dict[origin_info.LineLocation, origin_info.OriginInfo]
10401038FlipSignTransformertensorflow/tensorflow/python/autograph/pyct/transpiler_test.py30class
10411039visit_BinOptensorflow/tensorflow/python/autograph/pyct/transpiler_test.py32method
10421040DummyGensymtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py40classA dumb gensym that suffixes a stem by sequential numbers from 1000.
10431041new_nametensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py50method
10441042ASTEdgePatterntensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py60classA pattern defining a type of AST edge. This consists of three components: - The type of the parent node, checked with isinstance, - The name of the field, checked with string equality, and - The type of the child node, also checked with isinstance. If all three match, the whole pattern is considered to match. In all three slots, the special value `anf.ANY` is treated as "match anything". The internal nodes are produced from the `gast` library rather than the standard `ast` module, which may affect `isinstance` checks.
10451043matchestensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py76methodComputes whether this pattern matches the given edge.
10461044AnfTransformertensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py89classPerforms the conversion to A-normal form (ANF).
10471045visit_Returntensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py256method
10481046visit_Deletetensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py259method
10491047visit_Assigntensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py262method
10501048visit_AugAssigntensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py265method
10511049visit_Printtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py268method
10521050visit_Fortensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py271method
10531051visit_AsyncFortensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py290method
10541052visit_Whiletensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py295method
10551053visit_Iftensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py307method
10561054visit_Withtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py324method
10571055visit_AsyncWithtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py343method
10581056visit_Raisetensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py348method
10591057visit_Asserttensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py353method
10601058visit_Exectensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py361method
10611059visit_Exprtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py366method
10621060visit_BoolOptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py371method
10631061visit_BinOptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py376method
10641062visit_UnaryOptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py379method
10651063visit_Lambdatensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py382method
10661064visit_IfExptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py387method
10671065visit_Dicttensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py393method
10681066visit_Settensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py396method
10691067visit_ListComptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py399method
10701068visit_SetComptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py405method
10711069visit_DictComptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py411method
10721070visit_GeneratorExptensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py417method
10731071visit_Awaittensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py423method
10741072visit_Yieldtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py428method
10751073visit_YieldFromtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py431method
10761074visit_Comparetensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py436method
10771075visit_Calltensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py443method
10781076visit_Reprtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py446method
10791077visit_FormattedValuetensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py451method
10801078visit_JoinedStrtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py456method
10811079visit_Attributetensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py461method
10821080visit_Subscripttensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py464method
10831081visit_Listtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py470method
10841082visit_Tupletensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py476method
10851083transformtensorflow/tensorflow/python/autograph/pyct/common_transformers/anf.py527functionConverts the given node to A-normal form (ANF). The general idea of A-normal form: https://en.wikipedia.org/wiki/A-normal_form The specific converters used here are based on Python AST semantics as documented at https://greentreesnakes.readthedocs.io/en/latest/. What exactly should be considered A-normal form for any given programming language is not completely obvious. The transformation defined here is therefore configurable as to which syntax to replace with a fresh variable and which to leave be. The configuration is intentionally flexible enough to define very precise variable insertion transformations, should that be desired. The configuration is a list of syntax rules, each of which is a 2-tuple: - An `ASTEdgePattern` (which see) defining a type of AST edge, and - Whether to transform children of such edges. The special object `anf.ANY` may be used as a pattern that matches all edges. Each replacement directive is one of three possible things: - The object `anf.REPLACE`, meaning "Replace this child node with a variable", - The object `anf.LEAVE`, meaning "Do not replace this child node with a variable", or - A Python callable. If a callable, it is called with the parent node, the field name, and the child node, and must compute a boolean indicating whether to transform the child node or not. The callable is free to use whatever context information it chooses. The callable may be invoked more than once on the same link, and must produce the same answer each time. The syntax rules are tested in order, and the first match governs. If no rule matches, the node is not transformed. The above rules notwithstanding, - Variable references are never replaced with (fresh) variables, as that would accomplish nothing. - The left-hand children of Assign and AugAssign nodes, and the children of Del nodes, are never replaced with variables, as that would break their semantics. - The right-hand children of Assign nodes are never replaced with variables, as the original assignment would still have to be present in the result to define the new variable. (That is, there's no point in transforming `x = sin(y)` into `tmp = sin(y); x = tmp`.) - The right-hand children of AugAssign nodes are never replaced with variables either, but only because the difference from Assign was considered a potential source of confusion (and it would have been slightly awkward in the code to treat the RHS differently than the LHS). - Various special-purpose AST nodes are not exposed to the configuration, lest the transform produce invalid syntax like, e.g., `tmp = +; x = 1 tmp 2`. For example, the configuration ```python [(anf.ASTEdgePattern(anf.ANY, anf.ANY, gast.expr), anf.REPLACE)] ``` gives explicit fresh names to all expressions regardless of context (except as outlined above), whereas ```python [(anf.ASTEdgePattern(gast.If, "test", anf.ANY), anf.REPLACE)] ``` only transforms the conditionals of `if` statements (but not, e.g., `while`). If no configuration is supplied, the default behavior is to transform all expressions except literal constants, which is defined as a configuration as ```python # For Python 3, and gast library versions before 0.3 literals = (gast.Num, gast.Str, gast.Bytes, gast.NameConstant) [(anf.ASTEdgePattern(anf.ANY, anf.ANY, literals), anf.LEAVE), (anf.ASTEdgePattern(anf.ANY, anf.ANY, gast.expr), anf.REPLACE)] ``` Args: node: The node to transform. ctx: transformer.EntityInfo. TODO(mdan): What information does this argument provide? config: Optional ANF configuration. If omitted, ANF replaces all expression expect literal constants.
10861084exec_expected_resulttensorflow/tensorflow/python/autograph/pyct/common_transformers/anf_test.py40function
10871085Scopetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py36classEncloses local symbol definition and usage information. This can track for instance whether a symbol is modified in the current scope. Note that scopes do not necessarily align with Python's scopes. For example, the body of an if statement may be considered a separate scope. Caution - the AST references held by this object are weak. Scope objects are mutable during construction only, and must be frozen using `Scope.finalize()` before use. Furthermore, a scope is consistent only after all its children have been frozen. While analysing code blocks, scopes are being gradually built, from the innermost scope outward. Freezing indicates that the analysis of a code block is complete. Once frozen, mutation is no longer allowed. `is_final` tracks whether the scope is frozen or not. Certain properties, like `referenced`, are only accurate when called on frozen scopes. Attributes: parent: Optional[Scope], the parent scope, if any. isolated: bool, whether the scope is a true Python scope (e.g. the scope of a function), or just a surrogate tracking an ordinary code block. Using the terminology of the Python 3 reference documentation, True roughly represents an actual scope, whereas False represents an ordinary code block. function_name: Optional[str], name of the function owning this scope. isolated_names: Set[qual_names.QN], identifiers that are isolated to this scope (even if the scope is not isolated). annotations: Set[qual_names.QN], identifiers used as type annotations in this scope. read: Set[qual_names.QN], identifiers read in this scope. modified: Set[qual_names.QN], identifiers modified in this scope. deleted: Set[qual_names.QN], identifiers deleted in this scope. bound: Set[qual_names.QN], names that are bound to this scope. See https://docs.python.org/3/reference/executionmodel.html#binding-of-names for a precise definition. globals: Set[qual_names.QN], names that are explicitly marked as global in this scope. Note that this doesn't include free read-only vars bound to global symbols. nonlocals: Set[qual_names.QN], names that are explicitly marked as nonlocal in this scope. Note that this doesn't include free read-only vars bound to global symbols. free_vars: Set[qual_names.QN], the free variables in this scope. See https://docs.python.org/3/reference/executionmodel.html for a precise definition. params: WeakValueDictionary[qual_names.QN, ast.Node], function arguments visible in this scope, mapped to the function node that defines them. enclosing_scope: Scope, the innermost isolated scope that is a transitive parent of this scope. May be the scope itself. referenced: Set[qual_names.QN], the totality of the symbols used by this scope and its parents. is_final: bool, whether the scope is frozen or not. Note - simple statements may never delete and modify a symbol at the same time. However, compound ones like if statements can. In that latter case, it's undefined whether the symbol is actually modified or deleted upon statement exit. Certain analyses like reaching definitions need to be careful about this.
10881086enclosing_scopetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py130method
10891087referencedtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py137method
10901088free_varstensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py143method
10911089copy_fromtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py147methodRecursively copies the contents of this scope from another scope.
10921090copy_oftensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py162method
10931091merge_fromtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py172methodAdds all activity from another scope to this scope.
10941092finalizetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py185methodFreezes this scope.
10951093mark_paramtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py207method
10961094ActivityAnalyzertensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py230classAnnotates nodes with local scope information. See Scope. The use of this class requires that qual_names.resolve() has been called on the node. This class will ignore nodes have not been annotated with their qualified names.
10971095visit_Importtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py353method
10981096visit_ImportFromtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py356method
10991097visit_Globaltensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py359method
11001098visit_Nonlocaltensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py368method
11011099visit_Exprtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py378method
11021100visit_Raisetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py381method
11031101visit_Returntensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py384method
11041102visit_Assigntensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py387method
11051103visit_AnnAssigntensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py390method
11061104visit_AugAssigntensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py399method
11071105visit_Deletetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py413method
11081106visit_Nametensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py416method
11091107visit_aliastensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py422method
11101108visit_Attributetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py435method
11111109visit_Subscripttensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py443method
11121110visit_Printtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py450method
11131111visit_Asserttensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py457method
11141112visit_Calltensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py460method
11151113visit_comprehensiontensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py509method
11161114visit_DictComptensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py516method
11171115visit_ListComptensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py519method
11181116visit_SetComptensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py522method
11191117visit_GeneratorExptensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py525method
11201118visit_ClassDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py528method
11211119visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py568method
11221120visit_Lambdatensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py606method
11231121visit_Withtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py648method
11241122visit_withitemtensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py654method
11251123visit_Iftensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py657method
11261124visit_Fortensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py668method
11271125visit_Whiletensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py685method
11281126visit_ExceptHandlertensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py696method
11291127resolvetensorflow/tensorflow/python/autograph/pyct/static_analysis/activity.py707function
11301128NoValuetensorflow/tensorflow/python/autograph/pyct/static_analysis/annos.py27class
11311129NodeAnnotensorflow/tensorflow/python/autograph/pyct/static_analysis/annos.py33classAdditional annotations used by the static analyzer. These are in addition to the basic annotations declared in anno.py.
11321130Analyzertensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py40classCFG visitor that performs liveness analysis at statement level.
11331131init_statetensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py47method
11341132visit_nodetensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py50method
11351133TreeAnnotatortensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py96classRuns liveness analysis on each of the functions defined in the AST. If a function defined other local functions, those will have separate CFGs. However, dataflow analysis needs to tie up these CFGs to properly emulate the effect of closures. In the case of liveness, the parent function's live variables must account for the variables that are live at the entry of each subfunction. For example: def foo(): # baz is live from here on def bar(): print(baz) This analyzer runs liveness analysis on each individual function, accounting for the effect above.
11361134visittensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py121method
11371135visit_Lambdatensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py142method
11381136visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py145method
11391137visit_Iftensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py168method
11401138visit_Fortensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py173method
11411139visit_Whiletensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py178method
11421140visit_Trytensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py183method
11431141visit_ExceptHandlertensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py188method
11441142visit_Withtensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py193method
11451143visit_Exprtensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py197method
11461144resolvetensorflow/tensorflow/python/autograph/pyct/static_analysis/liveness.py206functionResolves the live symbols at the exit of control flow statements. Args: node: ast.AST source_info: transformer.SourceInfo graphs: Dict[ast.FunctionDef, cfg.Graph] include_annotations: Bool, whether type annotations should be included in the analysis. Returns: ast.AST
11471145Definitiontensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py40classDefinition objects describe a unique definition of a variable. Subclasses of this may be used by passing an appropriate factory function to resolve. Attributes: param_of: Optional[ast.AST] directives: Dict, optional definition annotations
11481146Analyzertensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py112classCFG visitor that determines reaching definitions at statement level.
11491147init_statetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py120method
11501148visit_nodetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py123method
11511149TreeAnnotatortensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py169classAST visitor that annotates each symbol name with its reaching definitions. Simultaneously, the visitor runs the dataflow analysis on each function node, accounting for the effect of closures. For example: def foo(): bar = 1 def baz(): # bar = 1 reaches here
11521150visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py189method
11531151visit_Nametensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py204method
11541152visit_Iftensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py233method
11551153visit_Fortensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py237method
11561154visit_Whiletensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py253method
11571155visit_Trytensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py257method
11581156visit_ExceptHandlertensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py261method
11591157visittensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py267method
11601158resolvetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_definitions.py279functionResolves reaching definitions for each symbol. Args: node: ast.AST source_info: transformer.SourceInfo graphs: Dict[ast.FunctionDef, cfg.Graph] definition_factory: Callable[[], Definition] Returns: ast.AST
11611159Definitiontensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py32classDefinition objects describe a unique definition of a function.
11621160Analyzertensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py76classCFG visitor that determines reaching definitions at statement level.
11631161init_statetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py85method
11641162visit_nodetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py88method
11651163TreeAnnotatortensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py109classAST visitor that annotates each symbol name with its reaching definitions. Simultaneously, the visitor runs the dataflow analysis on each function node, accounting for the effect of closures. For example: def foo(): def f(): pass def g(): # `def f` reaches here
11661164visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py147method
11671165visit_Lambdatensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py150method
11681166visittensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py153method
11691167resolvetensorflow/tensorflow/python/autograph/pyct/static_analysis/reaching_fndefs.py170functionResolves reaching definitions for each symbol. Args: node: ast.AST source_info: transformer.SourceInfo graphs: Dict[ast.FunctionDef, cfg.Graph] Returns: ast.AST
11701168Resolvertensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py41classResolver objects handle the process of looking up actual names and types. All resolve_* methods: * have a first namespace argument, mapping string to actual values * specify names as QN objects * specify types as a Set of inferred types All resolve_* methods must return either: * a set of `type` objects * None
11711169res_nametensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py54methodResolves the type an external (e.g. closure, global) variable.
11721170res_valuetensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py58methodResolves the type a literal value.
11731171res_calltensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py63methodResolves the return type an external function or method call. Args: ns: namespace name: str, the function name target: if this is a method call, the types of the method target, None otherwise args: list or argument types keywords: dict of name to argument types starargs: list of types of the *args arguments (should be at most one) kwargs: list of types of the **kwargs arguments (in order of appearance)
11741172res_argtensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py78methodResolves the type of a (possibly annotated) function argument.
11751173StmtInferrertensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py162classRuns type inference on a single AST statement. This visitor annotates most nodes with type information. It also sets types for the symbols modified by this statement in its types_out property.
11761174visittensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py178method
11771175visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py185method
11781176visit_Constanttensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py189method
11791177visit_Tupletensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py192method
11801178visit_Listtensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py203method
11811179visit_Settensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py212method
11821180visit_Nametensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py215method
11831181visit_Calltensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py244method
11841182visit_Indextensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py273method
11851183visit_Assigntensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py276method
11861184visit_Subscripttensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py284method
11871185visit_Comparetensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py294method
11881186visit_BinOptensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py316method
11891187Analyzertensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py329classCFG visitor that propagates type information across statements.
11901188init_statetensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py348method
11911189visit_nodetensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py364method
11921190FunctionVisitortensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py394classAST visitor that applies type inference to each function separately.
11931191visit_FunctionDeftensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py402method
11941192resolvetensorflow/tensorflow/python/autograph/pyct/static_analysis/type_inference.py417functionPerforms type inference. Args: node: ast.AST source_info: transformer.SourceInfo graphs: Dict[ast.FunctionDef, cfg.Graph] resolver: Resolver Returns: ast.AST
11951193simple_functiontensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py23functionDocstring.
11961194nested_functionstensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py28functionDocstring.
11971195function_with_printtensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py37function
11981196SimpleClasstensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py44class
11991197simple_methodtensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py46method
12001198method_with_printtensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py49method
12011199function_with_multiline_calltensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py53functionDocstring.
12021200basic_decoratortensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py61function
12031201decorated_functiontensorflow/tensorflow/python/autograph/pyct/testing/basic_definitions.py67function
12041202NodeSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py30class
12051203sampletensorflow/tensorflow/python/autograph/pyct/testing/codegen.py33method
12061204StatementSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py39class
12071205ExpressionSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py49class
12081206CompareSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py58class
12091207BinaryOpSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py71class
12101208UnaryOpSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py83class
12111209NameSamplertensorflow/tensorflow/python/autograph/pyct/testing/codegen.py87class
12121210CodeGeneratortensorflow/tensorflow/python/autograph/pyct/testing/codegen.py98classGenerate random syntactically-valid Python ASTs.
12131211generate_statementtensorflow/tensorflow/python/autograph/pyct/testing/codegen.py105methodGenerate a statement node, dispatching to the correct class method.
12141212sample_node_listtensorflow/tensorflow/python/autograph/pyct/testing/codegen.py124methodGenerate a list of statements of random length. Args: low: Fewest number of statements to generate. high: Highest number of statements to generate. generator: Function to call to generate nodes. Returns: A list of statements.
12151213generate_Nametensorflow/tensorflow/python/autograph/pyct/testing/codegen.py140method
12161214generate_BinOptensorflow/tensorflow/python/autograph/pyct/testing/codegen.py145method
12171215generate_Comparetensorflow/tensorflow/python/autograph/pyct/testing/codegen.py151method
12181216generate_UnaryOptensorflow/tensorflow/python/autograph/pyct/testing/codegen.py155method
12191217generate_expressiontensorflow/tensorflow/python/autograph/pyct/testing/codegen.py160method
12201218generate_Assigntensorflow/tensorflow/python/autograph/pyct/testing/codegen.py167methodGenerate an Assign node.
12211219generate_Iftensorflow/tensorflow/python/autograph/pyct/testing/codegen.py177methodGenerate an If node.
12221220generate_Whiletensorflow/tensorflow/python/autograph/pyct/testing/codegen.py196methodGenerate a While node.
12231221generate_Calltensorflow/tensorflow/python/autograph/pyct/testing/codegen.py207method
12241222generate_Returntensorflow/tensorflow/python/autograph/pyct/testing/codegen.py210method
12251223generate_Printtensorflow/tensorflow/python/autograph/pyct/testing/codegen.py213method
12261224generate_FunctionDeftensorflow/tensorflow/python/autograph/pyct/testing/codegen.py216methodGenerate a FunctionDef node.
12271225generate_random_functiondeftensorflow/tensorflow/python/autograph/pyct/testing/codegen.py233function
12281226wrapping_decoratortensorflow/tensorflow/python/autograph/pyct/testing/decorators.py24function
12291227standalone_decoratortensorflow/tensorflow/python/autograph/pyct/testing/decorators.py33function
12301228functional_decoratortensorflow/tensorflow/python/autograph/pyct/testing/decorators.py41function
12311229set_verbositytensorflow/tensorflow/python/autograph/utils/ag_logging.py41functionSets the AutoGraph verbosity level. _Debug logging in AutoGraph_ More verbose logging is useful to enable when filing bug reports or doing more in-depth debugging. There are two means to control the logging verbosity: * The `set_verbosity` function * The `AUTOGRAPH_VERBOSITY` environment variable `set_verbosity` takes precedence over the environment variable. For example: ```python import os import tensorflow as tf os.environ['AUTOGRAPH_VERBOSITY'] = 5 # Verbosity is now 5 tf.autograph.set_verbosity(0) # Verbosity is now 0 os.environ['AUTOGRAPH_VERBOSITY'] = 1 # No effect, because set_verbosity was already called. ``` Logs entries are output to [absl](https://abseil.io)'s [default output](https://abseil.io/docs/python/guides/logging), with `INFO` level. Logs can be mirrored to stdout by using the `alsologtostdout` argument. Mirroring is enabled by default when Python runs in interactive mode. Args: level: int, the verbosity level; larger values specify increased verbosity; 0 means no logging. When reporting bugs, it is recommended to set this value to a larger number, like 10. alsologtostdout: bool, whether to also output log messages to `sys.stdout`.
12321230tracetensorflow/tensorflow/python/autograph/utils/ag_logging.py92functionTraces argument information at compilation time. `trace` is useful when debugging, and it always executes during the tracing phase, that is, when the TF graph is constructed. _Example usage_ ```python import tensorflow as tf for i in tf.range(10): tf.autograph.trace(i) # Output: <Tensor ...> ``` Args: *args: Arguments to print to `sys.stdout`.
12331231get_verbositytensorflow/tensorflow/python/autograph/utils/ag_logging.py114function
12341232has_verbositytensorflow/tensorflow/python/autograph/utils/ag_logging.py121function
12351233errortensorflow/tensorflow/python/autograph/utils/ag_logging.py131function
12361234logtensorflow/tensorflow/python/autograph/utils/ag_logging.py138function
12371235warntensorflow/tensorflow/python/autograph/utils/ag_logging.py145function
12381236BasicReftensorflow/tensorflow/python/autograph/utils/compat_util.py27classThis shim emulates the nonlocal keyword in Py2-compatible source.
12391237deprecated_py2_supporttensorflow/tensorflow/python/autograph/utils/compat_util.py34functionSwaps calling module with a Py2-specific implementation. Noop in Py3.
12401238control_dependency_on_returnstensorflow/tensorflow/python/autograph/utils/context_managers.py27functionCreate a TF control dependency on the return values of a function. If the function had no return value, a no-op context is returned. Args: return_value: The return value to set as control dependency. Returns: A context manager.
12411239alias_tensorstensorflow/tensorflow/python/autograph/utils/misc.py27functionWraps any Tensor arguments with an identity op. Any other argument, including Variables, is returned unchanged. Args: *args: Any arguments. Must contain at least one element. Returns: Same as *args, with Tensor instances replaced as described. Raises: ValueError: If args doesn't meet the requirements.
12421240get_range_lentensorflow/tensorflow/python/autograph/utils/misc.py55function
12431241MatchDTypetensorflow/tensorflow/python/autograph/utils/py_func.py28classAllows matching the dtype of an argument. Used in conjunction with function calls. For example, MatchDType(0) will match the DType of the first argument.
12441242wrap_py_functensorflow/tensorflow/python/autograph/utils/py_func.py38functionHelper that wraps a callable to py_func. The helper passes tensor arguments through the py_func interface. Non-tensor arguments are allowed, and will be passed to f directly. Note that non-tensor arguments are captured by f will not update every time the wrapper is called (this is consistent with its argument list, which only includes the tensor arguments). In general, it's safest not to reuse this wrapper. Args: f: Callable return_dtypes: None, individual of tuple/list of DType or MatchDType, the data type for each of f's return value(s). Set to None if f has no return values or use_dummy_return is True. Use MatchDType to define a dtype identical to that of `i`th argument (argument 0 is the first); an argument must of Tensor type if it is to be used with MatchDType. args: Positional arguments for f, as list or tuple. kwargs: Keyword arguments for f, as dict with string keys. May be None. use_dummy_return: If True, the function will return a dummy value of 1 and discard its actual return value. Returns: The return values of f converted to tensor. Raises: ValueError: if any of the arguments are incorrect.
12451243dynamic_list_appendtensorflow/tensorflow/python/autograph/utils/tensor_list.py26functionConverts a list append call inline.
12461244TensorListtensorflow/tensorflow/python/autograph/utils/tensor_list.py43classTensor list wrapper API-compatible with Python built-in list.
12471245appendtensorflow/tensorflow/python/autograph/utils/tensor_list.py51method
12481246poptensorflow/tensorflow/python/autograph/utils/tensor_list.py54method
12491247cleartensorflow/tensorflow/python/autograph/utils/tensor_list.py58method
12501248counttensorflow/tensorflow/python/autograph/utils/tensor_list.py61method
12511249is_dense_tensortensorflow/tensorflow/python/autograph/utils/tensors.py32function
12521250is_tensor_arraytensorflow/tensorflow/python/autograph/utils/tensors.py38function
12531251is_tensor_listtensorflow/tensorflow/python/autograph/utils/tensors.py42function
12541252is_range_tensortensorflow/tensorflow/python/autograph/utils/tensors.py51functionReturns True if a tensor is the result of a tf.range op. Best effort.
12551253list_local_devicestensorflow/tensorflow/python/client/device_lib.py25functionList the available devices available in the local process. Args: session_config: a session config proto or None to use the default config. Returns: A list of `DeviceAttribute` protocol buffers.
12561254TF_NewSessionOptionstensorflow/tensorflow/python/client/pywrap_tf_session.py51function
12571255TF_Resettensorflow/tensorflow/python/client/pywrap_tf_session.py65function
12581256SessionInterfacetensorflow/tensorflow/python/client/session.py51classBase class for implementations of TensorFlow client sessions.
12591257graphtensorflow/tensorflow/python/client/session.py55methodThe underlying TensorFlow graph, to be used in building Operations.
12601258sess_strtensorflow/tensorflow/python/client/session.py60methodThe TensorFlow process to which this session will connect.
12611259runtensorflow/tensorflow/python/client/session.py64methodRuns operations in the session. See `BaseSession.run()` for details.
12621260partial_run_setuptensorflow/tensorflow/python/client/session.py68methodSets up the feeds and fetches for partial runs in the session.
12631261partial_runtensorflow/tensorflow/python/client/session.py72methodContinues the execution with additional feeds and fetches.
12641262register_session_run_conversion_functionstensorflow/tensorflow/python/client/session.py144functionRegister fetch and feed conversion functions for `tf.Session.run()`. This function registers a triple of conversion functions for fetching and/or feeding values of user-defined types in a call to tf.Session.run(). An example ```python class SquaredTensor(object): def __init__(self, tensor): self.sq = tf.square(tensor) #you can define conversion functions as follows: fetch_function = lambda squared_tensor:([squared_tensor.sq], lambda val: val[0]) feed_function = lambda feed, feed_val: [(feed.sq, feed_val)] feed_function_for_partial_run = lambda feed: [feed.sq] #then after invoking this register function, you can use as follows: session.run(squared_tensor1, feed_dict = {squared_tensor2 : some_numpy_array}) ``` Args: tensor_type: The type for which you want to register a conversion function. fetch_function: A callable that takes an object of type `tensor_type` and returns a tuple, where the first element is a list of `tf.Tensor` objects, and the second element is a callable that takes a list of ndarrays and returns an object of some value type that corresponds to `tensor_type`. fetch_function describes how to expand fetch into its component Tensors and how to contract the fetched results back into a single return value. feed_function: A callable that takes feed_key and feed_value as input, and returns a list of tuples (feed_tensor, feed_val), feed_key must have type `tensor_type`, and feed_tensor must have type `tf.Tensor`. Each feed function describes how to unpack a single fed value and map it to feeds of one or more tensors and their corresponding values. feed_function_for_partial_run: A callable for specifying tensor values to feed when setting up a partial run, which takes a `tensor_type` type object as input, and returns a list of Tensors. Raises: ValueError: If `tensor_type` has already been registered.
12651263BaseSessiontensorflow/tensorflow/python/client/session.py627classA class for interacting with a TensorFlow computation. The BaseSession enables incremental graph building with inline execution of Operations and evaluation of Tensors.
12661264list_devicestensorflow/tensorflow/python/client/session.py706methodLists available devices in this session. ```python devices = sess.list_devices() for d in devices: print(d.name) ``` Where: Each element in the list has the following properties name: A string with the full name of the device. ex: `/job:worker/replica:0/task:3/device:CPU:0` device_type: The type of the device (e.g. `CPU`, `GPU`, `TPU`.) memory_limit: The maximum amount of memory available on the device. Note: depending on the device, it is possible the usable memory could be substantially less. Raises: tf.errors.OpError: If it encounters an error (e.g. session is in an invalid state, or network errors occur). Returns: A list of devices in the session.
12671265closetensorflow/tensorflow/python/client/session.py744methodCloses this session. Calling this method frees all resources associated with the session. Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while closing the TensorFlow session.
12681266graphtensorflow/tensorflow/python/client/session.py775methodThe graph that was launched in this session.
12691267graph_deftensorflow/tensorflow/python/client/session.py780methodA serializable version of the underlying TensorFlow graph. Returns: A graph_pb2.GraphDef proto containing nodes for all of the Operations in the underlying TensorFlow graph.
12701268sess_strtensorflow/tensorflow/python/client/session.py790method
12711269as_defaulttensorflow/tensorflow/python/client/session.py793methodReturns a context manager that makes this object the default session. Use with the `with` keyword to specify that calls to `tf.Operation.run` or `tf.Tensor.eval` should be executed in this session. ```python c = tf.constant(..) sess = tf.compat.v1.Session() with sess.as_default(): assert tf.compat.v1.get_default_session() is sess print(c.eval()) ``` To get the current default session, use `tf.compat.v1.get_default_session`. *N.B.* The `as_default` context manager *does not* close the session when you exit the context, and you must close the session explicitly. ```python c = tf.constant(...) sess = tf.compat.v1.Session() with sess.as_default(): print(c.eval()) # ... with sess.as_default(): print(c.eval()) sess.close() ``` Alternatively, you can use `with tf.compat.v1.Session():` to create a session that is automatically closed on exiting the context, including when an uncaught exception is raised. *N.B.* The default session is a property of the current thread. If you create a new thread, and wish to use the default session in that thread, you must explicitly add a `with sess.as_default():` in that thread's function. *N.B.* Entering a `with sess.as_default():` block does not affect the current default graph. If you are using multiple graphs, and `sess.graph` is different from the value of `tf.compat.v1.get_default_graph`, you must explicitly enter a `with sess.graph.as_default():` block to make `sess.graph` the default graph. Returns: A context manager using this session as the default session.
12721270runtensorflow/tensorflow/python/client/session.py848methodRuns operations and evaluates tensors in `fetches`. This method runs one "step" of TensorFlow computation, by running the necessary graph fragment to execute every `Operation` and evaluate every `Tensor` in `fetches`, substituting the values in `feed_dict` for the corresponding input values. The `fetches` argument may be a single graph element, or an arbitrarily nested list, tuple, namedtuple, dict, or OrderedDict containing graph elements at its leaves. A graph element can be one of the following types: * A `tf.Operation`. The corresponding fetched value will be `None`. * A `tf.Tensor`. The corresponding fetched value will be a numpy ndarray containing the value of that tensor. * A `tf.sparse.SparseTensor`. The corresponding fetched value will be a `tf.compat.v1.SparseTensorValue` containing the value of that sparse tensor. * A `get_tensor_handle` op. The corresponding fetched value will be a numpy ndarray containing the handle of that tensor. * A `string` which is the name of a tensor or operation in the graph. The value returned by `run()` has the same shape as the `fetches` argument, where the leaves are replaced by the corresponding values returned by TensorFlow. Example: ```python a = tf.constant([10, 20]) b = tf.constant([1.0, 2.0]) # 'fetches' can be a singleton v = session.run(a) # v is the numpy array [10, 20] # 'fetches' can be a list. v = session.run([a, b]) # v is a Python list with 2 numpy arrays: the 1-D array [10, 20] and the # 1-D array [1.0, 2.0] # 'fetches' can be arbitrary lists, tuples, namedtuple, dicts: MyData = collections.namedtuple('MyData', ['a', 'b']) v = session.run({'k1': MyData(a, b), 'k2': [b, a]}) # v is a dict with # v['k1'] is a MyData namedtuple with 'a' (the numpy array [10, 20]) and # 'b' (the numpy array [1.0, 2.0]) # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array # [10, 20]. ``` The optional `feed_dict` argument allows the caller to override the value of tensors in the graph. Each key in `feed_dict` can be one of the following types: * If the key is a `tf.Tensor`, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same `dtype` as that tensor. Additionally, if the key is a `tf.compat.v1.placeholder`, the shape of the value will be checked for compatibility with the placeholder. * If the key is a `tf.sparse.SparseTensor`, the value should be a `tf.compat.v1.SparseTensorValue`. * If the key is a nested tuple of `Tensor`s or `SparseTensor`s, the value should be a nested tuple with the same structure that maps to their corresponding values as above. Each value in `feed_dict` must be convertible to a numpy array of the dtype of the corresponding key. The optional `options` argument expects a [`RunOptions`] proto. The options allow controlling the behavior of this particular step (e.g. turning tracing on). The optional `run_metadata` argument expects a [`RunMetadata`] proto. When appropriate, the non-Tensor output of this step will be collected there. For example, when users turn on tracing in `options`, the profiled info will be collected into this argument and passed back. Args: fetches: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above). feed_dict: A dictionary that maps graph elements to values (described above). options: A [`RunOptions`] protocol buffer run_metadata: A [`RunMetadata`] protocol buffer Returns: Either a single value if `fetches` is a single graph element, or a list of values if `fetches` is a list, or a dictionary with the same keys as `fetches` if that is a dictionary (described above). Order in which `fetches` operations are evaluated inside the call is undefined. Raises: RuntimeError: If this `Session` is in an invalid state (e.g. has been closed). TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type. ValueError: If `fetches` or `feed_dict` keys are invalid or refer to a `Tensor` that doesn't exist.
12731271partial_runtensorflow/tensorflow/python/client/session.py969methodContinues the execution with more feeds and fetches. This is EXPERIMENTAL and subject to change. To use partial execution, a user first calls `partial_run_setup()` and then a sequence of `partial_run()`. `partial_run_setup` specifies the list of feeds and fetches that will be used in the subsequent `partial_run` calls. The optional `feed_dict` argument allows the caller to override the value of tensors in the graph. See run() for more information. Below is a simple example: ```python a = array_ops.placeholder(dtypes.float32, shape=[]) b = array_ops.placeholder(dtypes.float32, shape=[]) c = array_ops.placeholder(dtypes.float32, shape=[]) r1 = math_ops.add(a, b) r2 = math_ops.multiply(r1, c) h = sess.partial_run_setup([r1, r2], [a, b, c]) res = sess.partial_run(h, r1, feed_dict={a: 1, b: 2}) res = sess.partial_run(h, r2, feed_dict={c: res}) ``` Args: handle: A handle for a sequence of partial runs. fetches: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (see documentation for `run`). feed_dict: A dictionary that maps graph elements to values (described above). Returns: Either a single value if `fetches` is a single graph element, or a list of values if `fetches` is a list, or a dictionary with the same keys as `fetches` if that is a dictionary (see documentation for `run`). Raises: tf.errors.OpError: Or one of its subclasses on error.
12741272partial_run_setuptensorflow/tensorflow/python/client/session.py1016methodSets up a graph with feeds and fetches for partial run. This is EXPERIMENTAL and subject to change. Note that contrary to `run`, `feeds` only specifies the graph elements. The tensors will be supplied by the subsequent `partial_run` calls. Args: fetches: A single graph element, or a list of graph elements. feeds: A single graph element, or a list of graph elements. Returns: A handle for partial run. Raises: RuntimeError: If this `Session` is in an invalid state (e.g. has been closed). TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type. tf.errors.OpError: Or one of its subclasses if a TensorFlow error happens.
12751273make_callabletensorflow/tensorflow/python/client/session.py1186methodReturns a Python callable that runs a particular step. The returned callable will take `len(feed_list)` arguments whose types must be compatible feed values for the respective elements of `feed_list`. For example, if element `i` of `feed_list` is a `tf.Tensor`, the `i`th argument to the returned callable must be a numpy ndarray (or something convertible to an ndarray) with matching element type and shape. See `tf.Session.run` for details of the allowable feed key and value types. The returned callable will have the same return type as `tf.Session.run(fetches, ...)`. For example, if `fetches` is a `tf.Tensor`, the callable will return a numpy ndarray; if `fetches` is a `tf.Operation`, it will return `None`. Args: fetches: A value or list of values to fetch. See `tf.Session.run` for details of the allowable fetch types. feed_list: (Optional.) A list of `feed_dict` keys. See `tf.Session.run` for details of the allowable feed key types. accept_options: (Optional.) If `True`, the returned `Callable` will be able to accept `tf.compat.v1.RunOptions` and `tf.compat.v1.RunMetadata` as optional keyword arguments `options` and `run_metadata`, respectively, with the same syntax and semantics as `tf.Session.run`, which is useful for certain use cases (profiling and debugging) but will result in measurable slowdown of the `Callable`'s performance. Default: `False`. Returns: A function that when called will execute the step defined by `feed_list` and `fetches` in this session. Raises: TypeError: If `fetches` or `feed_list` cannot be interpreted as arguments to `tf.Session.run`.
12761274Sessiontensorflow/tensorflow/python/client/session.py1509classA class for running TensorFlow operations. A `Session` object encapsulates the environment in which `Operation` objects are executed, and `Tensor` objects are evaluated. For example: ```python tf.compat.v1.disable_eager_execution() # need to disable eager in TF2.x # Build a graph. a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # Launch the graph in a session. sess = tf.compat.v1.Session() # Evaluate the tensor `c`. print(sess.run(c)) # prints 30.0 ``` A session may own resources, such as `tf.Variable`, `tf.queue.QueueBase`, and `tf.compat.v1.ReaderBase`. It is important to release these resources when they are no longer required. To do this, either invoke the `tf.Session.close` method on the session, or use the session as a context manager. The following two examples are equivalent: ```python # Using the `close()` method. sess = tf.compat.v1.Session() sess.run(...) sess.close() # Using the context manager. with tf.compat.v1.Session() as sess: sess.run(...) ``` The [`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto) protocol buffer exposes various configuration options for a session. For example, to create a session that uses soft constraints for device placement, and log the resulting placement decisions, create a session as follows: ```python # Launch the graph in a session that allows soft device placement and # logs the placement decisions. sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto( allow_soft_placement=True, log_device_placement=True)) ```
12771275resettensorflow/tensorflow/python/client/session.py1644methodResets resource containers on `target`, and close all connected sessions. A resource container is distributed across all workers in the same cluster as `target`. When a resource container on `target` is reset, resources associated with that container will be cleared. In particular, all Variables in the container will become undefined: they lose their values and shapes. NOTE: (i) reset() is currently only implemented for distributed sessions. (ii) Any sessions on the master named by `target` will be closed. If no resource containers are provided, all containers are reset. Args: target: The execution engine to connect to. containers: A list of resource container name strings, or `None` if all of all the containers are to be reset. config: (Optional.) Protocol buffer with configuration options. Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while resetting containers.
12781276InteractiveSessiontensorflow/tensorflow/python/client/session.py1679classA TensorFlow `Session` for use in interactive contexts, such as a shell. The only difference with a regular `Session` is that an `InteractiveSession` installs itself as the default session on construction. The methods `tf.Tensor.eval` and `tf.Operation.run` will use that session to run ops. This is convenient in interactive shells and [IPython notebooks](http://ipython.org), as it avoids having to pass an explicit `Session` object to run ops. For example: ```python sess = tf.compat.v1.InteractiveSession() a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # We can just use 'c.eval()' without passing 'sess' print(c.eval()) sess.close() ``` Note that a regular session installs itself as the default session when it is created in a `with` statement. The common usage in non-interactive programs is to follow that pattern: ```python a = tf.constant(5.0) b = tf.constant(6.0) c = a * b with tf.compat.v1.Session(): # We can also use 'c.eval()' here. print(c.eval()) ```
12791277closetensorflow/tensorflow/python/client/session.py1771methodCloses an `InteractiveSession`.
12801278SessionBenchmarktensorflow/tensorflow/python/client/session_benchmark.py36classTests and benchmarks for interacting with the `tf.compat.v1.Session`.
12811279benchmarkGrpcSessiontensorflow/tensorflow/python/client/session_benchmark.py178method
12821280benchmarkDirectSessiontensorflow/tensorflow/python/client/session_benchmark.py204method
12831281AllocationMaximumtensorflow/tensorflow/python/client/timeline.py32classStores the maximum allocation for a given allocator within the timelne. Parameters: timestamp: `tensorflow::Env::NowMicros()` when this maximum was reached. num_bytes: the total memory used at this time. tensors: the set of tensors allocated at this time.
12841282StepStatsAnalysistensorflow/tensorflow/python/client/timeline.py44classStores the step stats analysis output. Parameters: chrome_trace: A dict containing the chrome trace analysis. allocator_maximums: A dict mapping allocator names to AllocationMaximum.
12851283Timelinetensorflow/tensorflow/python/client/timeline.py346classA class for visualizing execution timelines of TensorFlow steps.
12861284analyze_step_statstensorflow/tensorflow/python/client/timeline.py674methodAnalyze the step stats and format it into Chrome Trace Format. Args: show_dataflow: (Optional.) If True, add flow events to the trace connecting producers and consumers of tensors. show_memory: (Optional.) If True, add object snapshot events to the trace showing the sizes and lifetimes of tensors. op_time: (Optional.) How the execution time of op is shown in timeline. Possible values are "schedule", "gpu" and "all". "schedule" will show op from the time it is scheduled to the end of the scheduling. Notice by the end of its scheduling its async kernels may not start yet. It is shown using the default value from step_stats. "gpu" will show op with the execution time of its kernels on GPU. "all" will show op from the start of its scheduling to the end of its last kernel. Returns: A 'StepStatsAnalysis' object.
12871285generate_chrome_trace_formattensorflow/tensorflow/python/client/timeline.py707methodProduces a trace in Chrome Trace Format. Args: show_dataflow: (Optional.) If True, add flow events to the trace connecting producers and consumers of tensors. show_memory: (Optional.) If True, add object snapshot events to the trace showing the sizes and lifetimes of tensors. op_time: (Optional.) How the execution time of op is shown in timeline. Possible values are "schedule", "gpu" and "all". "schedule" will show op from the time it is scheduled to the end of the scheduling. Notice by the end of its scheduling its async kernels may not start yet. It is shown using the default value from step_stats. "gpu" will show op with the execution time of its kernels on GPU. "all" will show op from the start of its scheduling to the end of its last kernel. Returns: A JSON formatted string in Chrome Trace format.
12881286forward_compatibletensorflow/tensorflow/python/compat/compat.py70functionReturn true if the forward compatibility window has expired. See [Version compatibility](https://tensorflow.org/guide/version_compat#backward_forward). Forward-compatibility refers to scenarios where the producer of a TensorFlow model (a GraphDef or SavedModel) is compiled against a version of the TensorFlow library newer than what the consumer was compiled against. The "producer" is typically a Python program that constructs and trains a model while the "consumer" is typically another program that loads and serves the model. TensorFlow has been supporting a 3 week forward-compatibility window for programs compiled from source at HEAD. For example, consider the case where a new operation `MyNewAwesomeAdd` is created with the intent of replacing the implementation of an existing Python wrapper - `tf.add`. The Python wrapper implementation should change from something like: ```python def add(inputs, name=None): return gen_math_ops.add(inputs, name) ``` to: ```python from tensorflow.python.compat import compat def add(inputs, name=None): if compat.forward_compatible(year, month, day): # Can use the awesome new implementation. return gen_math_ops.my_new_awesome_add(inputs, name) # To maintain forward compatibility, use the old implementation. return gen_math_ops.add(inputs, name) ``` Where `year`, `month`, and `day` specify the date beyond which binaries that consume a model are expected to have been updated to include the new operations. This date is typically at least 3 weeks beyond the date the code that adds the new operation is committed. Args: year: A year (e.g., 2018). Must be an `int`. month: A month (1 <= month <= 12) in year. Must be an `int`. day: A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an `int`. Returns: True if the caller can expect that serialized TensorFlow graphs produced can be consumed by programs that are compiled with the TensorFlow library source code after (year, month, day).
12891287forward_compatibility_horizontensorflow/tensorflow/python/compat/compat.py131functionContext manager for testing forward compatibility of generated graphs. See [Version compatibility](https://tensorflow.org/guide/version_compat#backward_forward). To ensure forward compatibility of generated graphs (see `forward_compatible`) with older binaries, new features can be gated with: ```python if compat.forward_compatible(year=2018, month=08, date=01): generate_graph_with_new_features() else: generate_graph_so_older_binaries_can_consume_it() ``` However, when adding new features, one may want to unittest it before the forward compatibility window expires. This context manager enables such tests. For example: ```python from tensorflow.python.compat import compat def testMyNewFeature(self): with compat.forward_compatibility_horizon(2018, 08, 02): # Test that generate_graph_with_new_features() has an effect ``` Args: year: A year (e.g., 2018). Must be an `int`. month: A month (1 <= month <= 12) in year. Must be an `int`. day: A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an `int`. Yields: Nothing.
12901288enable_v2_behaviortensorflow/tensorflow/python/compat/v2_compat.py43functionEnables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 2.x. This function is called in the main TensorFlow `__init__.py` file, user should not need to call it, except during complex migrations.
12911289disable_v2_behaviortensorflow/tensorflow/python/compat/v2_compat.py82functionDisables TensorFlow 2.x behaviors. This function can be called at the beginning of the program (before `Tensors`, `Graphs` or other structures have been created, and before devices have been initialized. It switches all global behaviors that are different between TensorFlow 1.x and 2.x to behave as intended for 1.x. User can call this function to disable 2.x behavior during complex migrations.
12921290convert_graph_deftensorflow/tensorflow/python/compiler/mlir/mlir.py26functionImport a GraphDef and convert it to a textual MLIR module. Args: graph_def: An object of type graph_pb2.GraphDef or a textual proto representation of a valid GraphDef. pass_pipeline: A textual description of an MLIR Pass Pipeline to run on the module, see MLIR documentation for the [textual pass pipeline syntax](https://github.com/tensorflow/mlir/blob/master/g3doc/WritingAPass.md#textual-pass-pipeline-specification). Returns: A textual representation of the MLIR module corresponding to the graphdef. Raises a RuntimeError on error.
12931291TrtPrecisionModetensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py98class
12941292supported_precision_modestensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py104method
12951293TrtConversionParamstensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py117classParameters that are used for TF-TRT conversion. Fields: rewriter_config_template: a template RewriterConfig proto used to create a TRT-enabled RewriterConfig. If None, it will use a default one. max_workspace_size_bytes: the maximum GPU temporary memory which the TRT engine can use at execution time. This corresponds to the 'workspaceSize' parameter of nvinfer1::IBuilder::setMaxWorkspaceSize(). precision_mode: one the strings in TrtPrecisionMode.supported_precision_modes(). minimum_segment_size: the minimum number of nodes required for a subgraph to be replaced by TRTEngineOp. is_dynamic_op: whether to generate dynamic TRT ops which will build the TRT network and engine at run time. i.e. Since TensorRT version < 6.0 does not support dynamic dimensions other than the batch dimension, when the TensorFlow graph has a non-batch dimension of dynamic size, we would need to enable this option. This option should be set to True in TF 2.0. maximum_cached_engines: max number of cached TRT engines for dynamic TRT ops. Created TRT engines for a dynamic dimension are cached. This is the maximum number of engines that can be cached. If the number of cached engines is already at max but none of them supports the input shapes, the TRTEngineOp will fall back to run the original TF subgraph that corresponds to the TRTEngineOp. use_calibration: this argument is ignored if precision_mode is not INT8. If set to True, a calibration graph will be created to calibrate the missing ranges. The calibration graph must be converted to an inference graph by running calibration with calibrate(). If set to False, quantization nodes will be expected for every tensor in the graph (excluding those which will be fused). If a range is missing, an error will occur. Please note that accuracy may be negatively affected if there is a mismatch between which tensors TRT quantizes and which tensors were trained with fake quantization. max_batch_size: max size for the input batch. This parameter is only effective when is_dynamic_op=False which is not supported in TF 2.0. allow_build_at_runtime: whether to build TensorRT engines during runtime. If no TensorRT engine can be found in cache that can handle the given inputs during runtime, then a new TensorRT engine is built at runtime if allow_build_at_runtime=True, and otherwise native TF is used. This argument is only effective if is_dynamic_op=True.
12961294get_tensorrt_rewriter_configtensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py292functionReturns a RewriterConfig proto for TRT transformation. Args: conversion_params: a TrtConversionParams instance. is_v2: whether we're getting a RewriterConfig for TF 2.0. disable_non_trt_optimizers: Turn off all default Grappler optimizers. Returns: A RewriterConfig proto which sets a TensorRTOptimizer to run Grappler. Raises: TypeError: if any of the parameters are of unexpected type. ValueError: if any of the parameters are of unexpected value.
12971295is_explicit_batch_mode_enabledtensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py387functionChecks whether explicit batch is enabled by the rewriter config.
12981296TrtGraphConvertertensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py398classA converter for TF-TRT transformation for TF 1.x GraphDef/SavedModels. To run the conversion without quantization calibration (e.g. for FP32/FP16 precision modes): ```python converter = TrtGraphConverter( input_saved_model_dir="my_dir", precision_mode=TrtPrecisionMode.FP16) converted_graph_def = converter.convert() converter.save(output_saved_model_dir) ``` To run the conversion with quantization calibration: ```python converter = TrtGraphConverter( input_saved_model_dir="my_dir", precision_mode=TrtPrecisionMode.INT8) converter.convert() # Run calibration 10 times. converted_graph_def = converter.calibrate( fetch_names=['output:0'], num_runs=10, feed_dict_fn=lambda: {'input:0': my_next_data()}) converter.save(output_saved_model_dir) ```
12991297converttensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py643methodRun the TF-TRT conversion. Returns: The converted GraphDef for TF 1.x.
13001298calibratetensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py656methodRun the calibration and return the calibrated GraphDef. Args: fetch_names: a list of output tensor name to fetch during calibration. num_runs: number of runs of the graph during calibration. feed_dict_fn: a function that returns a dictionary mapping input names (as strings) in the GraphDef to be calibrated to values (e.g. Python list, numpy arrays, etc). One and only one of `feed_dict_fn` and `input_map_fn` should be specified. input_map_fn: a function that returns a dictionary mapping input names (as strings) in the GraphDef to be calibrated to Tensor objects. The values of the named input tensors in the GraphDef to be calibrated will be re-mapped to the respective `Tensor` values during calibration. One and only one of `feed_dict_fn` and `input_map_fn` should be specified. Raises: ValueError: if the input combination is invalid. RuntimeError: if this method is called in eager mode. Returns: The GraphDef after the calibration.
13011299savetensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py749methodSave the converted graph as a SavedModel. Args: output_saved_model_dir: construct a SavedModel using the converted GraphDef and save it to the specified directory. This option only works when the input graph is loaded from a SavedModel, i.e. when input_saved_model_dir is specified and input_graph_def is None in __init__(). Raises: ValueError: if the input to the converter is a GraphDef instead of a SavedModel.
13021300TrtGraphConverterV2tensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py880classAn offline converter for TF-TRT transformation for TF 2.0 SavedModels. Currently this is not available on Windows platform. Note that in V2, is_dynamic_op=False is not supported, meaning TRT engines will be built only when the corresponding TRTEngineOp is executed. But we still provide a way to avoid the cost of building TRT engines during inference (see more below). There are several ways to run the conversion: 1. FP32/FP16 precision ```python params = tf.experimental.tensorrt.ConversionParams( precision_mode='FP16') converter = tf.experimental.tensorrt.Converter( input_saved_model_dir="my_dir", conversion_params=params) converter.convert() converter.save(output_saved_model_dir) ``` In this case, no TRT engines will be built or saved in the converted SavedModel. But if input data is available during conversion, we can still build and save the TRT engines to reduce the cost during inference (see option 2 below). 2. FP32/FP16 precision with pre-built engines ```python params = tf.experimental.tensorrt.ConversionParams( precision_mode='FP16', # Set this to a large enough number so it can cache all the engines. maximum_cached_engines=16) converter = tf.experimental.tensorrt.Converter( input_saved_model_dir="my_dir", conversion_params=params) converter.convert() # Define a generator function that yields input data, and use it to execute # the graph to build TRT engines. # With TensorRT 5.1, different engines will be built (and saved later) for # different input shapes to the TRTEngineOp. def my_input_fn(): for _ in range(num_runs): inp1, inp2 = ... yield inp1, inp2 converter.build(input_fn=my_input_fn) # Generate corresponding TRT engines converter.save(output_saved_model_dir) # Generated engines will be saved. ``` In this way, one engine will be built/saved for each unique input shapes of the TRTEngineOp. This is good for applications that cannot afford building engines during inference but have access to input data that is similar to the one used in production (for example, that has the same input shapes). Also, the generated TRT engines is platform dependent, so we need to run `build()` in an environment that is similar to production (e.g. with same type of GPU). 3. INT8 precision and calibration with pre-built engines ```python params = tf.experimental.tensorrt.ConversionParams( precision_mode='INT8', # Currently only one INT8 engine is supported in this mode. maximum_cached_engines=1, use_calibration=True) converter = tf.experimental.tensorrt.Converter( input_saved_model_dir="my_dir", conversion_params=params) # Define a generator function that yields input data, and run INT8 # calibration with the data. All input data should have the same shape. # At the end of convert(), the calibration stats (e.g. range information) # will be saved and can be used to generate more TRT engines with different # shapes. Also, one TRT engine will be generated (with the same shape as # the calibration data) for save later. def my_calibration_input_fn(): for _ in range(num_runs): inp1, inp2 = ... yield inp1, inp2 converter.convert(calibration_input_fn=my_calibration_input_fn) # (Optional) Generate more TRT engines offline (same as the previous # option), to avoid the cost of generating them during inference. def my_input_fn(): for _ in range(num_runs): inp1, inp2 = ... yield inp1, inp2 converter.build(input_fn=my_input_fn) # Save the TRT engine and the engines. converter.save(output_saved_model_dir) ```
13031301converttensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py1060methodConvert the input SavedModel in 2.0 format. Args: calibration_input_fn: a generator function that yields input data as a list or tuple, which will be used to execute the converted signature for calibration. All the returned input data should have the same shape. Example: `def input_fn(): yield input1, input2, input3` Raises: ValueError: if the input combination is invalid. Returns: The TF-TRT converted Function.
13041302buildtensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py1126methodRun inference with converted graph in order to build TensorRT engines. Args: input_fn: a generator function that yields input data as a list or tuple, which will be used to execute the converted signature to generate TRT engines. Example: `def input_fn(): # Let's assume a network with 2 input tensors. We generate 3 sets # of dummy input data: input_shapes = [[(1, 16), (2, 16)], # 1st input list [(2, 32), (4, 32)], # 2nd list of two tensors [(4, 32), (8, 32)]] # 3rd input list for shapes in input_shapes: # return a list of input tensors yield [np.zeros(x).astype(np.float32) for x in shapes]` Raises: NotImplementedError: build() is already called. RuntimeError: the input_fx is None.
13051303savetensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py1189methodSave the converted SavedModel. Args: output_saved_model_dir: directory to saved the converted SavedModel.
13061304create_inference_graphtensorflow/tensorflow/python/compiler/tensorrt/trt_convert.py1270functionPython wrapper for the TRT transformation. Args: input_graph_def: a GraphDef object containing a model to be transformed. If set to None, the graph will be read from the SavedModel loaded from input_saved_model_dir. outputs: list of tensors or node names for the model outputs. Only used when input_graph_def is not None. max_batch_size: max size for the input batch. max_workspace_size_bytes: the maximum GPU temporary memory which the TRT engine can use at execution time. This corresponds to the 'workspaceSize' parameter of nvinfer1::IBuilder::setMaxWorkspaceSize(). precision_mode: one of TrtPrecisionMode.supported_precision_modes(). minimum_segment_size: the minimum number of nodes required for a subgraph to be replaced by TRTEngineOp. is_dynamic_op: whether to generate dynamic TRT ops which will build the TRT network and engine at run time. maximum_cached_engines: max number of cached TRT engines in dynamic TRT ops. If the number of cached engines is already at max but none of them can serve the input, the TRTEngineOp will fall back to run the TF function based on which the TRTEngineOp is created. input_saved_model_dir: the directory to load the SavedModel which contains the input graph to transforms. Used only when input_graph_def is None. input_saved_model_tags: list of tags to load the SavedModel. input_saved_model_signature_key: the key of the signature to optimize the graph for. output_saved_model_dir: if not None, construct a SavedModel using the returned GraphDef and save it to the specified directory. This option only works when the input graph is loaded from a SavedModel, i.e. when input_saved_model_dir is specified and input_graph_def is None. session_config: the ConfigProto used to create a Session. It's also used as a template to create a TRT-enabled ConfigProto for conversion. If not specified, a default ConfigProto will be used. Returns: A GraphDef transformed from input_graph_def (or the SavedModel graph def loaded from input_saved_model_dir, if input_graph_def is not present), where all TRT compatible subgraphs are replaced with TRTEngineOps, and a TF function is added for each of the subgraphs. If is_dynamic_op is True, each TRTEngineOp will contain a serialized subgraph GraphDef, which will be converted to a TRT engine at execution time and the TRT engine will be cached for future usage. A new TRT engine will be created each time when none of the cached engines match the input shapes. If it fails to execute the TRT engine or the number of cached engines reaches maximum_cached_engines, the op will fall back to call the corresponding TF function. If is_dynamic_op is False, each TRTEngineOp will contain a serialized TRT engine created from the corresponding subgraph. No more engines will be created on the fly, and the op will fall back to call the corresponding TF function when it fails to execute the engine. Raises: ValueError: if the combination of the parameters is invalid.
13071305TrtPrecisionModetensorflow/tensorflow/python/compiler/tensorrt/trt_convert_windows.py31class
13081306TrtConversionParamstensorflow/tensorflow/python/compiler/tensorrt/trt_convert_windows.py43classParameters that are used for TF-TRT conversion. Fields: rewriter_config_template: a template RewriterConfig proto used to create a TRT-enabled RewriterConfig. If None, it will use a default one. max_workspace_size_bytes: the maximum GPU temporary memory which the TRT engine can use at execution time. This corresponds to the 'workspaceSize' parameter of nvinfer1::IBuilder::setMaxWorkspaceSize(). precision_mode: one the strings in TrtPrecisionMode.supported_precision_modes(). minimum_segment_size: the minimum number of nodes required for a subgraph to be replaced by TRTEngineOp. is_dynamic_op: whether to generate dynamic TRT ops which will build the TRT network and engine at run time. i.e. Since TensorRT version < 6.0 does not support dynamic dimensions other than the batch dimension, when the TensorFlow graph has a non-batch dimension of dynamic size, we would need to enable this option. This option should be set to True in TF 2.0. maximum_cached_engines: max number of cached TRT engines for dynamic TRT ops. Created TRT engines for a dynamic dimension are cached. This is the maximum number of engines that can be cached. If the number of cached engines is already at max but none of them supports the input shapes, the TRTEngineOp will fall back to run the original TF subgraph that corresponds to the TRTEngineOp. use_calibration: this argument is ignored if precision_mode is not INT8. If set to True, a calibration graph will be created to calibrate the missing ranges. The calibration graph must be converted to an inference graph by running calibration with calibrate(). If set to False, quantization nodes will be expected for every tensor in the graph (exlcuding those which will be fused). If a range is missing, an error will occur. Please note that accuracy may be negatively affected if there is a mismatch between which tensors TRT quantizes and which tensors were trained with fake quantization. max_batch_size: max size for the input batch. This parameter is only effective when is_dynamic_op=False which is not supported in TF 2.0.
13091307TrtConverterWindowstensorflow/tensorflow/python/compiler/tensorrt/trt_convert_windows.py97classAn offline converter for TF-TRT transformation for TF 2.0 SavedModels. Currently this is not available on Windows platform.
13101308conv2d_layertensorflow/tensorflow/python/compiler/tensorrt/test/conv2d_test.py32function
13111309div_round_uptensorflow/tensorflow/python/compiler/tensorrt/test/conv2d_test.py62function
13121310build_graphtensorflow/tensorflow/python/compiler/tensorrt/test/conv2d_test.py66function
13131311CalibrationInt32Supporttensorflow/tensorflow/python/compiler/tensorrt/test/int32_test.py68classTest execution of calibration with int32 input
13141312GraphFntensorflow/tensorflow/python/compiler/tensorrt/test/int32_test.py71method
13151313GetParamstensorflow/tensorflow/python/compiler/tensorrt/test/int32_test.py76method
13161314ExpectedEnginesToBuildtensorflow/tensorflow/python/compiler/tensorrt/test/int32_test.py87method
13171315IsQuantizationModetensorflow/tensorflow/python/compiler/tensorrt/test/tf_trt_integration_test_base.py95function
13181316IsQuantizationWithCalibrationtensorflow/tensorflow/python/compiler/tensorrt/test/tf_trt_integration_test_base.py99function
13191317GraphStatetensorflow/tensorflow/python/compiler/tensorrt/test/tf_trt_integration_test_base.py103class
13201318GetGraphtensorflow/tensorflow/python/compiler/tensorrt/test/testdata/gen_tftrt_model.py49functionDefine graph.
13211319GenerateModelV2tensorflow/tensorflow/python/compiler/tensorrt/test/testdata/gen_tftrt_model.py59functionGenerate and convert a model using TFv2 API.
13221320GenerateModelV1tensorflow/tensorflow/python/compiler/tensorrt/test/testdata/gen_tftrt_model.py90functionGenerate and convert a model using TFv1 API.
13231321experimental_jit_scopetensorflow/tensorflow/python/compiler/xla/jit.py42functionEnable or disable JIT compilation of operators within the scope. NOTE: This is an experimental feature. The compilation is a hint and only supported on a best-effort basis. Example usage: ```python with tf.xla.experimental.jit_scope(): c = tf.matmul(a, b) # compiled with tf.xla.experimental.jit_scope(compile_ops=False): d = tf.matmul(a, c) # not compiled with tf.xla.experimental.jit_scope( compile_ops=lambda node_def: 'matmul' in node_def.op.lower()): e = tf.matmul(a, b) + d # matmul is compiled, the addition is not. ``` Example of `separate_compiled_gradients`: ```python # In the example below, the computations for f, g and h will all be compiled # in separate scopes. with tf.xla.experimental.jit_scope( separate_compiled_gradients=True): f = tf.matmul(a, b) g = tf.gradients([f], [a, b], name='mygrads1') h = tf.gradients([f], [a, b], name='mygrads2') ``` Args: compile_ops: Whether to enable or disable compilation in the scope. Either a Python bool, or a callable that accepts the parameter `node_def` and returns a python bool. separate_compiled_gradients: If true put each gradient subgraph into a separate compilation scope. This gives fine-grained control over which portions of the graph will be compiled as a single unit. Compiling gradients separately may yield better performance for some graphs. The scope is named based on the scope of the forward computation as well as the name of the gradients. As a result, the gradients will be compiled in a scope that is separate from both the forward computation, and from other gradients. Raises: RuntimeError: if called when eager execution is enabled. Yields: The current scope, enabling or disabling compilation.
13241322enable_jit_nonstatefultensorflow/tensorflow/python/compiler/xla/jit_test.py39function
13251323compiletensorflow/tensorflow/python/compiler/xla/xla.py67functionBuilds an operator that compiles and runs `computation` with XLA. NOTE: In eager mode, `computation` will have `@tf.function` semantics. Args: computation: A Python function that builds a computation to apply to the input. If the function takes n inputs, 'inputs' should be a list of n tensors. `computation` may return a list of operations and tensors. Tensors must come before operations in the returned list. The return value of `compile` is a list of tensors corresponding to the tensors from the output of `computation`. All `Operation`s returned from `computation` will be executed when evaluating any of the returned output tensors. inputs: A list of inputs or `None` (equivalent to an empty list). Each input can be a nested structure containing values that are convertible to tensors. Note that passing an N-dimension list of compatible values will result in a N-dimension list of scalar tensors rather than a single Rank-N tensors. If you need different behavior, convert part of inputs to tensors with `tf.convert_to_tensor`. Returns: Same data structure as if computation(*inputs) is called directly with some exceptions for correctness. Exceptions include: 1) None output: a NoOp would be returned which control-depends on computation. 2) Single value output: A tuple containing the value would be returned. 3) Operation-only outputs: a NoOp would be returned which control-depends on computation. TODO(b/121383831): Investigate into removing these special cases. Raises: RuntimeError: if called when eager execution is enabled. Known issues: When a tf.random operation is built with XLA, the implementation doesn't pass the user provided seed to the XLA compiler. As such, the XLA compiler generates a random number and uses it as a seed when compiling the operation. This implementation causes a violation of the Tensorflow defined semantics in two aspects. First, changing the value of the user defined seed doesn't change the numbers generated by the operation. Second, when a seed is not specified, running the program multiple times will generate the same numbers.
13261324XLACompileContexttensorflow/tensorflow/python/compiler/xla/xla.py125classA `ControlFlowContext` for nodes inside an XLA computation cluster. THIS IS ONLY FOR TENSORFLOW INTERNAL IMPLEMENTATION, DO NO USE DIRECTLY. The primary role of `XLACompileContext` is to mark operators inside a xla.compile() computation with attribute "_xla_compile_id=XYZ", where XYZ is a unique name. `ControlFlowContext` is used to perform the annotation since it integrates with Tensorflow constructs like ResourceVariables. For example, if a `ResourceVariable` is constructed inside a xla.compile() block, the `ResourceVariable` implementation can use `with ops.control_dependencies(None)` to build the variable's definition outside the compiled computation.
13271325report_unsupported_operationstensorflow/tensorflow/python/compiler/xla/xla.py159method
13281326AddOptensorflow/tensorflow/python/compiler/xla/xla.py195methodCreate op in XLACompileContext and notifies outer context recursively.
13291327AddValuetensorflow/tensorflow/python/compiler/xla/xla.py268methodAdd `val` to the current context and its outer context recursively.
13301328AddInnerOptensorflow/tensorflow/python/compiler/xla/xla.py285method
13311329grad_statetensorflow/tensorflow/python/compiler/xla/xla.py291method
13321330back_proptensorflow/tensorflow/python/compiler/xla/xla.py299methodForwards to the enclosing while context, if any.
13331331is_flattensorflow/tensorflow/python/compiler/xla/xla.py409functionChecks if outputs is a flat structure. Following structures and values are considered flat: 1) None 2) A single object 3) A list or tuple of Tensors/Operations The only structures that this function understands are sequences, dictionaries and types defined using the attrs library. E.g. this means that if outputs contains a single user-defined Object, it is considered to be flat. Errors are raised later on if that Object cannot be converted to a Tensor. Args: outputs: Output from `computation` inside `xla.compile`. Returns: A boolean indicates whether outputs is flat.
13341332check_function_argument_counttensorflow/tensorflow/python/compiler/xla/xla.py591functionValidate the number of input arguments to an XLA function. Args: func: the Python function that will be called to generate the body of an XLA computation graph. input_arity: the number of explicit arguments supplied by the caller. infeed_queue: if not None, the infeed queue that will supply additional arguments to the function. Returns: None if function can be called with the supplied number of arguments, or an error string if it cannot.
13351333BatchBenchmarktensorflow/tensorflow/python/data/benchmarks/batch_benchmark.py27classBenchmarks for `tf.data.Dataset.batch()`.
13361334benchmark_batch_sparsetensorflow/tensorflow/python/data/benchmarks/batch_benchmark.py30method
13371335benchmark_batch_densetensorflow/tensorflow/python/data/benchmarks/batch_benchmark.py51method
13381336DatasetBenchmarkBasetensorflow/tensorflow/python/data/benchmarks/benchmark_base.py31classBase class for dataset benchmarks.
13391337run_benchmarktensorflow/tensorflow/python/data/benchmarks/benchmark_base.py34methodBenchmarks the dataset. Runs the dataset `iters` times. In each iteration, the benchmark measures the time it takes to go through `num_elements` elements of the dataset. Args: dataset: Dataset to benchmark. num_elements: Number of dataset elements to iterate through each benchmark iteration. iters: Number of times to repeat the timing. warmup: If true, warms up the session caches by running an untimed run. apply_default_optimizations: Determines whether default optimizations should be applied. Returns: A float, representing the per-element wall time of the dataset in seconds. This is the median time (with respect to `iters`) it takes for the dataset to go through `num_elements` elements, divided by `num_elements.`
13401338run_and_report_benchmarktensorflow/tensorflow/python/data/benchmarks/benchmark_base.py87method
13411339FilterBenchmarktensorflow/tensorflow/python/data/benchmarks/filter_benchmark.py26classBenchmarks for `tf.data.Dataset.filter()`.
13421340benchmark_simple_functiontensorflow/tensorflow/python/data/benchmarks/filter_benchmark.py34method
13431341benchmark_return_component_optimizationtensorflow/tensorflow/python/data/benchmarks/filter_benchmark.py37method
13441342SingleThreadedFlatMapDatasettensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py30classA `Dataset` that maps a function over its input and flattens the result.
13451343element_spectensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py54method
13461344FromTensorSlicesBenchmarktensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py62classBenchmarks for `tf.data.Dataset.from_tensor_slices()`.
13471345benchmark_slice_repeat_batchtensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py65method
13481346benchmark_reshape_slice_repeattensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py82method
13491347benchmark_slice_repeat_sparsetensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py101method
13501348benchmark_slice_batch_cache_repeattensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py132method
13511349make_datasettensorflow/tensorflow/python/data/benchmarks/from_tensor_slices_benchmark.py116method
13521350ListFilesBenchmarktensorflow/tensorflow/python/data/benchmarks/list_files_benchmark.py35classBenchmarks for `tf.data.Dataset.list_files()`.
13531351benchmark_nested_directoriestensorflow/tensorflow/python/data/benchmarks/list_files_benchmark.py38method
13541352MapBenchmarktensorflow/tensorflow/python/data/benchmarks/map_benchmark.py32classBenchmarks for `tf.data.Dataset.map()`.
13551353benchmark_chain_of_mapstensorflow/tensorflow/python/data/benchmarks/map_benchmark.py35method
13561354benchmark_map_fan_outtensorflow/tensorflow/python/data/benchmarks/map_benchmark.py53method
13571355benchmark_statstensorflow/tensorflow/python/data/benchmarks/map_benchmark.py72method
13581356benchmark_sequential_control_flowtensorflow/tensorflow/python/data/benchmarks/map_benchmark.py85method
13591357benchmark_parallel_control_flowtensorflow/tensorflow/python/data/benchmarks/map_benchmark.py103method
13601358benchmark_helpertensorflow/tensorflow/python/data/benchmarks/map_benchmark.py37method
13611359benchmark_helpertensorflow/tensorflow/python/data/benchmarks/map_benchmark.py56method
13621360fntensorflow/tensorflow/python/data/benchmarks/map_benchmark.py88method
13631361fntensorflow/tensorflow/python/data/benchmarks/map_benchmark.py107method
13641362bodytensorflow/tensorflow/python/data/benchmarks/map_benchmark.py91method
13651363MetaBenchmarktensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py31classBenchmark that compares various ways of running tf.data benchmarks.
13661364setup_fast_datasettensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py37method
13671365benchmark_fast_dataset_with_only_cpp_iterationstensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py44method
13681366benchmark_fast_dataset_with_session_runtensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py48method
13691367benchmark_fast_dataset_with_session_callabletensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py52method
13701368benchmark_fast_dataset_in_eagertensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py56method
13711369setup_slow_datasettensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py61method
13721370benchmark_slow_dataset_with_only_cpp_iterationstensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py67method
13731371benchmark_slow_dataset_with_session_runtensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py71method
13741372benchmark_slow_dataset_with_session_callabletensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py75method
13751373benchmark_slow_dataset_in_eagertensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py79method
13761374reporttensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py84method
13771375run_benchmark_in_eagertensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py105method
13781376run_benchmark_with_session_runtensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py113method
13791377run_benchmark_with_only_cpp_iterationstensorflow/tensorflow/python/data/benchmarks/meta_benchmark.py132methodBenchmarks the dataset with the iterations performed in C++.
13801378PrefetchBenchmarktensorflow/tensorflow/python/data/benchmarks/prefetch_benchmark.py24classBenchmarks for `tf.data.Dataset.prefetch()`.
13811379benchmark_prefetchtensorflow/tensorflow/python/data/benchmarks/prefetch_benchmark.py27method
13821380RangeBenchmarktensorflow/tensorflow/python/data/benchmarks/range_benchmark.py24classBenchmarks for `tf.data.Dataset.range()`.
13831381benchmark_rangetensorflow/tensorflow/python/data/benchmarks/range_benchmark.py27method
13841382AutotuneBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py31classBenchmarks for autotuning performance knobs.
13851383benchmark_maptensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py66method
13861384benchmark_map_and_batchtensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py87method
13871385benchmark_interleavetensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py110method
13881386benchmark_map_and_interleavetensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py134method
13891387benchmark_map_batch_and_interleavetensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py182method
13901388f1tensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py152method
13911389f2tensorflow/tensorflow/python/data/experimental/benchmarks/autotune_benchmark.py155method
13921390CsvDatasetBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/csv_dataset_benchmark.py38classBenchmarks for `tf.data.experimental.CsvDataset`.
13931391benchmark_map_with_floatstensorflow/tensorflow/python/data/experimental/benchmarks/csv_dataset_benchmark.py90method
13941392benchmark_map_with_stringstensorflow/tensorflow/python/data/experimental/benchmarks/csv_dataset_benchmark.py100method
13951393benchmark_csv_dataset_with_floatstensorflow/tensorflow/python/data/experimental/benchmarks/csv_dataset_benchmark.py110method
13961394benchmark_csv_dataset_with_stringstensorflow/tensorflow/python/data/experimental/benchmarks/csv_dataset_benchmark.py120method
13971395MapAndBatchBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py40classBenchmarks for `tf.data.experimental.map_and_batch()`.
13981396benchmark_map_and_batchtensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py43methodMeasures the performance of parallelized batching.
13991397benchmark_map_and_batch_chaining_versus_fusingtensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py97methodCompares the performance of chaining and fusing map and batch. NOTE: It is recommended to build the benchmark with `-c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-gmlt` and execute it on a machine with at least 32 CPU cores.
14001398nametensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py116method
14011399benchmarktensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py126methodRuns benchmark the given series.
14021400make_datasettensorflow/tensorflow/python/data/experimental/benchmarks/map_and_batch_benchmark.py129method
14031401MapDefunBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/map_defun_benchmark.py34classBenchmarks for MapDefunOp.
14041402benchmark_defun_vs_map_fntensorflow/tensorflow/python/data/experimental/benchmarks/map_defun_benchmark.py52methodBenchmarks to compare the performance of MapDefun vs tf.map_fn.
14051403defuntensorflow/tensorflow/python/data/experimental/benchmarks/map_defun_benchmark.py56method
14061404fntensorflow/tensorflow/python/data/experimental/benchmarks/map_defun_benchmark.py59method
14071405MapVectorizationBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py97classBenchmarks for the `MapVectorization` optimization.
14081406benchmark_identitytensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py147method
14091407benchmark_add_consttensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py151method
14101408benchmark_return_consttensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py154method
14111409benchmark_selecttensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py157method
14121410benchmark_casttensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py160method
14131411benchmark_reshapetensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py164method
14141412benchmark_decode_csvtensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py168method
14151413benchmark_parse_single_exampletensorflow/tensorflow/python/data/experimental/benchmarks/map_vectorization_benchmark.py172method
14161414MatchingFilesBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/matching_files_benchmark.py35classBenchmark for the experimental `MatchingFilesDataset`.
14171415benchmark_nested_directoriestensorflow/tensorflow/python/data/experimental/benchmarks/matching_files_benchmark.py38method
14181416OptimizationBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/optimize_benchmark.py32classBenchmarks for static optimizations.
14191417benchmark_map_fusiontensorflow/tensorflow/python/data/experimental/benchmarks/optimize_benchmark.py35methodEvaluates performance map of fusion.
14201418benchmark_map_and_filter_fusiontensorflow/tensorflow/python/data/experimental/benchmarks/optimize_benchmark.py76methodEvaluates performance map of fusion.
14211419benchmark_filter_fusiontensorflow/tensorflow/python/data/experimental/benchmarks/optimize_benchmark.py119method
14221420ParallelInterleaveBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py68classBenchmarks for `tf.data.experimental.parallel_interleave()`.
14231421apply_interleavetensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py71method
14241422make_datasettensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py89method
14251423benchmark_remote_file_simulationtensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py126method
14261424benchmark_fast_inputtensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py135method
14271425benchmark_single_cycletensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py142method
14281426benchmark_single_parallel_calltensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py152method
14291427benchmark_long_cycletensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py159method
14301428benchmark_statstensorflow/tensorflow/python/data/experimental/benchmarks/parallel_interleave_benchmark.py167method
14311429RejectionResampleBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/rejection_resample_benchmark.py56classBenchmarks for `tf.data.experimental.rejection_resample()`.
14321430benchmark_resample_performancetensorflow/tensorflow/python/data/experimental/benchmarks/rejection_resample_benchmark.py59method
14331431SnapshotDatasetBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py34classBenchmarks for `tf.data.experimental.snapshot()`.
14341432benchmarkWriteSnapshotGzipCompressiontensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py68method
14351433benchmarkWriteSnapshotSnappyCompressiontensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py76method
14361434benchmarkWriteSnapshotSimpletensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py84method
14371435benchmarkPassthroughSnapshotSimpletensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py94method
14381436benchmarkReadSnapshotSimpletensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py104method
14391437benchmarkReadSnapshotGzipCompressiontensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py114method
14401438benchmarkReadSnapshotSnappyCompressiontensorflow/tensorflow/python/data/experimental/benchmarks/snapshot_dataset_benchmark.py123method
14411439UnbatchBenchmarktensorflow/tensorflow/python/data/experimental/benchmarks/unbatch_benchmark.py32classBenchmarks for `tf.data.Dataset.unbatch()`.
14421440benchmark_native_unbatchtensorflow/tensorflow/python/data/experimental/benchmarks/unbatch_benchmark.py35method
14431441benchmark_old_unbatch_implementationtensorflow/tensorflow/python/data/experimental/benchmarks/unbatch_benchmark.py72method
14441442chunktensorflow/tensorflow/python/data/experimental/kernel_tests/auto_shard_dataset_test.py46function
14451443remove_variantstensorflow/tensorflow/python/data/experimental/kernel_tests/serialization/dataset_serialization_test_base.py41functionRemove variants from a nest structure, so sess.run will execute.
14461444dense_to_ragged_batchtensorflow/tensorflow/python/data/experimental/ops/batching.py36functionA transformation that batches ragged elements into `tf.RaggedTensor`s. This transformation combines multiple consecutive elements of the input dataset into a single element. Like `tf.data.Dataset.batch`, the components of the resulting element will have an additional outer dimension, which will be `batch_size` (or `N % batch_size` for the last element if `batch_size` does not divide the number of input elements `N` evenly and `drop_remainder` is `False`). If your program depends on the batches having the same outer dimension, you should set the `drop_remainder` argument to `True` to prevent the smaller batch from being produced. Unlike `tf.data.Dataset.batch`, the input elements to be batched may have different shapes: * If an input element is a `tf.Tensor` whose static `tf.TensorShape` is fully defined, then it is batched as normal. * If an input element is a `tf.Tensor` whose static `tf.TensorShape` contains one or more axes with unknown size (i.e., `shape[i]=None`), then the output will contain a `tf.RaggedTensor` that is ragged up to any of such dimensions. * If an input element is a `tf.RaggedTensor` or any other type, then it is batched as normal. Example: >>> dataset = tf.data.Dataset.from_tensor_slices(np.arange(6)) >>> dataset = dataset.map(lambda x: tf.range(x)) >>> dataset.element_spec.shape TensorShape([None]) >>> dataset = dataset.apply( ... tf.data.experimental.dense_to_ragged_batch(batch_size=2)) >>> for batch in dataset: ... print(batch) <tf.RaggedTensor [[], [0]]> <tf.RaggedTensor [[0, 1], [0, 1, 2]]> <tf.RaggedTensor [[0, 1, 2, 3], [0, 1, 2, 3, 4]]> Args: batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing whether the last batch should be dropped in the case it has fewer than `batch_size` elements; the default behavior is not to drop the smaller batch. row_splits_dtype: The dtype that should be used for the `row_splits` of any new ragged tensors. Existing `tf.RaggedTensor` elements do not have their row_splits dtype changed. Returns: Dataset: A `Dataset`.
14471445dense_to_sparse_batchtensorflow/tensorflow/python/data/experimental/ops/batching.py102functionA transformation that batches ragged elements into `tf.sparse.SparseTensor`s. Like `Dataset.padded_batch()`, this transformation combines multiple consecutive elements of the dataset, which might have different shapes, into a single element. The resulting element has three components (`indices`, `values`, and `dense_shape`), which comprise a `tf.sparse.SparseTensor` that represents the same data. The `row_shape` represents the dense shape of each row in the resulting `tf.sparse.SparseTensor`, to which the effective batch size is prepended. For example: ```python # NOTE: The following examples use `{ ... }` to represent the # contents of a dataset. a = { ['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c', 'd'] } a.apply(tf.data.experimental.dense_to_sparse_batch( batch_size=2, row_shape=[6])) == { ([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1]], # indices ['a', 'b', 'c', 'a', 'b'], # values [2, 6]), # dense_shape ([[0, 0], [0, 1], [0, 2], [0, 3]], ['a', 'b', 'c', 'd'], [1, 6]) } ``` Args: batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. row_shape: A `tf.TensorShape` or `tf.int64` vector tensor-like object representing the equivalent dense shape of a row in the resulting `tf.sparse.SparseTensor`. Each element of this dataset must have the same rank as `row_shape`, and must have size less than or equal to `row_shape` in each dimension. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14481446map_and_batch_with_legacy_functiontensorflow/tensorflow/python/data/experimental/ops/batching.py153functionFused implementation of `map` and `batch`. NOTE: This is an escape hatch for existing uses of `map_and_batch` that do not work with V2 functions. New uses are strongly discouraged and existing uses should migrate to `map_and_batch` as this method will not be removed in V2. Args: map_func: A function mapping a nested structure of tensors to another nested structure of tensors. batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. num_parallel_batches: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the number of batches to create in parallel. On one hand, higher values can help mitigate the effect of stragglers. On the other hand, higher values can increase contention if CPU is scarce. drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing whether the last batch should be dropped in case its size is smaller than desired; the default behavior is not to drop the smaller batch. num_parallel_calls: (Optional.) A `tf.int32` scalar `tf.Tensor`, representing the number of elements to process in parallel. If not specified, `batch_size * num_parallel_batches` elements will be processed in parallel. If the value `tf.data.experimental.AUTOTUNE` is used, then the number of parallel calls is set dynamically based on available CPU. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: ValueError: If both `num_parallel_batches` and `num_parallel_calls` are specified.
14491447map_and_batchtensorflow/tensorflow/python/data/experimental/ops/batching.py213functionFused implementation of `map` and `batch`. Maps `map_func` across `batch_size` consecutive elements of this dataset and then combines them into a batch. Functionally, it is equivalent to `map` followed by `batch`. This API is temporary and deprecated since input pipeline optimization now fuses consecutive `map` and `batch` operations automatically. Args: map_func: A function mapping a nested structure of tensors to another nested structure of tensors. batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. num_parallel_batches: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the number of batches to create in parallel. On one hand, higher values can help mitigate the effect of stragglers. On the other hand, higher values can increase contention if CPU is scarce. drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing whether the last batch should be dropped in case its size is smaller than desired; the default behavior is not to drop the smaller batch. num_parallel_calls: (Optional.) A `tf.int32` scalar `tf.Tensor`, representing the number of elements to process in parallel. If not specified, `batch_size * num_parallel_batches` elements will be processed in parallel. If the value `tf.data.experimental.AUTOTUNE` is used, then the number of parallel calls is set dynamically based on available CPU. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: ValueError: If both `num_parallel_batches` and `num_parallel_calls` are specified.
14501448unbatchtensorflow/tensorflow/python/data/experimental/ops/batching.py269functionSplits elements of a dataset into multiple elements on the batch dimension. For example, if elements of the dataset are shaped `[B, a0, a1, ...]`, where `B` may vary for each input element, then for each element in the dataset, the unbatched dataset will contain `B` consecutive elements of shape `[a0, a1, ...]`. ```python # NOTE: The following example uses `{ ... }` to represent the contents # of a dataset. a = { ['a', 'b', 'c'], ['a', 'b'], ['a', 'b', 'c', 'd'] } a.unbatch() == { 'a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'd'} ``` Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14511449cardinalitytensorflow/tensorflow/python/data/experimental/ops/cardinality.py38functionReturns the cardinality of `dataset`, if known. The operation returns the cardinality of `dataset`. The operation may return `tf.data.experimental.INFINITE_CARDINALITY` if `dataset` contains an infinite number of elements or `tf.data.experimental.UNKNOWN_CARDINALITY` if the analysis fails to determine the number of elements in `dataset` (e.g. when the dataset source is a file). >>> dataset = tf.data.Dataset.range(42) >>> print(tf.data.experimental.cardinality(dataset).numpy()) 42 >>> dataset = dataset.repeat() >>> cardinality = tf.data.experimental.cardinality(dataset) >>> print((cardinality == tf.data.experimental.INFINITE_CARDINALITY).numpy()) True >>> dataset = dataset.filter(lambda x: True) >>> cardinality = tf.data.experimental.cardinality(dataset) >>> print((cardinality == tf.data.experimental.UNKNOWN_CARDINALITY).numpy()) True Args: dataset: A `tf.data.Dataset` for which to determine cardinality. Returns: A scalar `tf.int64` `Tensor` representing the cardinality of `dataset`. If the cardinality is infinite or unknown, the operation returns the named constant `INFINITE_CARDINALITY` and `UNKNOWN_CARDINALITY` respectively.
14521450assert_cardinalitytensorflow/tensorflow/python/data/experimental/ops/cardinality.py72functionAsserts the cardinality of the input dataset. NOTE: The following assumes that "examples.tfrecord" contains 42 records. >>> dataset = tf.data.TFRecordDataset("examples.tfrecord") >>> cardinality = tf.data.experimental.cardinality(dataset) >>> print((cardinality == tf.data.experimental.UNKNOWN_CARDINALITY).numpy()) True >>> dataset = dataset.apply(tf.data.experimental.assert_cardinality(42)) >>> print(tf.data.experimental.cardinality(dataset).numpy()) 42 Args: expected_cardinality: The expected cardinality of the input dataset. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: FailedPreconditionError: The assertion is checked at runtime (when iterating the dataset) and an error is raised if the actual and expected cardinality differ.
14531451compresstensorflow/tensorflow/python/data/experimental/ops/compression_ops.py24functionCompress a dataset element. Args: element: A nested structure of types supported by Tensorflow. Returns: A variant tensor representing the compressed element. This variant can be passed to `uncompress` to get back the original element.
14541452uncompresstensorflow/tensorflow/python/data/experimental/ops/compression_ops.py39functionUncompress a compressed dataset element. Args: element: A scalar variant tensor to uncompress. The element should have been created by calling `compress`. output_spec: A nested structure of `tf.TypeSpec` representing the type(s) of the uncompressed element. Returns: The uncompressed element.
14551453CounterV2tensorflow/tensorflow/python/data/experimental/ops/counter.py29functionCreates a `Dataset` that counts from `start` in steps of size `step`. For example: ```python Dataset.count() == [0, 1, 2, ...) Dataset.count(2) == [2, 3, ...) Dataset.count(2, 5) == [2, 7, 12, ...) Dataset.count(0, -1) == [0, -1, -2, ...) Dataset.count(10, -1) == [10, 9, ...) ``` Args: start: (Optional.) The starting value for the counter. Defaults to 0. step: (Optional.) The step size for the counter. Defaults to 1. dtype: (Optional.) The data type for counter elements. Defaults to `tf.int64`. Returns: A `Dataset` of scalar `dtype` elements.
14561454CounterV1tensorflow/tensorflow/python/data/experimental/ops/counter.py59function
14571455ProcessingModetensorflow/tensorflow/python/data/experimental/ops/data_service_ops.py36class
14581456validatetensorflow/tensorflow/python/data/experimental/ops/data_service_ops.py40methodRaises a ValueError if the given object is not a valid processing mode.
14591457distributetensorflow/tensorflow/python/data/experimental/ops/data_service_ops.py295functionA transformation that moves dataset processing to the tf.data service. When you iterate over a dataset containing the `distribute` transformation, the tf.data service creates a "job" which produces data for the dataset iteration. The `processing_mode` argument controls what data is produced by a tf.data service job. Currently, the only supported mode is "parallel_epochs". processing_mode="parallel_epochs" means that multiple tf.data workers will iterate through the dataset in parallel, each producing all elements of the dataset. For example, if the dataset contains {0, 1, 2}, every tf.data worker used for execution will produce {0, 1, 2}. If there are 3 workers, the job will produce the elements {0, 0, 0, 1, 1, 1, 2, 2, 2} (though not necessarily in that order). To account for this, it is recommended to randomly shuffle your dataset, so that different tf.data workers will iterate through the dataset in different orders. In the future, there will be additional processing modes. For example, a "one_epoch" mode which partitions the dataset across the tf.data workers, so that the consumers see each element of the dataset only once. ``` dataset = tf.data.Dataset.range(5) dataset = dataset.map(lambda x: x*x) dataset = dataset.apply( tf.data.experimental.service.distribute("parallel_epochs", "grpc://dataservice:5000")) dataset = dataset.map(lambda x: x+1) for element in dataset: print(element) # prints { 1, 2, 5, 10, 17 } ``` In the above example, the first two lines (before the call to `distribute`) will be executed on tf.data workers, and the elements provided over RPC. The remaining transformations (after the call to `distribute`) will be executed locally. The `job_name` argument allows jobs to be shared across multiple datasets. Instead of each dataset creating its own job, all datasets with the same `job_name` will consume from the same job. A new job will be created for each iteration of the dataset (with each repetition of `Dataset.repeat` counting as a new iteration). Suppose two training workers (in either a single client or multi-client setup) iterate over the below dataset, and there is a single tf.data worker: ``` range5_dataset = tf.data.Dataset.range(5) dataset = range5_dataset.apply(tf.data.experimental.service.distribute( "parallel_epochs", "grpc://dataservice:5000", job_name="my_job_name")) for iteration in range(3): print(list(dataset)) ``` The elements of each job will be split between the two processes, with elements being consumed by the processes on a first-come first-served basis. One possible result is that process 1 prints ``` [0, 2, 4] [0, 1, 3] [1] ``` and process 2 prints ``` [1, 3] [2, 4] [0, 2, 3, 4] ``` Job names must not be re-used across different training jobs within the lifetime of the tf.data service. In general, the tf.data service is expected to live for the duration of a single training job. To use the tf.data service with multiple training jobs, make sure to use different job names to avoid conflicts. For example, suppose a training job calls `distribute` with `job_name="job"` and reads until end of input. If another independent job connects to the same tf.data service and tries to read from `job_name="job"`, it will immediately receive end of input, without getting any data. **Keras and Distribution Strategies** The dataset produced by the `distribute` transformation can be passed to Keras' `Model.fit` or Distribution Strategy's `tf.distribute.Strategy.experimental_distribute_dataset` like any other `tf.data.Dataset`. We recommend setting a `job_name` on the call to `distribute` so that if there are multiple workers, they read data from the same job. Note that the autosharding normally performed by `experimental_distribute_dataset` will be disabled when setting a `job_name`, since sharing the job already results in splitting data across the workers. When using a shared job, data will be dynamically balanced across workers, so that they reach end of input about the same time. This results in better worker utilization than with autosharding, where each worker processes an independent set of files, and some workers may run out of data earlier than others. Args: processing_mode: A string specifying the policy for how data should be processed by tf.data workers. Currently, the only supported value is "parallel_epochs". service: A string indicating how to connect to the tf.data service. The string should be in the format "protocol://address", e.g. "grpc://localhost:5000". job_name: (Optional.) The name of the job. This argument makes it possible for multiple datasets to share the same job. The default behavior is that the dataset creates anonymous, exclusively owned jobs. max_outstanding_requests: (Optional.) A limit on how many elements may be requested at the same time. You can use this option to control the amount of memory used, since `distribute` won't use more than `element_size` * `max_outstanding_requests` of memory. Returns: Dataset: A `Dataset` of the elements produced by the data service.
14601458register_datasettensorflow/tensorflow/python/data/experimental/ops/data_service_ops.py424functionRegisters a dataset with the tf.data service. `register_dataset` registers a dataset with the tf.data service so that datasets can be created later with `tf.data.experimental.service.from_dataset_id`. This is useful when the dataset is registered by one process, then used in another process. When the same process is both registering and reading from the dataset, it is simpler to use `tf.data.experimental.service.distribute` instead. If the dataset is already registered with the tf.data service, `register_dataset` returns the already-registered dataset's id. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0) >>> dispatcher_address = dispatcher.target.split("://")[1] >>> worker = tf.data.experimental.service.WorkerServer( ... port=0, dispatcher_address=dispatcher_address) >>> dataset = tf.data.Dataset.range(10) >>> dataset_id = tf.data.experimental.service.register_dataset( ... dispatcher.target, dataset) >>> dataset = tf.data.experimental.service.from_dataset_id( ... processing_mode="parallel_epochs", ... service=dispatcher.target, ... dataset_id=dataset_id, ... element_spec=dataset.element_spec) >>> print(list(dataset.as_numpy_iterator())) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Args: service: A string indicating how to connect to the tf.data service. The string should be in the format "protocol://address", e.g. "grpc://localhost:5000". dataset: A `tf.data.Dataset` to register with the tf.data service. Returns: A scalar int64 tensor of the registered dataset's id.
14611459from_dataset_idtensorflow/tensorflow/python/data/experimental/ops/data_service_ops.py491functionCreates a dataset which reads data from the tf.data service. This is useful when the dataset is registered by one process, then used in another process. When the same process is both registering and reading from the dataset, it is simpler to use `tf.data.experimental.service.distribute` instead. Before using `from_dataset_id`, the dataset must have been registered with the tf.data service using `tf.data.experimental.service.register_dataset`. `register_dataset` returns a dataset id for the registered dataset. That is the `dataset_id` which should be passed to `from_dataset_id`. The `element_spec` argument indicates the `tf.TypeSpec`s for the elements produced by the dataset. Currently `element_spec` must be explicitly specified, and match the dataset registered under `dataset_id`. `element_spec` defaults to `None` so that in the future we can support automatically discovering the `element_spec` by querying the tf.data service. `tf.data.experimental.service.distribute` is a convenience method which combines `register_dataset` and `from_dataset_id` into a dataset transformation. See the documentation for `tf.data.experimental.service.distribute` for more detail about how `from_dataset_id` works. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0) >>> dispatcher_address = dispatcher.target.split("://")[1] >>> worker = tf.data.experimental.service.WorkerServer( ... port=0, dispatcher_address=dispatcher_address) >>> dataset = tf.data.Dataset.range(10) >>> dataset_id = tf.data.experimental.service.register_dataset( ... dispatcher.target, dataset) >>> dataset = tf.data.experimental.service.from_dataset_id( ... processing_mode="parallel_epochs", ... service=dispatcher.target, ... dataset_id=dataset_id, ... element_spec=dataset.element_spec) >>> print(list(dataset.as_numpy_iterator())) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Args: processing_mode: A string specifying the policy for how data should be processed by tf.data workers. Currently, the only supported value is "parallel_epochs". service: A string indicating how to connect to the tf.data service. The string should be in the format "protocol://address", e.g. "grpc://localhost:5000". dataset_id: The id of the dataset to read from. This id is returned by `register_dataset` when the dataset is registered with the tf.data service. element_spec: A nested structure of `tf.TypeSpec`s representing the type of elements produced by the dataset. Use `tf.data.Dataset.element_spec` to see the element spec for a given dataset. job_name: (Optional.) The name of the job. This argument makes it possible for multiple datasets to share the same job. The default behavior is that the dataset creates anonymous, exclusively owned jobs. max_outstanding_requests: (Optional.) A limit on how many elements may be requested at the same time. You can use this option to control the amount of memory used, since `distribute` won't use more than `element_size` * `max_outstanding_requests` of memory. Returns: A `tf.data.Dataset` which reads from the tf.data service.
14621460replicatetensorflow/tensorflow/python/data/experimental/ops/distribute.py294functionA transformation that replicates `dataset` onto a list of devices. Args: dataset: A `tf.data.Dataset` object. devices: A list of devices to replicate the dataset on. Returns: A dictionary mapping device name to a dataset on that device.
14631461batch_sizes_for_workertensorflow/tensorflow/python/data/experimental/ops/distribute.py328functionDetermines how to rebatch a dataset for the given worker. Given the global batch size, number of workers, number of replicas per worker, and worker index, returns the correct batch sizes for rebatching a dataset on worker `worker_index` of `num_workers`, such that each global step (across all workers and replicas) will consume global_batch_size elements. The returned value should be passed as the `batch_sizes` input parameter to `tf.data.experimental.rebatch()`. The returned batch sizes meet the following constraints: Let G = global_batch_size, W = num_workers, R = num_replicas_per_worker (A) for any worker, len(batch_sizes) = W * R (B) for any worker, sum(batch_sizes) == G (C) for any global step (i.e. R iterations on each worker), the sum of batches consumed by replicas across all workers is G. (D) any two batch sizes of any two replicas differs by at most one. For example, suppose we have G = 7, W = 2, R = 2, and suppose we have two files which each contain 7 elements: ```python # WORKER 0 batch_sizes_0 = batch_sizes_for_worker(global_batch_size=global_batch_size, num_workers=2, num_replicas_per_worker=2, worker_index=0) print(batch_sizes_0) >> [2, 2, 2, 1] dataset_0 = tf.data.Dataset.from_tensor_slices(["file_a", "file_b"]) dataset_0 = dataset_0.shard(num_shards, index=0) dataset_0 = dataset_0.batch(7) dataset_0 = dataset_0.apply(tf.data.experimental.rebatch(batch_sizes_0)) for elem in dataset_0: print(elem) >> [[A0, A1], [A2, A3], [A4, A5], [A6]] # WORKER 1 batch_sizes_1 = batch_sizes_for_worker(global_batch_size=global_batch_size, num_workers=2, num_replicas_per_worker=2, worker_index=1) print(batch_sizes_1) >> [2, 1, 2, 2] dataset_1 = tf.data.Dataset.from_tensor_slices(["file_a", "file_b"]) dataset_1 = dataset_1.shard(num_shards, index=1) dataset_1 = dataset_1.batch(7) dataset_1 = dataset_1.apply(tf.data.experimental.rebatch(batch_sizes_1)) for elem in dataset_1: print(elem) >> [[B0, B1], [B2], [B3, B4], [B5, B6]] ``` The above example will produce the following elements: Step 1: Worker 0 Replica 0: [A0, A1] Worker 0 Replica 1: [A2, A3] Worker 1 Replica 0: [B0, B1] Worker 1 Replica 1: [B2] Total batch size = 7 Step 2: Worker 0 Replica 0: [A4, A5] Worker 0 Replica 1: [A6] Worker 1 Replica 0: [B3, B4] Worker 1 Replica 1: [B5, B6] Total batch size = 7 Args: global_batch_size: A `tf.int64` scalar, representing the global batch size. num_workers: An integer representing the number of workers the dataset will be distributed across. num_replicas_per_worker: An integer representing the number of replicas per worker. All workers are assumed to have the same number of replicas. worker_index: An integer index of the worker to be rebatched. Returns: A `tf.int64` vector, representing the batch sizes to rebatch the dataset into.
14641462compute_batch_sizetensorflow/tensorflow/python/data/experimental/ops/distribute.py436functionAn operation that returns the batch size of the dataset. This op tries to infer the batch size statically by walking up the dataset tree from the final dataset node and returning the batch size of the first batching dataset (such as from .batch() and .padded_batch()) that it encounters. This differs from using the `element_spec` of a dataset in that it does not account for partial batches. This operation may fail if it encounters contradictory batch sizes (for example, if the dataset is created by zipping together two datasets with different batch sizes), if there are no explicit batching transformations, or if there are operations downstream from the batching transformation that may modify its batch size. In these cases, it returns a -1. Args: dataset: A `tf.data.Dataset` object. Returns: A `tf.int64` Tensor representing the batch size of the dataset sans partial batches. If this cannot be inferred statically, the value of this tensor will be -1.
14651463AutoShardPolicytensorflow/tensorflow/python/data/experimental/ops/distribute_options.py27classRepresents the type of auto-sharding we enable. Please see the DistributeOptions.auto_shard_policy documentation for more information on each type of autosharding.
14661464ExternalStatePolicytensorflow/tensorflow/python/data/experimental/ops/distribute_options.py39class
14671465DistributeOptionstensorflow/tensorflow/python/data/experimental/ops/distribute_options.py46classRepresents options for distributed data processing. You can set the distribution options of a dataset through the `experimental_distribute` property of `tf.data.Options`; the property is an instance of `tf.data.experimental.DistributeOptions`. ```python options = tf.data.Options() options.experimental_distribute.auto_shard_policy = AutoShardPolicy.OFF dataset = dataset.with_options(options) ```
14681466enumerate_datasettensorflow/tensorflow/python/data/experimental/ops/enumerate_ops.py26functionA transformation that enumerates the elements of a dataset. It is similar to python's `enumerate`. For example: ```python # NOTE: The following examples use `{ ... }` to represent the # contents of a dataset. a = { 1, 2, 3 } b = { (7, 8), (9, 10) } # The nested structure of the `datasets` argument determines the # structure of elements in the resulting dataset. a.apply(tf.data.experimental.enumerate_dataset(start=5)) => { (5, 1), (6, 2), (7, 3) } b.apply(tf.data.experimental.enumerate_dataset()) => { (0, (7, 8)), (1, (9, 10)) } ``` Args: start: A `tf.int64` scalar `tf.Tensor`, representing the start value for enumeration. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14691467ignore_errorstensorflow/tensorflow/python/data/experimental/ops/error_ops.py26functionCreates a `Dataset` from another `Dataset` and silently ignores any errors. Use this transformation to produce a dataset that contains the same elements as the input, but silently drops any elements that caused an error. For example: ```python dataset = tf.data.Dataset.from_tensor_slices([1., 2., 0., 4.]) # Computing `tf.debugging.check_numerics(1. / 0.)` will raise an InvalidArgumentError. dataset = dataset.map(lambda x: tf.debugging.check_numerics(1. / x, "error")) # Using `ignore_errors()` will drop the element that causes an error. dataset = dataset.apply(tf.data.experimental.ignore_errors()) # ==> {1., 0.5, 0.2} ``` Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14701468get_single_elementtensorflow/tensorflow/python/data/experimental/ops/get_single_element.py27functionReturns the single element in `dataset` as a nested structure of tensors. This function enables you to use a `tf.data.Dataset` in a stateless "tensor-in tensor-out" expression, without creating an iterator. This can be useful when your preprocessing transformations are expressed as a `Dataset`, and you want to use the transformation at serving time. For example: ```python def preprocessing_fn(input_str): # ... return image, label input_batch = ... # input batch of BATCH_SIZE elements dataset = (tf.data.Dataset.from_tensor_slices(input_batch) .map(preprocessing_fn, num_parallel_calls=BATCH_SIZE) .batch(BATCH_SIZE)) image_batch, label_batch = tf.data.experimental.get_single_element(dataset) ``` Args: dataset: A `tf.data.Dataset` object containing a single element. Returns: A nested structure of `tf.Tensor` objects, corresponding to the single element of `dataset`. Raises: TypeError: if `dataset` is not a `tf.data.Dataset` object. InvalidArgumentError (at runtime): if `dataset` does not contain exactly one element.
14711469group_by_reducertensorflow/tensorflow/python/data/experimental/ops/grouping.py38functionA transformation that groups elements and performs a reduction. This transformation maps element of a dataset to a key using `key_func` and groups the elements by key. The `reducer` is used to process each group; its `init_func` is used to initialize state for each group when it is created, the `reduce_func` is used to update the state every time an element is mapped to the matching group, and the `finalize_func` is used to map the final state to an output value. Args: key_func: A function mapping a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to a scalar `tf.int64` tensor. reducer: An instance of `Reducer`, which captures the reduction logic using the `init_func`, `reduce_func`, and `finalize_func` functions. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14721470group_by_windowtensorflow/tensorflow/python/data/experimental/ops/grouping.py68functionA transformation that groups windows of elements by key and reduces them. This transformation maps each consecutive element in a dataset to a key using `key_func` and groups the elements by key. It then applies `reduce_func` to at most `window_size_func(key)` elements matching the same key. All except the final window for each key will contain `window_size_func(key)` elements; the final window may be smaller. You may provide either a constant `window_size` or a window size determined by the key through `window_size_func`. Args: key_func: A function mapping a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to a scalar `tf.int64` tensor. reduce_func: A function mapping a key and a dataset of up to `window_size` consecutive elements matching that key to another dataset. window_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements matching the same key to combine in a single batch, which will be passed to `reduce_func`. Mutually exclusive with `window_size_func`. window_size_func: A function mapping a key to a `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements matching the same key to combine in a single batch, which will be passed to `reduce_func`. Mutually exclusive with `window_size`. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: ValueError: if neither or both of {`window_size`, `window_size_func`} are passed.
14731471bucket_by_sequence_lengthtensorflow/tensorflow/python/data/experimental/ops/grouping.py128functionA transformation that buckets elements in a `Dataset` by length. Elements of the `Dataset` are grouped together by length and then are padded and batched. This is useful for sequence tasks in which the elements have variable length. Grouping together elements that have similar lengths reduces the total fraction of padding in a batch which increases training step efficiency. Args: element_length_func: function from element in `Dataset` to `tf.int32`, determines the length of the element, which will determine the bucket it goes into. bucket_boundaries: `list<int>`, upper length boundaries of the buckets. bucket_batch_sizes: `list<int>`, batch size per bucket. Length should be `len(bucket_boundaries) + 1`. padded_shapes: Nested structure of `tf.TensorShape` to pass to `tf.data.Dataset.padded_batch`. If not provided, will use `dataset.output_shapes`, which will result in variable length dimensions being padded out to the maximum length in each batch. padding_values: Values to pad with, passed to `tf.data.Dataset.padded_batch`. Defaults to padding with 0. pad_to_bucket_boundary: bool, if `False`, will pad dimensions with unknown size to maximum length in batch. If `True`, will pad dimensions with unknown size to bucket boundary minus 1 (i.e., the maximum length in each bucket), and caller must ensure that the source `Dataset` does not contain any elements with length longer than `max(bucket_boundaries)`. no_padding: `bool`, indicates whether to pad the batch features (features need to be either of type `tf.sparse.SparseTensor` or of same shape). drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing whether the last batch should be dropped in the case it has fewer than `batch_size` elements; the default behavior is not to drop the smaller batch. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: ValueError: if `len(bucket_batch_sizes) != len(bucket_boundaries) + 1`.
14741472Reducertensorflow/tensorflow/python/data/experimental/ops/grouping.py443classA reducer is used for reducing a set of elements. A reducer is represented as a tuple of the three functions: 1) initialization function: key => initial state 2) reduce function: (old state, input) => new state 3) finalization function: state => result
14751473init_functensorflow/tensorflow/python/data/experimental/ops/grouping.py458method
14761474reduce_functensorflow/tensorflow/python/data/experimental/ops/grouping.py462method
14771475finalize_functensorflow/tensorflow/python/data/experimental/ops/grouping.py466method
14781476parallel_interleavetensorflow/tensorflow/python/data/experimental/ops/interleave_ops.py43functionA parallel version of the `Dataset.interleave()` transformation. `parallel_interleave()` maps `map_func` across its input to produce nested datasets, and outputs their elements interleaved. Unlike `tf.data.Dataset.interleave`, it gets elements from `cycle_length` nested datasets in parallel, which increases the throughput, especially in the presence of stragglers. Furthermore, the `sloppy` argument can be used to improve performance, by relaxing the requirement that the outputs are produced in a deterministic order, and allowing the implementation to skip over nested datasets whose elements are not readily available when requested. Example usage: ```python # Preprocess 4 files concurrently. filenames = tf.data.Dataset.list_files("/path/to/data/train*.tfrecords") dataset = filenames.apply( tf.data.experimental.parallel_interleave( lambda filename: tf.data.TFRecordDataset(filename), cycle_length=4)) ``` WARNING: If `sloppy` is `True`, the order of produced elements is not deterministic. Args: map_func: A function mapping a nested structure of tensors to a `Dataset`. cycle_length: The number of input `Dataset`s to interleave from in parallel. block_length: The number of consecutive elements to pull from an input `Dataset` before advancing to the next input `Dataset`. sloppy: A boolean controlling whether determinism should be traded for performance by allowing elements to be produced out of order. If `sloppy` is `None`, the `tf.data.Options.experimental_deterministic` dataset option (`True` by default) is used to decide whether to enforce a deterministic order. buffer_output_elements: The number of elements each iterator being interleaved should buffer (similar to the `.prefetch()` transformation for each interleaved iterator). prefetch_input_elements: The number of input elements to transform to iterators before they are needed for interleaving. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14791477sample_from_datasets_v2tensorflow/tensorflow/python/data/experimental/ops/interleave_ops.py146functionSamples elements at random from the datasets in `datasets`. Args: datasets: A list of `tf.data.Dataset` objects with compatible structure. weights: (Optional.) A list of `len(datasets)` floating-point values where `weights[i]` represents the probability with which an element should be sampled from `datasets[i]`, or a `tf.data.Dataset` object where each element is such a list. Defaults to a uniform distribution across `datasets`. seed: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the random seed that will be used to create the distribution. See `tf.random.set_seed` for behavior. Returns: A dataset that interleaves elements from `datasets` at random, according to `weights` if provided, otherwise with uniform probability. Raises: TypeError: If the `datasets` or `weights` arguments have the wrong type. ValueError: If the `weights` argument is specified and does not match the length of the `datasets` element.
14801478sample_from_datasets_v1tensorflow/tensorflow/python/data/experimental/ops/interleave_ops.py230function
14811479choose_from_datasets_v2tensorflow/tensorflow/python/data/experimental/ops/interleave_ops.py237functionCreates a dataset that deterministically chooses elements from `datasets`. For example, given the following datasets: ```python datasets = [tf.data.Dataset.from_tensors("foo").repeat(), tf.data.Dataset.from_tensors("bar").repeat(), tf.data.Dataset.from_tensors("baz").repeat()] # Define a dataset containing `[0, 1, 2, 0, 1, 2, 0, 1, 2]`. choice_dataset = tf.data.Dataset.range(3).repeat(3) result = tf.data.experimental.choose_from_datasets(datasets, choice_dataset) ``` The elements of `result` will be: ``` "foo", "bar", "baz", "foo", "bar", "baz", "foo", "bar", "baz" ``` Args: datasets: A list of `tf.data.Dataset` objects with compatible structure. choice_dataset: A `tf.data.Dataset` of scalar `tf.int64` tensors between `0` and `len(datasets) - 1`. Returns: A dataset that interleaves elements from `datasets` according to the values of `choice_dataset`. Raises: TypeError: If the `datasets` or `choice_dataset` arguments have the wrong type.
14821480choose_from_datasets_v1tensorflow/tensorflow/python/data/experimental/ops/interleave_ops.py280function
14831481savetensorflow/tensorflow/python/data/experimental/ops/io.py34functionSaves the content of the given dataset. Example usage: >>> import tempfile >>> path = os.path.join(tempfile.gettempdir(), "saved_data") >>> # Save a dataset >>> dataset = tf.data.Dataset.range(2) >>> tf.data.experimental.save(dataset, path) >>> new_dataset = tf.data.experimental.load(path, ... tf.TensorSpec(shape=(), dtype=tf.int64)) >>> for elem in new_dataset: ... print(elem) tf.Tensor(0, shape=(), dtype=int64) tf.Tensor(1, shape=(), dtype=int64) The saved dataset is saved in multiple file "shards". By default, the dataset output is divided to shards in a round-robin fashion but custom sharding can be specified via the `shard_func` function. For example, you can save the dataset to using a single shard as follows: ```python dataset = make_dataset() def custom_shard_func(element): return 0 dataset = tf.data.experimental.save( path="/path/to/data", ..., shard_func=custom_shard_func) ``` NOTE: The directory layout and file format used for saving the dataset is considered an implementation detail and may change. For this reason, datasets saved through `tf.data.experimental.save` should only be consumed through `tf.data.experimental.load`, which is guaranteed to be backwards compatible. Args: dataset: The dataset to save. path: Required. A directory to use for saving the dataset. compression: Optional. The algorithm to use to compress data when writing it. Supported options are `GZIP` and `NONE`. Defaults to `NONE`. shard_func: Optional. A function to control the mapping of dataset elements to file shards. The function is expected to map elements of the input dataset to int64 shard IDs. If present, the function will be traced and executed as graph computation.
14841482loadtensorflow/tensorflow/python/data/experimental/ops/io.py146functionLoads a previously saved dataset. Example usage: >>> import tempfile >>> path = os.path.join(tempfile.gettempdir(), "saved_data") >>> # Save a dataset >>> dataset = tf.data.Dataset.range(2) >>> tf.data.experimental.save(dataset, path) >>> new_dataset = tf.data.experimental.load(path, ... tf.TensorSpec(shape=(), dtype=tf.int64)) >>> for elem in new_dataset: ... print(elem) tf.Tensor(0, shape=(), dtype=int64) tf.Tensor(1, shape=(), dtype=int64) Note that to load a previously saved dataset, you need to specify `element_spec` -- a type signature of the elements of the saved dataset, which can be obtained via `tf.data.Dataset.element_spec`. This requirement exists so that shape inference of the loaded dataset does not need to perform I/O. If the default option of sharding the saved dataset was used, the element order of the saved dataset will be preserved when loading it. The `reader_func` argument can be used to specify a custom order in which elements should be loaded from the individual shards. The `reader_func` is expected to take a single argument -- a dataset of datasets, each containing elements of one of the shards -- and return a dataset of elements. For example, the order of shards can be shuffled when loading them as follows: ```python def custom_reader_func(datasets): datasets = datasets.shuffle(NUM_SHARDS) return datasets.interleave(lambda x: x, num_parallel_calls=AUTOTUNE) dataset = tf.data.experimental.load( path="/path/to/data", ..., reader_func=custom_reader_func) ``` Args: path: Required. A path pointing to a previously saved dataset. element_spec: Required. A nested structure of `tf.TypeSpec` objects matching the structure of an element of the saved dataset and specifying the type of individual element components. compression: Optional. The algorithm to use to decompress the data when reading it. Supported options are `GZIP` and `NONE`. Defaults to `NONE`. reader_func: Optional. A function to control how to read data from shards. If present, the function will be traced and executed as graph computation. Returns: A `tf.data.Dataset` instance.
14851483make_saveable_from_iteratortensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py49functionReturns a SaveableObject for saving/restoring iterator state using Saver. Args: iterator: Iterator. external_state_policy: A string that identifies how to handle input pipelines that depend on external state. Possible values are 'ignore': The external state is silently ignored. 'warn': The external state is ignored, logging a warning. 'fail': The operation fails upon encountering external state. By default we set it to 'fail'. Returns: A SaveableObject for saving/restoring iterator state using Saver. Raises: ValueError: If iterator does not support checkpointing. ValueError: If `external_state_policy` is not one of 'warn', 'ignore' or 'fail'. For example: ```python with tf.Graph().as_default(): ds = tf.data.Dataset.range(10) iterator = ds.make_initializable_iterator() # Build the iterator SaveableObject. saveable_obj = tf.data.experimental.make_saveable_from_iterator(iterator) # Add the SaveableObject to the SAVEABLE_OBJECTS collection so # it can be automatically saved using Saver. tf.compat.v1.add_to_collection(tf.GraphKeys.SAVEABLE_OBJECTS, saveable_obj) saver = tf.compat.v1.train.Saver() while continue_training: ... Perform training ... if should_save_checkpoint: saver.save() ``` Note: When restoring the iterator, the existing iterator state is completely discarded. This means that any changes you may have made to the Dataset graph will be discarded as well! This includes the new Dataset graph that you may have built during validation. So, while running validation, make sure to run the initializer for the validation input pipeline after restoring the checkpoint. Note: Not all iterators support checkpointing yet. Attempting to save the state of an unsupported iterator will throw an error.
14861484CheckpointInputPipelineHooktensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py106classCheckpoints input pipeline state every N steps or seconds. This hook saves the state of the iterators in the `Graph` so that when training is resumed the input pipeline continues from where it left off. This could potentially avoid overfitting in certain pipelines where the number of training steps per eval are small compared to the dataset size or if the training pipeline is pre-empted. Differences from `CheckpointSaverHook`: 1. Saves only the input pipelines in the "iterators" collection and not the global variables or other saveable objects. 2. Does not write the `GraphDef` and `MetaGraphDef` to the summary. Example of checkpointing the training pipeline: ```python est = tf.estimator.Estimator(model_fn) while True: est.train( train_input_fn, hooks=[tf.data.experimental.CheckpointInputPipelineHook(est)], steps=train_steps_per_eval) # Note: We do not pass the hook here. metrics = est.evaluate(eval_input_fn) if should_stop_the_training(metrics): break ``` This hook should be used if the input pipeline state needs to be saved separate from the model checkpoint. Doing so may be useful for a few reasons: 1. The input pipeline checkpoint may be large, if there are large shuffle or prefetch buffers for instance, and may bloat the checkpoint size. 2. If the input pipeline is shared between training and validation, restoring the checkpoint during validation may override the validation input pipeline. For saving the input pipeline checkpoint alongside the model weights use `tf.data.experimental.make_saveable_from_iterator` directly to create a `SaveableObject` and add to the `SAVEABLE_OBJECTS` collection. Note, however, that you will need to be careful not to restore the training iterator during eval. You can do that by not adding the iterator to the SAVEABLE_OBJECTS collector when building the eval graph.
14871485begintensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py227method
14881486after_create_sessiontensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py244method
14891487before_runtensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py284method
14901488after_runtensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py290method
14911489endtensorflow/tensorflow/python/data/experimental/ops/iterator_ops.py293method
14921490map_defuntensorflow/tensorflow/python/data/experimental/ops/map_defun.py26functionMap a function on the list of tensors unpacked from `elems` on dimension 0. Args: fn: A function (`function.defun`) that takes a list of tensors and returns another list of tensors. The output list has the same types as output_dtypes. The elements of the output list have the same dimension 0 as `elems`, and the remaining dimensions correspond to those of `fn_output_shapes`. elems: A list of tensors. output_dtypes: A list of dtypes corresponding to the output types of the function. output_shapes: A list of `TensorShape`s corresponding to the output shapes from each invocation of the function on slices of inputs. max_intra_op_parallelism: An integer. If positive, sets the max parallelism limit of each function call to this. Raises: ValueError: if any of the inputs are malformed. Returns: A list of `Tensor` objects with the same types as `output_dtypes`.
14931491MatchingFilesDatasettensorflow/tensorflow/python/data/experimental/ops/matching_files.py28classA `Dataset` that list the files according to the input patterns.
14941492element_spectensorflow/tensorflow/python/data/experimental/ops/matching_files.py38method
14951493modeltensorflow/tensorflow/python/data/experimental/ops/optimization.py24functionA transformation that models performance. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14961494optimizetensorflow/tensorflow/python/data/experimental/ops/optimization.py39functionA transformation that applies optimizations. Args: optimizations: (Optional.) A `tf.string` vector `tf.Tensor` identifying optimizations to use. If not specified, the default set of optimizations is applied. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
14971495MapVectorizationOptionstensorflow/tensorflow/python/data/experimental/ops/optimization_options.py36classRepresents options for the MapVectorization optimization.
14981496OptimizationOptionstensorflow/tensorflow/python/data/experimental/ops/optimization_options.py70classRepresents options for dataset optimizations. You can set the optimization options of a dataset through the `experimental_optimization` property of `tf.data.Options`; the property is an instance of `tf.data.experimental.OptimizationOptions`. ```python options = tf.data.Options() options.experimental_optimization.noop_elimination = True options.experimental_optimization.map_vectorization.enabled = True options.experimental_optimization.apply_default_optimizations = False dataset = dataset.with_options(options) ```
14991497parse_example_datasettensorflow/tensorflow/python/data/experimental/ops/parsing_ops.py110functionA transformation that parses `Example` protos into a `dict` of tensors. Parses a number of serialized `Example` protos given in `serialized`. We refer to `serialized` as a batch with `batch_size` many entries of individual `Example` protos. This op parses serialized examples into a dictionary mapping keys to `Tensor`, `SparseTensor`, and `RaggedTensor` objects. `features` is a dict from keys to `VarLenFeature`, `RaggedFeature`, `SparseFeature`, and `FixedLenFeature` objects. Each `VarLenFeature` and `SparseFeature` is mapped to a `SparseTensor`; each `RaggedFeature` is mapped to a `RaggedTensor`; and each `FixedLenFeature` is mapped to a `Tensor`. See `tf.io.parse_example` for more details about feature dictionaries. Args: features: A `dict` mapping feature keys to `FixedLenFeature`, `VarLenFeature`, `RaggedFeature`, and `SparseFeature` values. num_parallel_calls: (Optional.) A `tf.int32` scalar `tf.Tensor`, representing the number of parsing processes to call in parallel. deterministic: (Optional.) A boolean controlling whether determinism should be traded for performance by allowing elements to be produced out of order if some parsing calls complete faster than others. If `deterministic` is `None`, the `tf.data.Options.experimental_deterministic` dataset option (`True` by default) is used to decide whether to produce elements deterministically. Returns: A dataset transformation function, which can be passed to `tf.data.Dataset.apply`. Raises: ValueError: if features argument is None.
15001498prefetch_to_devicetensorflow/tensorflow/python/data/experimental/ops/prefetching_ops.py37functionA transformation that prefetches dataset values to the given `device`. NOTE: Although the transformation creates a `tf.data.Dataset`, the transformation must be the final `Dataset` in the input pipeline. Args: device: A string. The name of a device to which elements will be prefetched. buffer_size: (Optional.) The number of elements to buffer on `device`. Defaults to an automatically chosen value. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15011499copy_to_devicetensorflow/tensorflow/python/data/experimental/ops/prefetching_ops.py60functionA transformation that copies dataset elements to the given `target_device`. Args: target_device: The name of a device to which elements will be copied. source_device: The original device on which `input_dataset` will be placed. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15021500map_on_gputensorflow/tensorflow/python/data/experimental/ops/prefetching_ops.py260functionMaps `map_func` across the elements of this dataset. NOTE: This is a highly experimental version of `tf.data.Dataset.map` that runs `map_func` on GPU. It must be used after applying the `tf.data.experimental.copy_to_device` transformation with a GPU device argument. Args: map_func: A function mapping a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to another nested structure of tensors. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15031501RandomDatasetV2tensorflow/tensorflow/python/data/experimental/ops/random_ops.py32classA `Dataset` of pseudorandom values.
15041502element_spectensorflow/tensorflow/python/data/experimental/ops/random_ops.py43method
15051503RandomDatasetV1tensorflow/tensorflow/python/data/experimental/ops/random_ops.py48classA `Dataset` of pseudorandom values.
15061504make_tf_record_datasettensorflow/tensorflow/python/data/experimental/ops/readers.py223functionReads and optionally parses TFRecord files into a dataset. Provides common functionality such as batching, optional parsing, shuffling, and performant defaults. Args: file_pattern: List of files or patterns of TFRecord file paths. See `tf.io.gfile.glob` for pattern rules. batch_size: An int representing the number of records to combine in a single batch. parser_fn: (Optional.) A function accepting string input to parse and process the record contents. This function must map records to components of a fixed shape, so they may be batched. By default, uses the record contents unmodified. num_epochs: (Optional.) An int specifying the number of times this dataset is repeated. If None (the default), cycles through the dataset forever. shuffle: (Optional.) A bool that indicates whether the input should be shuffled. Defaults to `True`. shuffle_buffer_size: (Optional.) Buffer size to use for shuffling. A large buffer size ensures better shuffling, but increases memory usage and startup time. shuffle_seed: (Optional.) Randomization seed to use for shuffling. prefetch_buffer_size: (Optional.) An int specifying the number of feature batches to prefetch for performance improvement. Defaults to auto-tune. Set to 0 to disable prefetching. num_parallel_reads: (Optional.) Number of threads used to read records from files. By default or if set to a value >1, the results will be interleaved. Defaults to `24`. num_parallel_parser_calls: (Optional.) Number of parallel records to parse in parallel. Defaults to `batch_size`. drop_final_batch: (Optional.) Whether the last batch should be dropped in case its size is smaller than `batch_size`; the default behavior is not to drop the smaller batch. Returns: A dataset, where each element matches the output of `parser_fn` except it will have an additional leading `batch-size` dimension, or a `batch_size`-length 1-D tensor of strings if `parser_fn` is unspecified.
15071505make_csv_dataset_v2tensorflow/tensorflow/python/data/experimental/ops/readers.py322functionReads CSV files into a dataset. Reads CSV files into a dataset, where each element is a (features, labels) tuple that corresponds to a batch of CSV rows. The features dictionary maps feature column names to `Tensor`s containing the corresponding feature data, and labels is a `Tensor` containing the batch's label data. Args: file_pattern: List of files or patterns of file paths containing CSV records. See `tf.io.gfile.glob` for pattern rules. batch_size: An int representing the number of records to combine in a single batch. column_names: An optional list of strings that corresponds to the CSV columns, in order. One per column of the input record. If this is not provided, infers the column names from the first row of the records. These names will be the keys of the features dict of each dataset element. column_defaults: A optional list of default values for the CSV fields. One item per selected column of the input record. Each item in the list is either a valid CSV dtype (float32, float64, int32, int64, or string), or a `Tensor` with one of the aforementioned types. The tensor can either be a scalar default value (if the column is optional), or an empty tensor (if the column is required). If a dtype is provided instead of a tensor, the column is also treated as required. If this list is not provided, tries to infer types based on reading the first num_rows_for_inference rows of files specified, and assumes all columns are optional, defaulting to `0` for numeric values and `""` for string values. If both this and `select_columns` are specified, these must have the same lengths, and `column_defaults` is assumed to be sorted in order of increasing column index. label_name: A optional string corresponding to the label column. If provided, the data for this column is returned as a separate `Tensor` from the features dictionary, so that the dataset complies with the format expected by a `tf.Estimator.train` or `tf.Estimator.evaluate` input function. select_columns: An optional list of integer indices or string column names, that specifies a subset of columns of CSV data to select. If column names are provided, these must correspond to names provided in `column_names` or inferred from the file header lines. When this argument is specified, only a subset of CSV columns will be parsed and returned, corresponding to the columns specified. Using this results in faster parsing and lower memory usage. If both this and `column_defaults` are specified, these must have the same lengths, and `column_defaults` is assumed to be sorted in order of increasing column index. field_delim: An optional `string`. Defaults to `","`. Char delimiter to separate fields in a record. use_quote_delim: An optional bool. Defaults to `True`. If false, treats double quotation marks as regular characters inside of the string fields. na_value: Additional string to recognize as NA/NaN. header: A bool that indicates whether the first rows of provided CSV files correspond to header lines with column names, and should not be included in the data. num_epochs: An int specifying the number of times this dataset is repeated. If None, cycles through the dataset forever. shuffle: A bool that indicates whether the input should be shuffled. shuffle_buffer_size: Buffer size to use for shuffling. A large buffer size ensures better shuffling, but increases memory usage and startup time. shuffle_seed: Randomization seed to use for shuffling. prefetch_buffer_size: An int specifying the number of feature batches to prefetch for performance improvement. Recommended value is the number of batches consumed per training step. Defaults to auto-tune. num_parallel_reads: Number of threads used to read CSV records from files. If >1, the results will be interleaved. Defaults to `1`. sloppy: If `True`, reading performance will be improved at the cost of non-deterministic ordering. If `False`, the order of elements produced is deterministic prior to shuffling (elements are still randomized if `shuffle=True`. Note that if the seed is set, then order of elements after shuffling is deterministic). Defaults to `False`. num_rows_for_inference: Number of rows of a file to use for type inference if record_defaults is not provided. If None, reads all the rows of all the files. Defaults to 100. compression_type: (Optional.) A `tf.string` scalar evaluating to one of `""` (no compression), `"ZLIB"`, or `"GZIP"`. Defaults to no compression. ignore_errors: (Optional.) If `True`, ignores errors with CSV file parsing, such as malformed data or empty lines, and moves on to the next valid CSV record. Otherwise, the dataset raises an error and stops processing when encountering any invalid records. Defaults to `False`. Returns: A dataset, where each element is a (features, labels) tuple that corresponds to a batch of `batch_size` CSV rows. The features dictionary maps feature column names to `Tensor`s containing the corresponding column data, and labels is a `Tensor` containing the column data for the label column specified by `label_name`. Raises: ValueError: If any of the arguments is malformed.
15081506make_csv_dataset_v1tensorflow/tensorflow/python/data/experimental/ops/readers.py569function
15091507CsvDatasetV2tensorflow/tensorflow/python/data/experimental/ops/readers.py604classA Dataset comprising lines from one or more CSV files.
15101508element_spectensorflow/tensorflow/python/data/experimental/ops/readers.py778method
15111509CsvDatasetV1tensorflow/tensorflow/python/data/experimental/ops/readers.py783classA Dataset comprising lines from one or more CSV files.
15121510make_batched_features_dataset_v2tensorflow/tensorflow/python/data/experimental/ops/readers.py874functionReturns a `Dataset` of feature dictionaries from `Example` protos. If label_key argument is provided, returns a `Dataset` of tuple comprising of feature dictionaries and label. Example: ``` serialized_examples = [ features { feature { key: "age" value { int64_list { value: [ 0 ] } } } feature { key: "gender" value { bytes_list { value: [ "f" ] } } } feature { key: "kws" value { bytes_list { value: [ "code", "art" ] } } } }, features { feature { key: "age" value { int64_list { value: [] } } } feature { key: "gender" value { bytes_list { value: [ "f" ] } } } feature { key: "kws" value { bytes_list { value: [ "sports" ] } } } } ] ``` We can use arguments: ``` features: { "age": FixedLenFeature([], dtype=tf.int64, default_value=-1), "gender": FixedLenFeature([], dtype=tf.string), "kws": VarLenFeature(dtype=tf.string), } ``` And the expected output is: ```python { "age": [[0], [-1]], "gender": [["f"], ["f"]], "kws": SparseTensor( indices=[[0, 0], [0, 1], [1, 0]], values=["code", "art", "sports"] dense_shape=[2, 2]), } ``` Args: file_pattern: List of files or patterns of file paths containing `Example` records. See `tf.io.gfile.glob` for pattern rules. batch_size: An int representing the number of records to combine in a single batch. features: A `dict` mapping feature keys to `FixedLenFeature` or `VarLenFeature` values. See `tf.io.parse_example`. reader: A function or class that can be called with a `filenames` tensor and (optional) `reader_args` and returns a `Dataset` of `Example` tensors. Defaults to `tf.data.TFRecordDataset`. label_key: (Optional) A string corresponding to the key labels are stored in `tf.Examples`. If provided, it must be one of the `features` key, otherwise results in `ValueError`. reader_args: Additional arguments to pass to the reader class. num_epochs: Integer specifying the number of times to read through the dataset. If None, cycles through the dataset forever. Defaults to `None`. shuffle: A boolean, indicates whether the input should be shuffled. Defaults to `True`. shuffle_buffer_size: Buffer size of the ShuffleDataset. A large capacity ensures better shuffling but would increase memory usage and startup time. shuffle_seed: Randomization seed to use for shuffling. prefetch_buffer_size: Number of feature batches to prefetch in order to improve performance. Recommended value is the number of batches consumed per training step. Defaults to auto-tune. reader_num_threads: Number of threads used to read `Example` records. If >1, the results will be interleaved. Defaults to `1`. parser_num_threads: Number of threads to use for parsing `Example` tensors into a dictionary of `Feature` tensors. Defaults to `2`. sloppy_ordering: If `True`, reading performance will be improved at the cost of non-deterministic ordering. If `False`, the order of elements produced is deterministic prior to shuffling (elements are still randomized if `shuffle=True`. Note that if the seed is set, then order of elements after shuffling is deterministic). Defaults to `False`. drop_final_batch: If `True`, and the batch size does not evenly divide the input dataset size, the final smaller batch will be dropped. Defaults to `False`. Returns: A dataset of `dict` elements, (or a tuple of `dict` elements and label). Each `dict` maps feature keys to `Tensor` or `SparseTensor` objects. Raises: TypeError: If `reader` is of the wrong type. ValueError: If `label_key` is not one of the `features` keys.
15131511make_batched_features_dataset_v1tensorflow/tensorflow/python/data/experimental/ops/readers.py1058function
15141512SqlDatasetV2tensorflow/tensorflow/python/data/experimental/ops/readers.py1114classA `Dataset` consisting of the results from a SQL query.
15151513element_spectensorflow/tensorflow/python/data/experimental/ops/readers.py1155method
15161514SqlDatasetV1tensorflow/tensorflow/python/data/experimental/ops/readers.py1160classA `Dataset` consisting of the results from a SQL query.
15171515rejection_resampletensorflow/tensorflow/python/data/experimental/ops/resampling.py37functionA transformation that resamples a dataset to achieve a target distribution. **NOTE** Resampling is performed via rejection sampling; some fraction of the input values will be dropped. Args: class_func: A function mapping an element of the input dataset to a scalar `tf.int32` tensor. Values should be in `[0, num_classes)`. target_dist: A floating point type tensor, shaped `[num_classes]`. initial_dist: (Optional.) A floating point type tensor, shaped `[num_classes]`. If not provided, the true class distribution is estimated live in a streaming fashion. seed: (Optional.) Python integer seed for the resampler. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15181516scantensorflow/tensorflow/python/data/experimental/ops/scan_ops.py158functionA transformation that scans a function across an input dataset. This transformation is a stateful relative of `tf.data.Dataset.map`. In addition to mapping `scan_func` across the elements of the input dataset, `scan()` accumulates one or more state tensors, whose initial values are `initial_state`. Args: initial_state: A nested structure of tensors, representing the initial state of the accumulator. scan_func: A function that maps `(old_state, input_element)` to `(new_state, output_element)`. It must take two arguments and return a pair of nested structures of tensors. The `new_state` must match the structure of `initial_state`. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15191517shuffle_and_repeattensorflow/tensorflow/python/data/experimental/ops/shuffle_ops.py60functionShuffles and repeats a Dataset, reshuffling with each repetition. >>> d = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> d = d.apply(tf.data.experimental.shuffle_and_repeat(2, count=2)) >>> [elem.numpy() for elem in d] # doctest: +SKIP [2, 3, 1, 1, 3, 2] ```python dataset.apply( tf.data.experimental.shuffle_and_repeat(buffer_size, count, seed)) ``` produces the same output as ```python dataset.shuffle( buffer_size, seed=seed, reshuffle_each_iteration=True).repeat(count) ``` In each repetition, this dataset fills a buffer with `buffer_size` elements, then randomly samples elements from this buffer, replacing the selected elements with new elements. For perfect shuffling, set the buffer size equal to the full size of the dataset. For instance, if your dataset contains 10,000 elements but `buffer_size` is set to 1,000, then `shuffle` will initially select a random element from only the first 1,000 elements in the buffer. Once an element is selected, its space in the buffer is replaced by the next (i.e. 1,001-st) element, maintaining the 1,000 element buffer. Args: buffer_size: A `tf.int64` scalar `tf.Tensor`, representing the maximum number elements that will be buffered when prefetching. count: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the number of times the dataset should be repeated. The default behavior (if `count` is `None` or `-1`) is for the dataset be repeated indefinitely. seed: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the random seed that will be used to create the distribution. See `tf.random.set_seed` for behavior. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15201518sleeptensorflow/tensorflow/python/data/experimental/ops/sleep.py37functionSleeps for `sleep_microseconds` before producing each input element. Args: sleep_microseconds: The number of microseconds to sleep before producing an input element. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15211519legacy_snapshottensorflow/tensorflow/python/data/experimental/ops/snapshot.py108functionWrites to/reads from a snapshot of a dataset. This function attempts to determine whether a valid snapshot exists at the `path`, and reads from the snapshot if so. If not, it will run the preprocessing pipeline as usual, and write out a snapshot of the data processed for future use. Args: path: A directory where we want to save our snapshots and/or read from a previously saved snapshot. compression: The type of compression to apply to the Dataset. Currently supports "GZIP" or None. Defaults to None (no compression). reader_path_prefix: A prefix to add to the path when reading from snapshots. Defaults to None. writer_path_prefix: A prefix to add to the path when writing to snapshots. Defaults to None. shard_size_bytes: The size of each shard to be written by the snapshot dataset op. Defaults to 10 GiB. pending_snapshot_expiry_seconds: How long to wait (in seconds) before the snapshot op considers a previously unfinished snapshot to be stale. num_reader_threads: Number of threads to parallelize reading from snapshot. Especially useful if compression is turned on since the decompression operation tends to be intensive. Defaults to 1. If > 1, then this might introduce non-determinism i.e. the order in which the elements are read from the snapshot are different from the order they're written. reader_buffer_size: Maximum number of elements we can prefetch reading from the snapshot. Defaults to 1. Increasing this might improve performance but will increase memory consumption. num_writer_threads: Number of threads to parallelize writing from snapshot. We'll open up `num_writer_threads` files and write to them in parallel. Especially useful if compression is turned on since the compression operation tends to be intensive. Defaults to 1. If > 1, then this might introduce non-determinism i.e. the order in which the elements are read from the upstream iterator are different from the order they're written. writer_buffer_size: Maximum number of pipeline elements to fill up the buffer before writing them out using `num_writer_threads`. shuffle_on_read: If this is True, then the order in which examples are produced when reading from a snapshot will be random. Defaults to False. shuffle_seed: Optional. If shuffle_seed is set, the random number generator used for shuffling (when shuffle_on_read is turned on) is seeded by the given seed. Otherwise, it is seeded by a random seed that differs for every run. mode: The mode at which snapshot should operate. Valid options are "auto", "read", "write", and "passthrough". The default mode is "auto", where the snapshot op will automatically determine what mode to operate in. snapshot_name: If set, use the supplied string as a named snapshot name instead of introspecting the data pipeline and automatically generating a unique identifier for the snapshot. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15221520snapshottensorflow/tensorflow/python/data/experimental/ops/snapshot.py258functionAPI to persist the output of the input dataset. The snapshot API allows users to transparently persist the output of their preprocessing pipeline to disk, and materialize the pre-processed data on a different training run. This API enables repeated preprocessing steps to be consolidated, and allows re-use of already processed data, trading off disk storage and network bandwidth for freeing up more valuable CPU resources and accelerator compute time. https://github.com/tensorflow/community/blob/master/rfcs/20200107-tf-data-snapshot.md has detailed design documentation of this feature. Users can specify various options to control the behavior of snapshot, including how snapshots are read from and written to by passing in user-defined functions to the `reader_func` and `shard_func` parameters. `shard_func` is a user specified function that maps input elements to snapshot shards. Users may want to specify this function to control how snapshot files should be written to disk. Below is an example of how a potential shard_func could be written. ```python dataset = ... dataset = dataset.enumerate() dataset = dataset.apply(tf.data.experimental.snapshot("/path/to/snapshot/dir", shard_func=lambda x, y: x % NUM_SHARDS, ...)) dataset = dataset.map(lambda x, y: y) ``` `reader_func` is a user specified function that accepts a single argument: (1) a Dataset of Datasets, each representing a "split" of elements of the original dataset. The cardinality of the input dataset matches the number of the shards specified in the `shard_func` (see above). The function should return a Dataset of elements of the original dataset. Users may want specify this function to control how snapshot files should be read from disk, including the amount of shuffling and parallelism. Here is an example of a standard reader function a user can define. This function enables both dataset shuffling and parallel reading of datasets: ```python def user_reader_func(datasets): # shuffle the datasets splits datasets = datasets.shuffle(NUM_CORES) # read datasets in parallel and interleave their elements return datasets.interleave(lambda x: x, num_parallel_calls=AUTOTUNE) dataset = dataset.apply(tf.data.experimental.snapshot("/path/to/snapshot/dir", reader_func=user_reader_func)) ``` By default, snapshot parallelizes reads by the number of cores available on the system, but will not attempt to shuffle the data. Args: path: Required. A directory to use for storing / loading the snapshot to / from. compression: Optional. The type of compression to apply to the snapshot written to disk. Supported options are `GZIP`, `SNAPPY`, `AUTO` or None. Defaults to AUTO, which attempts to pick an appropriate compression algorithm for the dataset. reader_func: Optional. A function to control how to read data from snapshot shards. shard_func: Optional. A function to control how to shard data when writing a snapshot. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15231521StatsAggregatorV2tensorflow/tensorflow/python/data/experimental/ops/stats_aggregator.py31classA stateful resource that aggregates statistics from one or more iterators. To record statistics, use one of the custom transformation functions defined in this module when defining your `tf.data.Dataset`. All statistics will be aggregated by the `StatsAggregator` that is associated with a particular iterator (see below). For example, to record the latency of producing each element by iterating over a dataset: ```python dataset = ... dataset = dataset.apply(tf.data.experimental.latency_stats("total_bytes")) ``` To associate a `StatsAggregator` with a `tf.data.Dataset` object, use the following pattern: ```python aggregator = tf.data.experimental.StatsAggregator() dataset = ... # Apply `StatsOptions` to associate `dataset` with `aggregator`. options = tf.data.Options() options.experimental_stats.aggregator = aggregator dataset = dataset.with_options(options) ``` Note: This interface is experimental and expected to change. In particular, we expect to add other implementations of `StatsAggregator` that provide different ways of exporting statistics, and add more types of statistics.
15241522StatsAggregatorV1tensorflow/tensorflow/python/data/experimental/ops/stats_aggregator.py82classA stateful resource that aggregates statistics from one or more iterators. To record statistics, use one of the custom transformation functions defined in this module when defining your `tf.data.Dataset`. All statistics will be aggregated by the `StatsAggregator` that is associated with a particular iterator (see below). For example, to record the latency of producing each element by iterating over a dataset: ```python dataset = ... dataset = dataset.apply(tf.data.experimental.latency_stats("total_bytes")) ``` To associate a `StatsAggregator` with a `tf.data.Dataset` object, use the following pattern: ```python aggregator = tf.data.experimental.StatsAggregator() dataset = ... # Apply `StatsOptions` to associate `dataset` with `aggregator`. options = tf.data.Options() options.experimental_stats.aggregator = aggregator dataset = dataset.with_options(options) ``` To get a protocol buffer summary of the currently aggregated statistics, use the `StatsAggregator.get_summary()` tensor. The easiest way to do this is to add the returned tensor to the `tf.GraphKeys.SUMMARIES` collection, so that the summaries will be included with any existing summaries. ```python aggregator = tf.data.experimental.StatsAggregator() # ... stats_summary = aggregator.get_summary() tf.compat.v1.add_to_collection(tf.GraphKeys.SUMMARIES, stats_summary) ``` Note: This interface is experimental and expected to change. In particular, we expect to add other implementations of `StatsAggregator` that provide different ways of exporting statistics, and add more types of statistics.
15251523get_summarytensorflow/tensorflow/python/data/experimental/ops/stats_aggregator.py130methodReturns a string `tf.Tensor` that summarizes the aggregated statistics. The returned tensor will contain a serialized `tf.compat.v1.summary.Summary` protocol buffer, which can be used with the standard TensorBoard logging facilities. Returns: A scalar string `tf.Tensor` that summarizes the aggregated statistics.
15261524set_stats_aggregatortensorflow/tensorflow/python/data/experimental/ops/stats_ops.py29functionSet the given `stats_aggregator` for aggregating the input dataset stats. Args: stats_aggregator: A `tf.data.experimental.StatsAggregator` object. prefix: (Optional) String, all statistics recorded for the input `dataset` will have given `prefix` prepend with the name. counter_prefix: (Optional) String, all statistics recorded as `counters` will have the given `prefix` for the counter. Defaults to "/tensorflow". Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15271525bytes_produced_statstensorflow/tensorflow/python/data/experimental/ops/stats_ops.py52functionRecords the number of bytes produced by each element of the input dataset. To consume the statistics, associate a `StatsAggregator` with the output dataset. Args: tag: String. All statistics recorded by the returned transformation will be associated with the given `tag`. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15281526latency_statstensorflow/tensorflow/python/data/experimental/ops/stats_ops.py75functionRecords the latency of producing each element of the input dataset. To consume the statistics, associate a `StatsAggregator` with the output dataset. Args: tag: String. All statistics recorded by the returned transformation will be associated with the given `tag`. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15291527StatsOptionstensorflow/tensorflow/python/data/experimental/ops/stats_options.py28classRepresents options for collecting dataset stats using `StatsAggregator`. You can set the stats options of a dataset through the `experimental_stats` property of `tf.data.Options`; the property is an instance of `tf.data.experimental.StatsOptions`. For example, to collect latency stats on all dataset edges, use the following pattern: ```python aggregator = tf.data.experimental.StatsAggregator() options = tf.data.Options() options.experimental_stats.aggregator = aggregator options.experimental_stats.latency_all_edges = True dataset = dataset.with_options(options) ```
15301528take_whiletensorflow/tensorflow/python/data/experimental/ops/take_while_ops.py56functionA transformation that stops dataset iteration based on a `predicate`. Args: predicate: A function that maps a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to a scalar `tf.bool` tensor. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15311529assert_nexttensorflow/tensorflow/python/data/experimental/ops/testing.py26functionA transformation that asserts which transformations happen next. Transformations should be referred to by their base name, not including version suffix. For example, use "Batch" instead of "BatchV2". "Batch" will match any of "Batch", "BatchV1", "BatchV2", etc. Args: transformations: A `tf.string` vector `tf.Tensor` identifying the transformations that are expected to happen next. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15321530non_serializabletensorflow/tensorflow/python/data/experimental/ops/testing.py49functionA non-serializable identity transformation. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15331531sleeptensorflow/tensorflow/python/data/experimental/ops/testing.py64functionSleeps for `sleep_microseconds` before producing each input element. Args: sleep_microseconds: The number of microseconds to sleep before producing an input element. Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15341532ThreadingOptionstensorflow/tensorflow/python/data/experimental/ops/threading_options.py26classRepresents options for dataset threading. You can set the threading options of a dataset through the `experimental_threading` property of `tf.data.Options`; the property is an instance of `tf.data.experimental.ThreadingOptions`. ```python options = tf.data.Options() options.experimental_threading.private_threadpool_size = 10 dataset = dataset.with_options(options) ```
15351533PrivateThreadPooltensorflow/tensorflow/python/data/experimental/ops/threadpool.py41classA stateful resource that represents a private thread pool.
15361534override_threadpooltensorflow/tensorflow/python/data/experimental/ops/threadpool.py78functionReturns a new dataset that uses the given thread pool for its operations. Args: dataset: A `tf.data.Dataset` object. thread_pool: A `PrivateThreadPool` object. Returns: A dataset containing the same values as `dataset`, but which uses `thread_pool` to compute any of its parallel operations (such as `tf.data.Dataset.map`).
15371535uniquetensorflow/tensorflow/python/data/experimental/ops/unique.py27functionCreates a `Dataset` from another `Dataset`, discarding duplicates. Use this transformation to produce a dataset that contains one instance of each unique element in the input. For example: ```python dataset = tf.data.Dataset.from_tensor_slices([1, 37, 2, 37, 2, 1]) # Using `unique()` will drop the duplicate elements. dataset = dataset.apply(tf.data.experimental.unique()) # ==> { 1, 37, 2 } ``` Returns: A `Dataset` transformation function, which can be passed to `tf.data.Dataset.apply`.
15381536TFRecordWritertensorflow/tensorflow/python/data/experimental/ops/writers.py30classWrites a dataset to a TFRecord file. The elements of the dataset must be scalar strings. To serialize dataset elements as strings, you can use the `tf.io.serialize_tensor` function. ```python dataset = tf.data.Dataset.range(3) dataset = dataset.map(tf.io.serialize_tensor) writer = tf.data.experimental.TFRecordWriter("/path/to/file.tfrecord") writer.write(dataset) ``` To read back the elements, use `TFRecordDataset`. ```python dataset = tf.data.TFRecordDataset("/path/to/file.tfrecord") dataset = dataset.map(lambda x: tf.io.parse_tensor(x, tf.int64)) ``` To shard a `dataset` across multiple TFRecord files: ```python dataset = ... # dataset to be written def reduce_func(key, dataset): filename = tf.strings.join([PATH_PREFIX, tf.strings.as_string(key)]) writer = tf.data.experimental.TFRecordWriter(filename) writer.write(dataset.map(lambda _, x: x)) return tf.data.Dataset.from_tensors(filename) dataset = dataset.enumerate() dataset = dataset.apply(tf.data.experimental.group_by_window( lambda i, _: i % NUM_SHARDS, reduce_func, tf.int64.max )) ```
15391537writetensorflow/tensorflow/python/data/experimental/ops/writers.py85methodWrites a dataset to a TFRecord file. An operation that writes the content of the specified dataset to the file specified in the constructor. If the file exists, it will be overwritten. Args: dataset: a `tf.data.Dataset` whose elements are to be written to a file Returns: In graph mode, this returns an operation which when executed performs the write. In eager mode, the write is performed by the method itself and there is no return value. Raises TypeError: if `dataset` is not a `tf.data.Dataset`. TypeError: if the elements produced by the dataset are not scalar strings.
15401538DispatchServertensorflow/tensorflow/python/data/experimental/service/server_lib.py29classAn in-process tf.data service dispatch server. A `tf.data.experimental.service.DispatchServer` coordinates a cluster of `tf.data.experimental.service.WorkerServer`s. When the workers start, they register themselves with the dispatcher. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0) >>> dispatcher_address = dispatcher.target.split("://")[1] >>> worker = tf.data.experimental.service.WorkerServer( ... port=0, dispatcher_address=dispatcher_address) >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.apply(tf.data.experimental.service.distribute( ... processing_mode="parallel_epochs", service=dispatcher.target)) >>> print(list(dataset.as_numpy_iterator())) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] When starting a dedicated tf.data dispatch process, use join() to block indefinitely after starting up the server. ``` dispatcher = tf.data.experimental.service.DispatchServer(port=5050) dispatcher.join() ```
15411539starttensorflow/tensorflow/python/data/experimental/service/server_lib.py78methodStarts this server. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0, ... start=False) >>> dispatcher.start() Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while starting the server.
15421540jointensorflow/tensorflow/python/data/experimental/service/server_lib.py91methodBlocks until the server has shut down. This is useful when starting a dedicated dispatch process. ``` dispatcher = tf.data.experimental.service.DispatchServer(port=5050) dispatcher.join() ``` Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while joining the server.
15431541targettensorflow/tensorflow/python/data/experimental/service/server_lib.py108methodReturns a target that can be used to connect to the server. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0) >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.apply(tf.data.experimental.service.distribute( ... processing_mode="parallel_epochs", service=dispatcher.target)) The returned string will be in the form protocol://address, e.g. "grpc://localhost:5050".
15441542WorkerServertensorflow/tensorflow/python/data/experimental/service/server_lib.py148classAn in-process tf.data service worker server. A `tf.data.experimental.service.WorkerServer` performs `tf.data.Dataset` processing for user-defined datasets, and provides the resulting elements over RPC. A worker is associated with a single `tf.data.experimental.service.DispatchServer`. >>> dispatcher = tf.data.experimental.service.DispatchServer(port=0) >>> dispatcher_address = dispatcher.target.split("://")[1] >>> worker = tf.data.experimental.service.WorkerServer( ... port=0, dispatcher_address=dispatcher_address) >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.apply(tf.data.experimental.service.distribute( ... processing_mode="parallel_epochs", service=dispatcher.target)) >>> print(list(dataset.as_numpy_iterator())) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] When starting a dedicated tf.data worker process, use join() to block indefinitely after starting up the server. ``` worker = tf.data.experimental.service.WorkerServer( port=5051, dispatcher_address="grpc://localhost:5050") worker.join() ```
15451543starttensorflow/tensorflow/python/data/experimental/service/server_lib.py213methodStarts this server. Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while starting the server.
15461544jointensorflow/tensorflow/python/data/experimental/service/server_lib.py222methodBlocks until the server has shut down. This is useful when starting a dedicated worker process. ``` worker_server = tf.data.experimental.service.WorkerServer( port=5051, dispatcher_address="grpc://localhost:5050") worker_server.join() ``` This method currently blocks forever. Raises: tf.errors.OpError: Or one of its subclasses if an error occurs while joining the server.
15471545Footensorflow/tensorflow/python/data/kernel_tests/map_test.py153classDummy class used for invalid return value tests.
15481546eager_only_combinationstensorflow/tensorflow/python/data/kernel_tests/test_base.py44functionReturns the default test combinations for eager mode only tf.data tests.
15491547graph_only_combinationstensorflow/tensorflow/python/data/kernel_tests/test_base.py49functionReturns the default test combinations for graph mode only tf.data tests.
15501548v2_only_combinationstensorflow/tensorflow/python/data/kernel_tests/test_base.py54functionReturns the default test combinations for v1 only tf.data tests.
15511549DatasetV2tensorflow/tensorflow/python/data/ops/dataset_ops.py106classRepresents a potentially large set of elements. The `tf.data.Dataset` API supports writing descriptive and efficient input pipelines. `Dataset` usage follows a common pattern: 1. Create a source dataset from your input data. 2. Apply dataset transformations to preprocess the data. 3. Iterate over the dataset and process the elements. Iteration happens in a streaming fashion, so the full dataset does not need to fit into memory. Source Datasets: The simplest way to create a dataset is to create it from a python `list`: >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> for element in dataset: ... print(element) tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32) To process lines from files, use `tf.data.TextLineDataset`: >>> dataset = tf.data.TextLineDataset(["file1.txt", "file2.txt"]) To process records written in the `TFRecord` format, use `TFRecordDataset`: >>> dataset = tf.data.TFRecordDataset(["file1.tfrecords", "file2.tfrecords"]) To create a dataset of all files matching a pattern, use `tf.data.Dataset.list_files`: >>> dataset = tf.data.Dataset.list_files("/path/*.txt") # doctest: +SKIP See `tf.data.FixedLengthRecordDataset` and `tf.data.Dataset.from_generator` for more ways to create datasets. Transformations: Once you have a dataset, you can apply transformations to prepare the data for your model: >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> dataset = dataset.map(lambda x: x*2) >>> list(dataset.as_numpy_iterator()) [2, 4, 6] Common Terms: **Element**: A single output from calling `next()` on a dataset iterator. Elements may be nested structures containing multiple components. For example, the element `(1, (3, "apple"))` has one tuple nested in another tuple. The components are `1`, `3`, and `"apple"`. **Component**: The leaf in the nested structure of an element. Supported types: Elements can be nested structures of tuples, named tuples, and dictionaries. Note that Python lists are *not* treated as nested structures of components. Instead, lists are converted to tensors and treated as components. For example, the element `(1, [1, 2, 3])` has only two components; the tensor `1` and the tensor `[1, 2, 3]`. Element components can be of any type representable by `tf.TypeSpec`, including `tf.Tensor`, `tf.data.Dataset`, `tf.sparse.SparseTensor`, `tf.RaggedTensor`, and `tf.TensorArray`. >>> a = 1 # Integer element >>> b = 2.0 # Float element >>> c = (1, 2) # Tuple element with 2 components >>> d = {"a": (2, 2), "b": 3} # Dict element with 3 components >>> Point = collections.namedtuple("Point", ["x", "y"]) # doctest: +SKIP >>> e = Point(1, 2) # Named tuple # doctest: +SKIP >>> f = tf.data.Dataset.range(10) # Dataset element
15521550optionstensorflow/tensorflow/python/data/ops/dataset_ops.py348methodReturns the options for this dataset and its inputs. Returns: A `tf.data.Options` object representing the dataset options.
15531551element_spectensorflow/tensorflow/python/data/ops/dataset_ops.py451methodThe type specification of an element of this dataset. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> dataset.element_spec TensorSpec(shape=(), dtype=tf.int32, name=None) Returns: A nested structure of `tf.TypeSpec` objects matching the structure of an element of this dataset and specifying the type of individual components.
15541552as_numpy_iteratortensorflow/tensorflow/python/data/ops/dataset_ops.py472methodReturns an iterator which converts all elements of the dataset to numpy. Use `as_numpy_iterator` to inspect the content of your dataset. To see element shapes and types, print dataset elements directly instead of using `as_numpy_iterator`. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> for element in dataset: ... print(element) tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(2, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32) This method requires that you are running in eager mode and the dataset's element_spec contains only `TensorSpec` components. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> for element in dataset.as_numpy_iterator(): ... print(element) 1 2 3 >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> print(list(dataset.as_numpy_iterator())) [1, 2, 3] `as_numpy_iterator()` will preserve the nested structure of dataset elements. >>> dataset = tf.data.Dataset.from_tensor_slices({'a': ([1, 2], [3, 4]), ... 'b': [5, 6]}) >>> list(dataset.as_numpy_iterator()) == [{'a': (1, 3), 'b': 5}, ... {'a': (2, 4), 'b': 6}] True Returns: An iterable over the elements of the dataset, with their tensors converted to numpy arrays. Raises: TypeError: if an element contains a non-`Tensor` value. RuntimeError: if eager execution is not enabled.
15551553from_tensorstensorflow/tensorflow/python/data/ops/dataset_ops.py570methodCreates a `Dataset` with a single element, comprising the given tensors. `from_tensors` produces a dataset containing only a single element. To slice the input tensor into multiple elements, use `from_tensor_slices` instead. >>> dataset = tf.data.Dataset.from_tensors([1, 2, 3]) >>> list(dataset.as_numpy_iterator()) [array([1, 2, 3], dtype=int32)] >>> dataset = tf.data.Dataset.from_tensors(([1, 2, 3], 'A')) >>> list(dataset.as_numpy_iterator()) [(array([1, 2, 3], dtype=int32), b'A')] >>> # You can use `from_tensors` to produce a dataset which repeats >>> # the same example many times. >>> example = tf.constant([1,2,3]) >>> dataset = tf.data.Dataset.from_tensors(example).repeat(2) >>> list(dataset.as_numpy_iterator()) [array([1, 2, 3], dtype=int32), array([1, 2, 3], dtype=int32)] Note that if `tensors` contains a NumPy array, and eager execution is not enabled, the values will be embedded in the graph as one or more `tf.constant` operations. For large datasets (> 1 GB), this can waste memory and run into byte limits of graph serialization. If `tensors` contains one or more large NumPy arrays, consider the alternative described in [this guide](https://tensorflow.org/guide/data#consuming_numpy_arrays). Args: tensors: A dataset element. Returns: Dataset: A `Dataset`.
15561554from_tensor_slicestensorflow/tensorflow/python/data/ops/dataset_ops.py607methodCreates a `Dataset` whose elements are slices of the given tensors. The given tensors are sliced along their first dimension. This operation preserves the structure of the input tensors, removing the first dimension of each tensor and using it as the dataset dimension. All input tensors must have the same size in their first dimensions. >>> # Slicing a 1D tensor produces scalar tensor elements. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> list(dataset.as_numpy_iterator()) [1, 2, 3] >>> # Slicing a 2D tensor produces 1D tensor elements. >>> dataset = tf.data.Dataset.from_tensor_slices([[1, 2], [3, 4]]) >>> list(dataset.as_numpy_iterator()) [array([1, 2], dtype=int32), array([3, 4], dtype=int32)] >>> # Slicing a tuple of 1D tensors produces tuple elements containing >>> # scalar tensors. >>> dataset = tf.data.Dataset.from_tensor_slices(([1, 2], [3, 4], [5, 6])) >>> list(dataset.as_numpy_iterator()) [(1, 3, 5), (2, 4, 6)] >>> # Dictionary structure is also preserved. >>> dataset = tf.data.Dataset.from_tensor_slices({"a": [1, 2], "b": [3, 4]}) >>> list(dataset.as_numpy_iterator()) == [{'a': 1, 'b': 3}, ... {'a': 2, 'b': 4}] True >>> # Two tensors can be combined into one Dataset object. >>> features = tf.constant([[1, 3], [2, 1], [3, 3]]) # ==> 3x2 tensor >>> labels = tf.constant(['A', 'B', 'A']) # ==> 3x1 tensor >>> dataset = Dataset.from_tensor_slices((features, labels)) >>> # Both the features and the labels tensors can be converted >>> # to a Dataset object separately and combined after. >>> features_dataset = Dataset.from_tensor_slices(features) >>> labels_dataset = Dataset.from_tensor_slices(labels) >>> dataset = Dataset.zip((features_dataset, labels_dataset)) >>> # A batched feature and label set can be converted to a Dataset >>> # in similar fashion. >>> batched_features = tf.constant([[[1, 3], [2, 3]], ... [[2, 1], [1, 2]], ... [[3, 3], [3, 2]]], shape=(3, 2, 2)) >>> batched_labels = tf.constant([['A', 'A'], ... ['B', 'B'], ... ['A', 'B']], shape=(3, 2, 1)) >>> dataset = Dataset.from_tensor_slices((batched_features, batched_labels)) >>> for element in dataset.as_numpy_iterator(): ... print(element) (array([[1, 3], [2, 3]], dtype=int32), array([[b'A'], [b'A']], dtype=object)) (array([[2, 1], [1, 2]], dtype=int32), array([[b'B'], [b'B']], dtype=object)) (array([[3, 3], [3, 2]], dtype=int32), array([[b'A'], [b'B']], dtype=object)) Note that if `tensors` contains a NumPy array, and eager execution is not enabled, the values will be embedded in the graph as one or more `tf.constant` operations. For large datasets (> 1 GB), this can waste memory and run into byte limits of graph serialization. If `tensors` contains one or more large NumPy arrays, consider the alternative described in [this guide]( https://tensorflow.org/guide/data#consuming_numpy_arrays). Args: tensors: A dataset element, with each component having the same size in the first dimension. Returns: Dataset: A `Dataset`.
15571555from_generatortensorflow/tensorflow/python/data/ops/dataset_ops.py721methodCreates a `Dataset` whose elements are generated by `generator`. The `generator` argument must be a callable object that returns an object that supports the `iter()` protocol (e.g. a generator function). The elements generated by `generator` must be compatible with the given `output_types` and (optional) `output_shapes` arguments. >>> import itertools >>> >>> def gen(): ... for i in itertools.count(1): ... yield (i, [1] * i) >>> >>> dataset = tf.data.Dataset.from_generator( ... gen, ... (tf.int64, tf.int64), ... (tf.TensorShape([]), tf.TensorShape([None]))) >>> >>> list(dataset.take(3).as_numpy_iterator()) [(1, array([1])), (2, array([1, 1])), (3, array([1, 1, 1]))] Note: The current implementation of `Dataset.from_generator()` uses `tf.numpy_function` and inherits the same constraints. In particular, it requires the dataset and iterator related operations to be placed on a device in the same process as the Python program that called `Dataset.from_generator()`. The body of `generator` will not be serialized in a `GraphDef`, and you should not use this method if you need to serialize your model and restore it in a different environment. Note: If `generator` depends on mutable global variables or other external state, be aware that the runtime may invoke `generator` multiple times (in order to support repeating the `Dataset`) and at any time between the call to `Dataset.from_generator()` and the production of the first element from the generator. Mutating global variables or external state can cause undefined behavior, and we recommend that you explicitly cache any external state in `generator` before calling `Dataset.from_generator()`. Args: generator: A callable object that returns an object that supports the `iter()` protocol. If `args` is not specified, `generator` must take no arguments; otherwise it must take as many arguments as there are values in `args`. output_types: A nested structure of `tf.DType` objects corresponding to each component of an element yielded by `generator`. output_shapes: (Optional.) A nested structure of `tf.TensorShape` objects corresponding to each component of an element yielded by `generator`. args: (Optional.) A tuple of `tf.Tensor` objects that will be evaluated and passed to `generator` as NumPy-array arguments. Returns: Dataset: A `Dataset`.
15581556rangetensorflow/tensorflow/python/data/ops/dataset_ops.py921methodCreates a `Dataset` of a step-separated range of values. >>> list(Dataset.range(5).as_numpy_iterator()) [0, 1, 2, 3, 4] >>> list(Dataset.range(2, 5).as_numpy_iterator()) [2, 3, 4] >>> list(Dataset.range(1, 5, 2).as_numpy_iterator()) [1, 3] >>> list(Dataset.range(1, 5, -2).as_numpy_iterator()) [] >>> list(Dataset.range(5, 1).as_numpy_iterator()) [] >>> list(Dataset.range(5, 1, -2).as_numpy_iterator()) [5, 3] >>> list(Dataset.range(2, 5, output_type=tf.int32).as_numpy_iterator()) [2, 3, 4] >>> list(Dataset.range(1, 5, 2, output_type=tf.float32).as_numpy_iterator()) [1.0, 3.0] Args: *args: follows the same semantics as python's xrange. len(args) == 1 -> start = 0, stop = args[0], step = 1. len(args) == 2 -> start = args[0], stop = args[1], step = 1. len(args) == 3 -> start = args[0], stop = args[1], step = args[2]. **kwargs: - output_type: Its expected dtype. (Optional, default: `tf.int64`). Returns: Dataset: A `RangeDataset`. Raises: ValueError: if len(args) == 0.
15591557ziptensorflow/tensorflow/python/data/ops/dataset_ops.py958methodCreates a `Dataset` by zipping together the given datasets. This method has similar semantics to the built-in `zip()` function in Python, with the main difference being that the `datasets` argument can be an arbitrary nested structure of `Dataset` objects. >>> # The nested structure of the `datasets` argument determines the >>> # structure of elements in the resulting dataset. >>> a = tf.data.Dataset.range(1, 4) # ==> [ 1, 2, 3 ] >>> b = tf.data.Dataset.range(4, 7) # ==> [ 4, 5, 6 ] >>> ds = tf.data.Dataset.zip((a, b)) >>> list(ds.as_numpy_iterator()) [(1, 4), (2, 5), (3, 6)] >>> ds = tf.data.Dataset.zip((b, a)) >>> list(ds.as_numpy_iterator()) [(4, 1), (5, 2), (6, 3)] >>> >>> # The `datasets` argument may contain an arbitrary number of datasets. >>> c = tf.data.Dataset.range(7, 13).batch(2) # ==> [ [7, 8], ... # [9, 10], ... # [11, 12] ] >>> ds = tf.data.Dataset.zip((a, b, c)) >>> for element in ds.as_numpy_iterator(): ... print(element) (1, 4, array([7, 8])) (2, 5, array([ 9, 10])) (3, 6, array([11, 12])) >>> >>> # The number of elements in the resulting dataset is the same as >>> # the size of the smallest dataset in `datasets`. >>> d = tf.data.Dataset.range(13, 15) # ==> [ 13, 14 ] >>> ds = tf.data.Dataset.zip((a, d)) >>> list(ds.as_numpy_iterator()) [(1, 13), (2, 14)] Args: datasets: A nested structure of datasets. Returns: Dataset: A `Dataset`.
15601558concatenatetensorflow/tensorflow/python/data/ops/dataset_ops.py1002methodCreates a `Dataset` by concatenating the given dataset with this dataset. >>> a = tf.data.Dataset.range(1, 4) # ==> [ 1, 2, 3 ] >>> b = tf.data.Dataset.range(4, 8) # ==> [ 4, 5, 6, 7 ] >>> ds = a.concatenate(b) >>> list(ds.as_numpy_iterator()) [1, 2, 3, 4, 5, 6, 7] >>> # The input dataset and dataset to be concatenated should have the same >>> # nested structures and output types. >>> c = tf.data.Dataset.zip((a, b)) >>> a.concatenate(c) Traceback (most recent call last): TypeError: Two datasets to concatenate have different types <dtype: 'int64'> and (tf.int64, tf.int64) >>> d = tf.data.Dataset.from_tensor_slices(["a", "b", "c"]) >>> a.concatenate(d) Traceback (most recent call last): TypeError: Two datasets to concatenate have different types <dtype: 'int64'> and <dtype: 'string'> Args: dataset: `Dataset` to be concatenated. Returns: Dataset: A `Dataset`.
15611559prefetchtensorflow/tensorflow/python/data/ops/dataset_ops.py1031methodCreates a `Dataset` that prefetches elements from this dataset. Most dataset input pipelines should end with a call to `prefetch`. This allows later elements to be prepared while the current element is being processed. This often improves latency and throughput, at the cost of using additional memory to store prefetched elements. Note: Like other `Dataset` methods, prefetch operates on the elements of the input dataset. It has no concept of examples vs. batches. `examples.prefetch(2)` will prefetch two elements (2 examples), while `examples.batch(20).prefetch(2)` will prefetch 2 elements (2 batches, of 20 examples each). >>> dataset = tf.data.Dataset.range(3) >>> dataset = dataset.prefetch(2) >>> list(dataset.as_numpy_iterator()) [0, 1, 2] Args: buffer_size: A `tf.int64` scalar `tf.Tensor`, representing the maximum number of elements that will be buffered when prefetching. Returns: Dataset: A `Dataset`.
15621560list_filestensorflow/tensorflow/python/data/ops/dataset_ops.py1060methodA dataset of all files matching one or more glob patterns. The `file_pattern` argument should be a small number of glob patterns. If your filenames have already been globbed, use `Dataset.from_tensor_slices(filenames)` instead, as re-globbing every filename with `list_files` may result in poor performance with remote storage systems. Note: The default behavior of this method is to return filenames in a non-deterministic random shuffled order. Pass a `seed` or `shuffle=False` to get results in a deterministic order. Example: If we had the following files on our filesystem: - /path/to/dir/a.txt - /path/to/dir/b.py - /path/to/dir/c.py If we pass "/path/to/dir/*.py" as the directory, the dataset would produce: - /path/to/dir/b.py - /path/to/dir/c.py Args: file_pattern: A string, a list of strings, or a `tf.Tensor` of string type (scalar or vector), representing the filename glob (i.e. shell wildcard) pattern(s) that will be matched. shuffle: (Optional.) If `True`, the file names will be shuffled randomly. Defaults to `True`. seed: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the random seed that will be used to create the distribution. See `tf.random.set_seed` for behavior. Returns: Dataset: A `Dataset` of strings corresponding to file names.
15631561repeattensorflow/tensorflow/python/data/ops/dataset_ops.py1128methodRepeats this dataset so each original value is seen `count` times. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> dataset = dataset.repeat(3) >>> list(dataset.as_numpy_iterator()) [1, 2, 3, 1, 2, 3, 1, 2, 3] Note: If this dataset is a function of global state (e.g. a random number generator), then different repetitions may produce different elements. Args: count: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the number of times the dataset should be repeated. The default behavior (if `count` is `None` or `-1`) is for the dataset be repeated indefinitely. Returns: Dataset: A `Dataset`.
15641562enumeratetensorflow/tensorflow/python/data/ops/dataset_ops.py1149methodEnumerates the elements of this dataset. It is similar to python's `enumerate`. >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) >>> dataset = dataset.enumerate(start=5) >>> for element in dataset.as_numpy_iterator(): ... print(element) (5, 1) (6, 2) (7, 3) >>> # The nested structure of the input dataset determines the structure of >>> # elements in the resulting dataset. >>> dataset = tf.data.Dataset.from_tensor_slices([(7, 8), (9, 10)]) >>> dataset = dataset.enumerate() >>> for element in dataset.as_numpy_iterator(): ... print(element) (0, array([7, 8], dtype=int32)) (1, array([ 9, 10], dtype=int32)) Args: start: A `tf.int64` scalar `tf.Tensor`, representing the start value for enumeration. Returns: Dataset: A `Dataset`.
15651563shuffletensorflow/tensorflow/python/data/ops/dataset_ops.py1182methodRandomly shuffles the elements of this dataset. This dataset fills a buffer with `buffer_size` elements, then randomly samples elements from this buffer, replacing the selected elements with new elements. For perfect shuffling, a buffer size greater than or equal to the full size of the dataset is required. For instance, if your dataset contains 10,000 elements but `buffer_size` is set to 1,000, then `shuffle` will initially select a random element from only the first 1,000 elements in the buffer. Once an element is selected, its space in the buffer is replaced by the next (i.e. 1,001-st) element, maintaining the 1,000 element buffer. `reshuffle_each_iteration` controls whether the shuffle order should be different for each epoch. In TF 1.X, the idiomatic way to create epochs was through the `repeat` transformation: >>> dataset = tf.data.Dataset.range(3) >>> dataset = dataset.shuffle(3, reshuffle_each_iteration=True) >>> dataset = dataset.repeat(2) # doctest: +SKIP [1, 0, 2, 1, 2, 0] >>> dataset = tf.data.Dataset.range(3) >>> dataset = dataset.shuffle(3, reshuffle_each_iteration=False) >>> dataset = dataset.repeat(2) # doctest: +SKIP [1, 0, 2, 1, 0, 2] In TF 2.0, `tf.data.Dataset` objects are Python iterables which makes it possible to also create epochs through Python iteration: >>> dataset = tf.data.Dataset.range(3) >>> dataset = dataset.shuffle(3, reshuffle_each_iteration=True) >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [1, 0, 2] >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [1, 2, 0] >>> dataset = tf.data.Dataset.range(3) >>> dataset = dataset.shuffle(3, reshuffle_each_iteration=False) >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [1, 0, 2] >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [1, 0, 2] Args: buffer_size: A `tf.int64` scalar `tf.Tensor`, representing the number of elements from this dataset from which the new dataset will sample. seed: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the random seed that will be used to create the distribution. See `tf.random.set_seed` for behavior. reshuffle_each_iteration: (Optional.) A boolean, which if true indicates that the dataset should be pseudorandomly reshuffled each time it is iterated over. (Defaults to `True`.) Returns: Dataset: A `Dataset`.
15661564cachetensorflow/tensorflow/python/data/ops/dataset_ops.py1242methodCaches the elements in this dataset. The first time the dataset is iterated over, its elements will be cached either in the specified file or in memory. Subsequent iterations will use the cached data. Note: For the cache to be finalized, the input dataset must be iterated through in its entirety. Otherwise, subsequent iterations will not use cached data. >>> dataset = tf.data.Dataset.range(5) >>> dataset = dataset.map(lambda x: x**2) >>> dataset = dataset.cache() >>> # The first time reading through the data will generate the data using >>> # `range` and `map`. >>> list(dataset.as_numpy_iterator()) [0, 1, 4, 9, 16] >>> # Subsequent iterations read from the cache. >>> list(dataset.as_numpy_iterator()) [0, 1, 4, 9, 16] When caching to a file, the cached data will persist across runs. Even the first iteration through the data will read from the cache file. Changing the input pipeline before the call to `.cache()` will have no effect until the cache file is removed or the filename is changed. >>> dataset = tf.data.Dataset.range(5) >>> dataset = dataset.cache("/path/to/file") # doctest: +SKIP >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [0, 1, 2, 3, 4] >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.cache("/path/to/file") # Same file! # doctest: +SKIP >>> list(dataset.as_numpy_iterator()) # doctest: +SKIP [0, 1, 2, 3, 4] Note: `cache` will produce exactly the same elements during each iteration through the dataset. If you wish to randomize the iteration order, make sure to call `shuffle` *after* calling `cache`. Args: filename: A `tf.string` scalar `tf.Tensor`, representing the name of a directory on the filesystem to use for caching elements in this Dataset. If a filename is not provided, the dataset will be cached in memory. Returns: Dataset: A `Dataset`.
15671565taketensorflow/tensorflow/python/data/ops/dataset_ops.py1292methodCreates a `Dataset` with at most `count` elements from this dataset. >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.take(3) >>> list(dataset.as_numpy_iterator()) [0, 1, 2] Args: count: A `tf.int64` scalar `tf.Tensor`, representing the number of elements of this dataset that should be taken to form the new dataset. If `count` is -1, or if `count` is greater than the size of this dataset, the new dataset will contain all elements of this dataset. Returns: Dataset: A `Dataset`.
15681566skiptensorflow/tensorflow/python/data/ops/dataset_ops.py1311methodCreates a `Dataset` that skips `count` elements from this dataset. >>> dataset = tf.data.Dataset.range(10) >>> dataset = dataset.skip(7) >>> list(dataset.as_numpy_iterator()) [7, 8, 9] Args: count: A `tf.int64` scalar `tf.Tensor`, representing the number of elements of this dataset that should be skipped to form the new dataset. If `count` is greater than the size of this dataset, the new dataset will contain no elements. If `count` is -1, skips the entire dataset. Returns: Dataset: A `Dataset`.
15691567shardtensorflow/tensorflow/python/data/ops/dataset_ops.py1330methodCreates a `Dataset` that includes only 1/`num_shards` of this dataset. `shard` is deterministic. The Dataset produced by `A.shard(n, i)` will contain all elements of A whose index mod n = i. >>> A = tf.data.Dataset.range(10) >>> B = A.shard(num_shards=3, index=0) >>> list(B.as_numpy_iterator()) [0, 3, 6, 9] >>> C = A.shard(num_shards=3, index=1) >>> list(C.as_numpy_iterator()) [1, 4, 7] >>> D = A.shard(num_shards=3, index=2) >>> list(D.as_numpy_iterator()) [2, 5, 8] This dataset operator is very useful when running distributed training, as it allows each worker to read a unique subset. When reading a single input file, you can shard elements as follows: ```python d = tf.data.TFRecordDataset(input_file) d = d.shard(num_workers, worker_index) d = d.repeat(num_epochs) d = d.shuffle(shuffle_buffer_size) d = d.map(parser_fn, num_parallel_calls=num_map_threads) ``` Important caveats: - Be sure to shard before you use any randomizing operator (such as shuffle). - Generally it is best if the shard operator is used early in the dataset pipeline. For example, when reading from a set of TFRecord files, shard before converting the dataset to input samples. This avoids reading every file on every worker. The following is an example of an efficient sharding strategy within a complete pipeline: ```python d = Dataset.list_files(pattern) d = d.shard(num_workers, worker_index) d = d.repeat(num_epochs) d = d.shuffle(shuffle_buffer_size) d = d.interleave(tf.data.TFRecordDataset, cycle_length=num_readers, block_length=1) d = d.map(parser_fn, num_parallel_calls=num_map_threads) ``` Args: num_shards: A `tf.int64` scalar `tf.Tensor`, representing the number of shards operating in parallel. index: A `tf.int64` scalar `tf.Tensor`, representing the worker index. Returns: Dataset: A `Dataset`. Raises: InvalidArgumentError: if `num_shards` or `index` are illegal values. Note: error checking is done on a best-effort basis, and errors aren't guaranteed to be caught upon dataset creation. (e.g. providing in a placeholder tensor bypasses the early checking, and will instead result in an error during a session.run call.)
15701568batchtensorflow/tensorflow/python/data/ops/dataset_ops.py1398methodCombines consecutive elements of this dataset into batches. >>> dataset = tf.data.Dataset.range(8) >>> dataset = dataset.batch(3) >>> list(dataset.as_numpy_iterator()) [array([0, 1, 2]), array([3, 4, 5]), array([6, 7])] >>> dataset = tf.data.Dataset.range(8) >>> dataset = dataset.batch(3, drop_remainder=True) >>> list(dataset.as_numpy_iterator()) [array([0, 1, 2]), array([3, 4, 5])] The components of the resulting element will have an additional outer dimension, which will be `batch_size` (or `N % batch_size` for the last element if `batch_size` does not divide the number of input elements `N` evenly and `drop_remainder` is `False`). If your program depends on the batches having the same outer dimension, you should set the `drop_remainder` argument to `True` to prevent the smaller batch from being produced. Args: batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. drop_remainder: (Optional.) A `tf.bool` scalar `tf.Tensor`, representing whether the last batch should be dropped in the case it has fewer than `batch_size` elements; the default behavior is not to drop the smaller batch. Returns: Dataset: A `Dataset`.
The file is too large to be shown. View raw