本文整理汇总了Python中tensorflow.assert_positive方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.assert_positive方法的具体用法?Python tensorflow.assert_positive怎么用?Python tensorflow.assert_positive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.assert_positive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_pi_tracenorm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def compute_pi_tracenorm(left_cov, right_cov):
"""Computes the scalar constant pi for Tikhonov regularization/damping.
pi = sqrt( (trace(A) / dim(A)) / (trace(B) / dim(B)) )
See section 6.3 of https://arxiv.org/pdf/1503.05671.pdf for details.
Args:
left_cov: A LinearOperator object. The left Kronecker factor "covariance".
right_cov: A LinearOperator object. The right Kronecker factor "covariance".
Returns:
The computed scalar constant pi for these Kronecker Factors (as a Tensor).
"""
# Instead of dividing by the dim of the norm, we multiply by the dim of the
# other norm. This works out the same in the ratio.
left_norm = left_cov.trace() * int(right_cov.domain_dimension)
right_norm = right_cov.trace() * int(left_cov.domain_dimension)
assert_positive = tf.assert_positive(
right_norm,
message="PI computation, trace of right cov matrix should be positive. "
"Note that most likely cause of this error is that the optimizer "
"diverged (e.g. due to bad hyperparameters).")
with tf.control_dependencies([assert_positive]):
pi = tf.sqrt(left_norm / right_norm)
return pi
示例2: build_cross_entropy_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def build_cross_entropy_loss(logits, gold):
"""Constructs a cross entropy from logits and one-hot encoded gold labels.
Supports skipping rows where the gold label is the magic -1 value.
Args:
logits: float Tensor of scores.
gold: int Tensor of one-hot labels.
Returns:
cost, correct, total: the total cost, the total number of correctly
predicted labels, and the total number of valid labels.
"""
valid = tf.reshape(tf.where(tf.greater(gold, -1)), [-1])
gold = tf.gather(gold, valid)
logits = tf.gather(logits, valid)
correct = tf.reduce_sum(tf.to_int32(tf.nn.in_top_k(logits, gold, 1)))
total = tf.size(gold)
with tf.control_dependencies([tf.assert_positive(total)]):
cost = tf.reduce_sum(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.cast(gold, tf.int64), logits=logits)) / tf.cast(
total, tf.float32)
return cost, correct, total
示例3: build_cross_entropy_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def build_cross_entropy_loss(logits, gold):
"""Constructs a cross entropy from logits and one-hot encoded gold labels.
Supports skipping rows where the gold label is the magic -1 value.
Args:
logits: float Tensor of scores.
gold: int Tensor of gold label ids.
Returns:
cost, correct, total: the total cost, the total number of correctly
predicted labels, and the total number of valid labels.
"""
valid = tf.reshape(tf.where(tf.greater(gold, -1)), [-1])
gold = tf.gather(gold, valid)
logits = tf.gather(logits, valid)
correct = tf.reduce_sum(tf.to_int32(tf.nn.in_top_k(logits, gold, 1)))
total = tf.size(gold)
with tf.control_dependencies([tf.assert_positive(total)]):
cost = tf.reduce_sum(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tf.cast(gold, tf.int64), logits=logits)) / tf.cast(
total, tf.float32)
return cost, correct, total
示例4: ptb_producer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def ptb_producer(raw_data, batch_size, num_steps, name=None):
"""Iterate on the raw PTB data.
This chunks up raw_data into batches of examples and returns Tensors that
are drawn from these batches.
Args:
raw_data: one of the raw data outputs from ptb_raw_data.
batch_size: int, the batch size.
num_steps: int, the number of unrolls.
name: the name of this operation (optional).
Returns:
A pair of Tensors, each shaped [batch_size, num_steps]. The second element
of the tuple is the same data time-shifted to the right by one.
Raises:
tf.errors.InvalidArgumentError: if batch_size or num_steps are too high.
"""
with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):
raw_data = tf.convert_to_tensor(
raw_data, name="raw_data", dtype=tf.int32)
data_len = tf.size(raw_data)
batch_len = data_len // batch_size
data = tf.reshape(raw_data[0: batch_size * batch_len],
[batch_size, batch_len])
epoch_size = (batch_len - 1) // num_steps
assertion = tf.assert_positive(
epoch_size,
message="epoch_size == 0, decrease batch_size or num_steps")
with tf.control_dependencies([assertion]):
epoch_size = tf.identity(epoch_size, name="epoch_size")
i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
x = tf.strided_slice(data, [0, i * num_steps],
[batch_size, (i + 1) * num_steps])
x.set_shape([batch_size, num_steps])
y = tf.strided_slice(data, [0, i * num_steps + 1],
[batch_size, (i + 1) * num_steps + 1])
y.set_shape([batch_size, num_steps])
return x, y
示例5: Kdiag
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def Kdiag(self, X, presliced=False):
kdiag = 1e-4 # some jitter
for variances in self.variances:
kdiag += tf.square(self._interpolate(X, variances, self.Kvar, transform=tf.nn.softplus))
with tf.control_dependencies([tf.assert_positive(kdiag, message='Kdiag negative: ')]):
# kdiag = tf.Print(kdiag, [kdiag], 'kdiag: ')
kdiag = tf.identity(kdiag)
return tf.squeeze(kdiag)
示例6: _build_likelihood
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def _build_likelihood(self):
"""
This gives a variational bound on the model likelihood.
Adds also the prior term for the GP parameters.
"""
# Get prior KL.
KL = self.build_prior_KL()
# Get conditionals
fmean, fvar = self._build_predict(self.X, full_cov=False)
#with tf.control_dependencies([tf.assert_positive(fvar, message='fvar negative: ')]):
# # Get variational expectations.
var_exp = self.likelihood.variational_expectations(fmean, fvar, self.Y)
# re-scale var_exp for minibatch size
scale = (tf.cast(self.num_data, settings.tf_float)
/ tf.cast(tf.shape(self.X)[0], settings.tf_float))
var_exp = tf.reduce_sum(var_exp) * scale
# latent functions have a whitened GP prior
prior = float_type(0.0)
for vars in zip(self.frequencies, self.variances, self.lengthscales):
prior += -0.5 * sum(tf.reduce_sum(tf.square(x)) for x in vars)
# re-scale prior for inducing point size
# scale = tf.cast(self.num_data, settings.tf_float) / tf.cast(self.num_inducing, settings.tf_float)
# prior = prior * scale
# print tensors
#var_exp = tf.Print(var_exp, [var_exp], message='var_exp:')
#KL = tf.Print(KL, [KL], message='KL:')
#prior = tf.Print(prior, [prior], message='prior:')
likelihood = var_exp - KL + prior
likelihood = tf.Print(likelihood, [likelihood], 'likelihood')
return likelihood
示例7: test_raises_when_negative
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def test_raises_when_negative(self):
with self.test_session():
freddie = tf.constant([-1, -2], name="freddie")
with tf.control_dependencies(
[tf.assert_positive(freddie, message="fail")]):
out = tf.identity(freddie)
with self.assertRaisesOpError("fail.*freddie"):
out.eval()
示例8: test_doesnt_raise_when_positive
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def test_doesnt_raise_when_positive(self):
with self.test_session():
remmy = tf.constant([1, 2], name="remmy")
with tf.control_dependencies([tf.assert_positive(remmy)]):
out = tf.identity(remmy)
out.eval()
示例9: test_raises_when_zero
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def test_raises_when_zero(self):
with self.test_session():
meechum = tf.constant([0], name="meechum")
with tf.control_dependencies([tf.assert_positive(meechum)]):
out = tf.identity(meechum)
with self.assertRaisesOpError("meechum"):
out.eval()
示例10: test_empty_tensor_doesnt_raise
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def test_empty_tensor_doesnt_raise(self):
# A tensor is positive when it satisfies:
# For every element x_i in x, x_i > 0
# and an empty tensor has no elements, so this is trivially satisfied.
# This is standard set theory.
with self.test_session():
empty = tf.constant([], name="empty")
with tf.control_dependencies([tf.assert_positive(empty)]):
out = tf.identity(empty)
out.eval()
示例11: ptb_producer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def ptb_producer(raw_data, batch_size, num_steps, name=None):
"""Iterate on the raw PTB data.
This chunks up raw_data into batches of examples and returns Tensors that
are drawn from these batches.
Args:
raw_data: one of the raw data outputs from ptb_raw_data.
batch_size: int, the batch size.
num_steps: int, the number of unrolls.
name: the name of this operation (optional).
Returns:
A pair of Tensors, each shaped [batch_size, num_steps]. The second element
of the tuple is the same data time-shifted to the right by one.
Raises:
tf.errors.InvalidArgumentError: if batch_size or num_steps are too high.
"""
with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):
raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)
data_len = tf.size(raw_data)
batch_len = data_len // batch_size
data = tf.reshape(raw_data[0 : batch_size * batch_len],
[batch_size, batch_len])
epoch_size = (batch_len - 1) // num_steps
assertion = tf.assert_positive(
epoch_size,
message="epoch_size == 0, decrease batch_size or num_steps")
with tf.control_dependencies([assertion]):
epoch_size = tf.identity(epoch_size, name="epoch_size")
i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
x = tf.slice(data, [0, i * num_steps], [batch_size, num_steps])
y = tf.slice(data, [0, i * num_steps + 1], [batch_size, num_steps])
return x, y
示例12: check_3d_image
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def check_3d_image(image, require_static=True):
"""Assert that we are working with properly shaped image.
Args:
image: 3-D Tensor of shape [height, width, channels]
require_static: If `True`, requires that all dimensions of `image` are
known and non-zero.
Raises:
ValueError: if `image.shape` is not a 3-vector.
Returns:
An empty list, if `image` has fully defined dimensions. Otherwise, a list
containing an assert op is returned.
"""
try:
image_shape = image.get_shape().with_rank(3)
except ValueError:
raise ValueError("'image' must be three-dimensional.")
if require_static and not image_shape.is_fully_defined():
raise ValueError("'image' must be fully defined.")
if any(x == 0 for x in image_shape):
raise ValueError("all dims of 'image.shape' must be > 0: %s" %
image_shape)
if not image_shape.is_fully_defined():
return [tf.assert_positive(tf.shape(image),
["all dims of 'image.shape' "
"must be > 0."])]
else:
return []
示例13: reduce_mean
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def reduce_mean(seq_batch, allow_empty=False):
"""Compute the mean of each sequence in a SequenceBatch.
Args:
seq_batch (SequenceBatch): a SequenceBatch with the following attributes:
values (Tensor): a Tensor of shape (batch_size, seq_length, :, ..., :)
mask (Tensor): if the mask values are arbitrary floats (rather than binary), the mean will be
a weighted average.
allow_empty (bool): allow computing the average of an empty sequence. In this case, we assume 0/0 == 0, rather
than NaN. Default is False, causing an error to be thrown.
Returns:
Tensor: of shape (batch_size, :, ..., :)
"""
values, mask = seq_batch.values, seq_batch.mask
# compute weights for the average
sums = tf.reduce_sum(mask, 1, keep_dims=True) # (batch_size, 1)
if allow_empty:
asserts = [] # no assertion
sums = tf.select(tf.equal(sums, 0), tf.ones(tf.shape(sums)), sums) # replace 0's with 1's
else:
asserts = [tf.assert_positive(sums)] # throw error if 0's exist
with tf.control_dependencies(asserts):
weights = mask / sums # (batch_size, seq_length)
return weighted_sum(seq_batch, weights)
示例14: reduce_max
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def reduce_max(seq_batch):
sums = tf.reduce_sum(seq_batch.mask, 1, keep_dims=True) # (batch_size, 1)
with tf.control_dependencies([tf.assert_positive(sums)]): # avoid dividing by zero
seq_batch = seq_batch.with_pad_value(float('-inf')) # set pad values to -inf
result = tf.reduce_max(seq_batch.values, 1)
return result
示例15: ptb_producer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assert_positive [as 别名]
def ptb_producer(raw_data, batch_size, num_steps, name=None):
"""Iterate on the raw PTB data.
This chunks up raw_data into batches of examples and returns Tensors that
are drawn from these batches.
Args:
raw_data: one of the raw data outputs from ptb_raw_data.
batch_size: int, the batch size.
num_steps: int, the number of unrolls.
name: the name of this operation (optional).
Returns:
A pair of Tensors, each shaped [batch_size, num_steps]. The second element
of the tuple is the same data time-shifted to the right by one.
Raises:
tf.errors.InvalidArgumentError: if batch_size or num_steps are too high.
"""
with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):
raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)
data_len = tf.size(raw_data)
batch_len = data_len // batch_size
data = tf.reshape(raw_data[0 : batch_size * batch_len],
[batch_size, batch_len])
epoch_size = (batch_len - 1) // num_steps
assertion = tf.assert_positive(
epoch_size,
message="epoch_size == 0, decrease batch_size or num_steps")
with tf.control_dependencies([assertion]):
epoch_size = tf.identity(epoch_size, name="epoch_size")
i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()
x = tf.strided_slice(data, [0, i * num_steps],
[batch_size, (i + 1) * num_steps])
x.set_shape([batch_size, num_steps])
y = tf.strided_slice(data, [0, i * num_steps + 1],
[batch_size, (i + 1) * num_steps + 1])
y.set_shape([batch_size, num_steps])
return x, y