本文整理汇总了Python中tensorflow.assert_greater_equal方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.assert_greater_equal方法的具体用法?Python tensorflow.assert_greater_equal怎么用?Python tensorflow.assert_greater_equal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.assert_greater_equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def __call__(self, x, y):
'''
Return K(x, y), where x and y are possibly batched.
:param x: shape [..., n_x, n_covariates]
:param y: shape [..., n_y, n_covariates]
:return: Tensor with shape [..., n_x, n_y]
'''
batch_shape = tf.shape(x)[:-2]
rank = x.shape.ndims
assert_ops = [
tf.assert_greater_equal(
rank, 2,
message='RBFKernel: rank(x) should be static and >=2'),
tf.assert_equal(
rank, tf.rank(y),
message='RBFKernel: x and y should have the same rank')]
with tf.control_dependencies(assert_ops):
x = tf.expand_dims(x, rank - 1)
y = tf.expand_dims(y, rank - 2)
k_scale = tf.reshape(self.k_scale, [1] * rank + [-1])
ret = tf.exp(
-tf.reduce_sum(tf.square(x - y) / k_scale, axis=-1) / 2)
return ret
示例2: sparse_softmax_cross_entropy
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def sparse_softmax_cross_entropy(labels,
logits,
num_classes,
weights=1.0,
label_smoothing=0.1):
"""Softmax cross entropy with example weights, label smoothing."""
assert_valid_label = [
tf.assert_greater_equal(labels, tf.cast(0, dtype=tf.int64)),
tf.assert_less(labels, tf.cast(num_classes, dtype=tf.int64))
]
with tf.control_dependencies(assert_valid_label):
labels = tf.reshape(labels, [-1])
dense_labels = tf.one_hot(labels, num_classes)
loss = tf.losses.softmax_cross_entropy(
onehot_labels=dense_labels,
logits=logits,
weights=weights,
label_smoothing=label_smoothing)
return loss
示例3: _preprocess_for_inception
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def _preprocess_for_inception(images):
"""Preprocess images for inception.
Args:
images: images minibatch. Shape [batch size, width, height,
channels]. Values are in [0..255].
Returns:
preprocessed_images
"""
images = tf.cast(images, tf.float32)
# tfgan_eval.preprocess_image function takes values in [0, 255]
with tf.control_dependencies([tf.assert_greater_equal(images, 0.0),
tf.assert_less_equal(images, 255.0)]):
images = tf.identity(images)
preprocessed_images = tf.map_fn(
fn=_TFGAN.preprocess_image,
elems=images,
back_prop=False)
return preprocessed_images
示例4: _project_perturbation
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def _project_perturbation(perturbation, epsilon, input_image):
"""Project `perturbation` onto L-infinity ball of radius `epsilon`."""
# Ensure inputs are in the correct range
with tf.control_dependencies([
tf.assert_less_equal(input_image, 1.0),
tf.assert_greater_equal(input_image, 0.0)
]):
clipped_perturbation = tf.clip_by_value(
perturbation, -epsilon, epsilon)
new_image = tf.clip_by_value(
input_image + clipped_perturbation, 0., 1.)
return new_image - input_image
示例5: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def __init__(self,
alpha,
group_ndims=0,
check_numerics=False,
**kwargs):
self._alpha = tf.convert_to_tensor(alpha)
dtype = assert_same_float_dtype(
[(self._alpha, 'Dirichlet.alpha')])
static_alpha_shape = self._alpha.get_shape()
shape_err_msg = "alpha should have rank >= 1."
cat_err_msg = "n_categories (length of the last axis " \
"of alpha) should be at least 2."
if static_alpha_shape and (static_alpha_shape.ndims < 1):
raise ValueError(shape_err_msg)
elif static_alpha_shape and (
static_alpha_shape[-1].value is not None):
self._n_categories = static_alpha_shape[-1].value
if self._n_categories < 2:
raise ValueError(cat_err_msg)
else:
_assert_shape_op = tf.assert_rank_at_least(
self._alpha, 1, message=shape_err_msg)
with tf.control_dependencies([_assert_shape_op]):
self._alpha = tf.identity(self._alpha)
self._n_categories = tf.shape(self._alpha)[-1]
_assert_cat_op = tf.assert_greater_equal(
self._n_categories, 2, message=cat_err_msg)
with tf.control_dependencies([_assert_cat_op]):
self._alpha = tf.identity(self._alpha)
self._check_numerics = check_numerics
super(Dirichlet, self).__init__(
dtype=dtype,
param_dtype=dtype,
is_continuous=True,
is_reparameterized=False,
group_ndims=group_ndims,
**kwargs)
示例6: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def __init__(self,
dtype,
param_dtype,
is_continuous,
is_reparameterized,
use_path_derivative=False,
group_ndims=0,
**kwargs):
if 'group_event_ndims' in kwargs:
raise ValueError(
"The argument `group_event_ndims` has been deprecated "
"Please use `group_ndims` instead.")
self._dtype = dtype
self._param_dtype = param_dtype
self._is_continuous = is_continuous
self._is_reparameterized = is_reparameterized
self._use_path_derivative = use_path_derivative
if isinstance(group_ndims, int):
if group_ndims < 0:
raise ValueError("group_ndims must be non-negative.")
self._group_ndims = group_ndims
else:
group_ndims = tf.convert_to_tensor(group_ndims, tf.int32)
_assert_rank_op = tf.assert_rank(
group_ndims, 0,
message="group_ndims should be a scalar (0-D Tensor).")
_assert_nonnegative_op = tf.assert_greater_equal(
group_ndims, 0,
message="group_ndims must be non-negative.")
with tf.control_dependencies([_assert_rank_op,
_assert_nonnegative_op]):
self._group_ndims = tf.identity(group_ndims)
示例7: check_nonnegative
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def check_nonnegative(value):
"""Check that the value is nonnegative."""
if isinstance(value, tf.Tensor):
with tf.control_dependencies([tf.assert_greater_equal(value, 0)]):
value = tf.identity(value)
elif value < 0:
raise ValueError("Value must be non-negative.")
return value
示例8: pre_attention
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def pre_attention(self, segment_number, query_antecedent,
memory_antecedent, bias):
"""Called prior to self-attention, to incorporate memory items.
Args:
segment_number: an integer Tensor with shape [batch]
query_antecedent: a Tensor with shape [batch, length_q, channels]
memory_antecedent: must be None. Attention normally allows this to be a
Tensor with shape [batch, length_m, channels], but we currently only
support memory for decoder-side self-attention.
bias: bias Tensor (see attention_bias())
Returns:
(data, new_query_antecedent, new_memory_antecedent, new_bias)
"""
with tf.variable_scope(self.name + "/pre_attention", reuse=tf.AUTO_REUSE):
assert memory_antecedent is None, "We only support language modeling"
with tf.control_dependencies([
tf.assert_greater_equal(self.batch_size, tf.size(segment_number))]):
difference = self.batch_size - tf.size(segment_number)
segment_number = tf.pad(segment_number, [[0, difference]])
reset_op = self.reset(tf.reshape(tf.where(
tf.less(segment_number, self.segment_number)), [-1]))
memory_results = {}
with tf.control_dependencies([reset_op]):
with tf.control_dependencies([
self.update_segment_number(segment_number)]):
x = tf.pad(query_antecedent, [
[0, difference], [0, 0], [0, 0]])
access_logits, retrieved_mem = self.read(x)
memory_results["x"] = x
memory_results["access_logits"] = access_logits
memory_results["retrieved_mem"] = retrieved_mem
return memory_results, query_antecedent, memory_antecedent, bias
示例9: scale_to_inception_range
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def scale_to_inception_range(image):
"""Scales an image in the range [0,1] to [-1,1] as expected by inception."""
# Assert that incoming images have been properly scaled to [0,1].
with tf.control_dependencies(
[tf.assert_less_equal(tf.reduce_max(image), 1.),
tf.assert_greater_equal(tf.reduce_min(image), 0.)]):
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
return image
示例10: new_mean_squared
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def new_mean_squared(grad_vec, decay, ms):
"""Calculates the new accumulated mean squared of the gradient.
Args:
grad_vec: the vector for the current gradient
decay: the decay term
ms: the previous mean_squared value
Returns:
the new mean_squared value
"""
decay_size = decay.get_shape().num_elements()
decay_check_ops = [
tf.assert_less_equal(decay, 1., summarize=decay_size),
tf.assert_greater_equal(decay, 0., summarize=decay_size)]
with tf.control_dependencies(decay_check_ops):
grad_squared = tf.square(grad_vec)
# If the previous mean_squared is the 0 vector, don't use the decay and just
# return the full grad_squared. This should only happen on the first timestep.
decay = tf.cond(tf.reduce_all(tf.equal(ms, 0.)),
lambda: tf.zeros_like(decay, dtype=tf.float32), lambda: decay)
# Update the running average of squared gradients.
epsilon = 1e-12
return (1. - decay) * (grad_squared + epsilon) + decay * ms
示例11: test_doesnt_raise_when_equal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def test_doesnt_raise_when_equal(self):
with self.test_session():
small = tf.constant([1, 2], name="small")
with tf.control_dependencies([tf.assert_greater_equal(small, small)]):
out = tf.identity(small)
out.eval()
示例12: test_raises_when_less
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def test_raises_when_less(self):
with self.test_session():
small = tf.constant([1, 2], name="small")
big = tf.constant([3, 4], name="big")
with tf.control_dependencies(
[tf.assert_greater_equal(small, big, message="fail")]):
out = tf.identity(small)
with self.assertRaisesOpError("fail.*small.*big"):
out.eval()
示例13: test_doesnt_raise_when_greater_equal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def test_doesnt_raise_when_greater_equal(self):
with self.test_session():
small = tf.constant([1, 2], name="small")
big = tf.constant([3, 2], name="big")
with tf.control_dependencies([tf.assert_greater_equal(big, small)]):
out = tf.identity(small)
out.eval()
示例14: test_doesnt_raise_when_greater_equal_and_broadcastable_shapes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def test_doesnt_raise_when_greater_equal_and_broadcastable_shapes(self):
with self.test_session():
small = tf.constant([1], name="small")
big = tf.constant([3, 1], name="big")
with tf.control_dependencies([tf.assert_greater_equal(big, small)]):
out = tf.identity(small)
out.eval()
示例15: test_raises_when_less_equal_but_non_broadcastable_shapes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_greater_equal [as 别名]
def test_raises_when_less_equal_but_non_broadcastable_shapes(self):
with self.test_session():
small = tf.constant([1, 1, 1], name="big")
big = tf.constant([3, 1], name="small")
with self.assertRaisesRegexp(ValueError, "Dimensions must be equal"):
with tf.control_dependencies([tf.assert_greater_equal(big, small)]):
out = tf.identity(small)
out.eval()