tfp.experimental.auto_batching.truthy
Stay organized with collections
Save and categorize content based on your preferences.
Normalizes Tensor ranks for use in if
conditions.
tfp.experimental.auto_batching.truthy(
x
)
This enables dry-runs of programs with control flow. Usage: Program the
conditions of if
statements and while
loops to have a batch dimension, and
then wrap them with this function. Example:
ctx = frontend.Context
truthy = frontend.truthy
@ctx.batch(type_inference=...)
def my_abs(x):
if truthy(x > 0):
return x
else:
return -x
my_abs([-5], dry_run=True)
# returns [5] in Eager mode
This is necessary because auto-batched programs still have a leading batch
dimension (of size 1) even in dry-run mode, and a Tensor of shape [1] is not
acceptable as the condition to an if
or while
. However, the leading
dimension is critical during batched execution; so conditions of ifs need to
have rank 1 if running batched and rank 0 if running unbatched (i.e.,
dry-run). The truthy
function arranges for this be happen (by detecting
whether it is in dry-run mode or not).
If you missed a spot where you should have used truthy
, the error message
will say Non-scalar tensor <Tensor ...> cannot be converted to boolean.
Returns |
x
|
The Tensor x if we are in batch mode, or if the shape of x is
anything other than [1] . Otherwise returns the single scalar in x as
a Tensor of scalar shape (which is acceptable in the conditions of if
and while statements.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2023-11-21 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-11-21 UTC."],[],[]]