def piecewise_constant(x, boundaries, values):
""" Piecewise constant function.
Arguments:
x: A 0-D Tensor.
boundaries: A 1-D NumPy array with strictly increasing entries.
values: A 1-D NumPy array that specifies the values for the intervals
defined by `boundaries`. (It should therefore have one more entry
than `boundaries`.)
Returns: A 0-D Tensor. Its value is `values[0]` when `x <= boundaries[0]`,
`values[1]` when `x > boundaries[0]` and `x <= boundaries[1]`, ..., and
values[-1] when `x > boundaries[-1]`.
"""
pred_fn_pairs = {}
pred_fn_pairs[x <= boundaries[0]] = lambda: tf.constant(values[0])
pred_fn_pairs[x > boundaries[-1]] = lambda: tf.constant(values[-1])
for lower, upper, value in zip(boundaries[:-1],
boundaries[1:],
values[1:-1]):
# We need to bind value here; can do this with lambda value=value: ...
pred = (x > lower) & (x <= upper)
pred_fn_pairs[pred] = lambda value=value: tf.constant(value)
return tf.case(pred_fn_pairs, lambda: tf.constant(values[0]),
exclusive=True)
optimizers.py 文件源码
python
阅读 32
收藏 0
点赞 0
评论 0
评论列表
文章目录