本文整理汇总了Python中tensorflow.python.ops.check_ops.assert_integer函数的典型用法代码示例。如果您正苦于以下问题:Python assert_integer函数的具体用法?Python assert_integer怎么用?Python assert_integer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_integer函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _verify_input
def _verify_input(tensor_list, labels, probs_list):
"""Verify that batched inputs are well-formed."""
checked_probs_list = []
for probs in probs_list:
# Since number of classes shouldn't change at runtime, probabilities shape
# should be fully defined.
probs.get_shape().assert_is_fully_defined()
# Probabilities must be 1D.
probs.get_shape().assert_has_rank(1)
# Probabilities must be nonnegative and sum to one.
tol = 1e-6
prob_sum = math_ops.reduce_sum(probs)
checked_probs = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(probs),
check_ops.assert_less(prob_sum, 1.0 + tol),
check_ops.assert_less(1.0 - tol, prob_sum)
], probs)
checked_probs_list.append(checked_probs)
# All probabilities should be the same length.
prob_length = checked_probs_list[0].get_shape().num_elements()
for checked_prob in checked_probs_list:
if checked_prob.get_shape().num_elements() != prob_length:
raise ValueError('Probability parameters must have the same length.')
# Labels tensor should only have batch dimension.
labels.get_shape().assert_has_rank(1)
for tensor in tensor_list:
# Data tensor should have a batch dimension.
shape = tensor.get_shape().with_rank_at_least(1)
# Data and label batch dimensions must be compatible.
tensor_shape.dimension_at_index(shape, 0).assert_is_compatible_with(
labels.get_shape()[0])
# Data and labels must have the same, strictly positive batch size. Since we
# can't assume we know the batch size at graph creation, add runtime checks.
labels_batch_size = array_ops.shape(labels)[0]
lbl_assert = check_ops.assert_positive(labels_batch_size)
# Make each tensor depend on its own checks.
labels = control_flow_ops.with_dependencies([lbl_assert], labels)
tensor_list = [
control_flow_ops.with_dependencies([
lbl_assert,
check_ops.assert_equal(array_ops.shape(x)[0], labels_batch_size)
], x) for x in tensor_list
]
# Label's classes must be integers 0 <= x < num_classes.
labels = control_flow_ops.with_dependencies([
check_ops.assert_integer(labels), check_ops.assert_non_negative(labels),
check_ops.assert_less(labels, math_ops.cast(prob_length, labels.dtype))
], labels)
return tensor_list, labels, checked_probs_list
开发者ID:Ajaycs99,项目名称:tensorflow,代码行数:59,代码来源:sampling_ops.py
示例2: _verify_input
def _verify_input(data, labels, probs_list):
"""Verify that batched inputs are well-formed."""
checked_probs_list = []
for probs in probs_list:
# Probabilities must be able to be converted to non-object numpy array.
np_probs = np.asarray(probs)
if np_probs.dtype == np.dtype('object'):
raise ValueError('Probabilities must be able to be converted to a numpy '
'array.')
checked_probs_list.append(np_probs)
# Probabilities must sum to one.
# TODO(joelshor): Investigate whether logits should be passed instead of
# probs.
if not np.isclose(np.sum(probs), 1.0):
raise ValueError('Probabilities must sum to one.')
# All probabilities should be the same length.
if not np.array_equiv([probs.shape for probs in checked_probs_list],
checked_probs_list[0].shape):
raise ValueError('Probability parameters must have the same length.')
# Labels tensor should only have batch dimension.
labels.get_shape().assert_has_rank(1)
# Data tensor should have a batch dimension.
data_shape = data.get_shape().with_rank_at_least(1)
# Data and label batch dimensions must be compatible.
data_shape[0].assert_is_compatible_with(labels.get_shape()[0])
# Data and labels must have the same, strictly positive batch size. Since we
# can't assume we know the batch size at graph creation, add runtime checks.
data_batch_size = array_ops.shape(data)[0]
labels_batch_size = array_ops.shape(labels)[0]
data = control_flow_ops.with_dependencies(
[check_ops.assert_positive(data_batch_size),
check_ops.assert_equal(data_batch_size, labels_batch_size)],
data)
# Label's classes must be integers 0 <= x < num_classes.
labels = control_flow_ops.with_dependencies(
[check_ops.assert_integer(labels),
check_ops.assert_non_negative(labels),
check_ops.assert_less(labels, math_ops.cast(len(probs), labels.dtype))],
labels)
return data, labels, checked_probs_list
开发者ID:Bala96,项目名称:tensorflow,代码行数:49,代码来源:sampling_ops.py
示例3: _verify_input
def _verify_input(data, labels, probs):
"""Verify that batched inputs are well-formed."""
# Probabilities must be a numpy array or a Python list.
if not (isinstance(probs, np.ndarray) or isinstance(probs, list)):
raise ValueError('Probabilities must be python or numpy array')
# Probabilities must sum to one.
# TODO(joelshor): Investigate whether logits should be passed instead of
# probs.
if np.sum(probs) != 1.0:
raise ValueError('Probabilities must sum to one.')
# Labels tensor should only have batch dimension.
labels.get_shape().assert_has_rank(1)
# Data tensor should have a batch dimension.
data_shape = data.get_shape().with_rank_at_least(1)
# Data and label batch dimensions must be compatible.
data_shape[0].assert_is_compatible_with(labels.get_shape()[0])
# Data and labels must have the same, strictly positive batch size. Since we
# can't assume we know the batch size at graph creation, add runtime checks.
data_batch_size = array_ops.shape(data)[0]
labels_batch_size = array_ops.shape(labels)[0]
data = control_flow_ops.with_dependencies(
[check_ops.assert_positive(data_batch_size),
check_ops.assert_equal(data_batch_size, labels_batch_size)],
data)
# Label's classes must be integers 0 <= x < num_classes.
labels = control_flow_ops.with_dependencies(
[check_ops.assert_integer(labels),
check_ops.assert_non_negative(labels),
check_ops.assert_less(labels, math_ops.cast(len(probs), labels.dtype))],
labels)
return data, labels
开发者ID:285219011,项目名称:hello-world,代码行数:39,代码来源:sampling_ops.py
示例4: _process_labels
def _process_labels(self, labels):
if labels is None:
raise ValueError(
'You must provide a labels Tensor. Given: None. '
'Suggested troubleshooting steps: Check that your data contain '
'your label feature. Check that your input_fn properly parses and '
'returns labels.')
if isinstance(labels, sparse_tensor.SparseTensor):
if labels.dtype == dtypes.string:
label_ids_values = lookup_ops.index_table_from_tensor(
vocabulary_list=tuple(self._label_vocabulary),
name='class_id_lookup').lookup(labels.values)
label_ids = sparse_tensor.SparseTensor(
indices=labels.indices,
values=label_ids_values,
dense_shape=labels.dense_shape)
return math_ops.to_int64(
sparse_ops.sparse_to_indicator(label_ids, self._n_classes))
else:
err_msg = (
r'labels must be an integer SparseTensor with values in '
r'[0, {})'.format(self._n_classes))
assert_int = check_ops.assert_integer(
labels.values, message=err_msg)
assert_less = check_ops.assert_less(
labels.values,
ops.convert_to_tensor(self._n_classes, dtype=labels.dtype),
message=err_msg)
assert_greater = check_ops.assert_non_negative(
labels.values, message=err_msg)
with ops.control_dependencies(
[assert_int, assert_less, assert_greater]):
return math_ops.to_int64(
sparse_ops.sparse_to_indicator(labels, self._n_classes))
err_msg = (
r'labels must be an integer indicator Tensor with values in [0, 1]')
return head_lib._assert_range(labels, 2, message=err_msg) # pylint:disable=protected-access,
开发者ID:didukhle,项目名称:tensorflow,代码行数:37,代码来源:head.py
示例5: test_raises_when_float
def test_raises_when_float(self):
with self.test_session():
floats = constant_op.constant([1.0, 2.0], name="floats")
with self.assertRaisesRegexp(TypeError, "Expected.*integer"):
check_ops.assert_integer(floats)
开发者ID:1000sprites,项目名称:tensorflow,代码行数:5,代码来源:check_ops_test.py
示例6: test_doesnt_raise_when_integer
def test_doesnt_raise_when_integer(self):
with self.test_session():
integers = constant_op.constant([1, 2], name="integers")
with ops.control_dependencies([check_ops.assert_integer(integers)]):
out = array_ops.identity(integers)
out.eval()
开发者ID:1000sprites,项目名称:tensorflow,代码行数:6,代码来源:check_ops_test.py
示例7: percentile
#.........这里部分代码省略.........
percentile. If `None` (the default), treat every dimension as a sample
dimension, returning a scalar.
interpolation : {"lower", "higher", "nearest"}. Default: "nearest"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points `i < j`:
* lower: `i`.
* higher: `j`.
* nearest: `i` or `j`, whichever is nearest.
keep_dims: Python `bool`. If `True`, the last dimension is kept with size 1
If `False`, the last dimension is removed from the output shape.
validate_args: Whether to add runtime checks of argument validity.
If False, and arguments are incorrect, correct behavior is not guaranteed.
name: A Python string name to give this `Op`. Default is "percentile"
Returns:
A `(N - len(axis))` dimensional `Tensor` of same dtype as `x`, or, if
`axis` is `None`, a scalar.
Raises:
ValueError: If argument 'interpolation' is not an allowed type.
"""
name = name or "percentile"
allowed_interpolations = {"lower", "higher", "nearest"}
if interpolation is None:
interpolation = "nearest"
else:
if interpolation not in allowed_interpolations:
raise ValueError("Argument 'interpolation' must be in %s. Found %s" %
(allowed_interpolations, interpolation))
with ops.name_scope(name, [x, q]):
x = ops.convert_to_tensor(x, name="x")
# Double is needed here and below, else we get the wrong index if the array
# is huge along axis.
q = math_ops.to_double(q, name="q")
_get_static_ndims(q, expect_ndims=0)
if validate_args:
q = control_flow_ops.with_dependencies([
check_ops.assert_rank(q, 0),
check_ops.assert_greater_equal(q, math_ops.to_double(0.)),
check_ops.assert_less_equal(q, math_ops.to_double(100.))
], q)
if axis is None:
y = array_ops.reshape(x, [-1])
else:
axis = ops.convert_to_tensor(axis, name="axis")
check_ops.assert_integer(axis)
axis_ndims = _get_static_ndims(
axis, expect_static=True, expect_ndims_no_more_than=1)
axis_const = tensor_util.constant_value(axis)
if axis_const is None:
raise ValueError(
"Expected argument 'axis' to be statically available. Found: %s" %
axis)
axis = axis_const
if axis_ndims == 0:
axis = [axis]
axis = [int(a) for a in axis]
x_ndims = _get_static_ndims(
x, expect_static=True, expect_ndims_at_least=1)
axis = _make_static_axis_non_negative(axis, x_ndims)
y = _move_dims_to_flat_end(x, axis, x_ndims)
frac_at_q_or_above = 1. - q / 100.
d = math_ops.to_double(array_ops.shape(y)[-1])
if interpolation == "lower":
index = math_ops.ceil((d - 1) * frac_at_q_or_above)
elif interpolation == "higher":
index = math_ops.floor((d - 1) * frac_at_q_or_above)
elif interpolation == "nearest":
index = math_ops.round((d - 1) * frac_at_q_or_above)
# If d is gigantic, then we would have d == d - 1, even in double... So
# let's use max/min to avoid out of bounds errors.
d = array_ops.shape(y)[-1]
# d - 1 will be distinct from d in int32.
index = clip_ops.clip_by_value(math_ops.to_int32(index), 0, d - 1)
# Sort everything, not just the top 'k' entries, which allows multiple calls
# to sort only once (under the hood) and use CSE.
sorted_y = _sort_tensor(y)
# result.shape = B
result = sorted_y[..., index]
result.set_shape(y.get_shape()[:-1])
if keep_dims:
if axis is None:
# ones_vec = [1, 1,..., 1], total length = len(S) + len(B).
ones_vec = array_ops.ones(
shape=[_get_best_effort_ndims(x)], dtype=dtypes.int32)
result *= array_ops.ones(ones_vec, dtype=x.dtype)
else:
result = _insert_back_keep_dims(result, axis)
return result
开发者ID:BhaskarNallani,项目名称:tensorflow,代码行数:101,代码来源:sample_stats.py
示例8: rotate_transpose
def rotate_transpose(x, shift, name="rotate_transpose"):
"""Circularly moves dims left or right.
Effectively identical to:
```python
numpy.transpose(x, numpy.roll(numpy.arange(len(x.shape)), shift))
```
When `validate_args=False` additional graph-runtime checks are
performed. These checks entail moving data from to GPU to CPU.
Example:
```python
x = ... # Tensor of shape [1, 2, 3, 4].
rotate_transpose(x, -1) # result shape: [2, 3, 4, 1]
rotate_transpose(x, -2) # result shape: [3, 4, 1, 2]
rotate_transpose(x, 1) # result shape: [4, 1, 2, 3]
rotate_transpose(x, 2) # result shape: [3, 4, 1, 2]
rotate_transpose(x, 7) == rotate_transpose(x, 3)
rotate_transpose(x, -7) == rotate_transpose(x, -3)
```
Args:
x: `Tensor`.
shift: `Tensor`. Number of dimensions to transpose left (shift<0) or
transpose right (shift>0).
name: `String`. The name to give this op.
Returns:
rotated_x: Input `Tensor` with dimensions circularly rotated by shift.
Raises:
TypeError: if shift is not integer type.
"""
with ops.name_scope(name, values=[x, shift]):
x = ops.convert_to_tensor(x, name="x")
shift = ops.convert_to_tensor(shift, name="shift")
# We do not assign back to preserve constant-ness.
check_ops.assert_integer(shift)
shift_value_static = tensor_util.constant_value(shift)
ndims = x.get_shape().ndims
if ndims is not None and shift_value_static is not None:
if ndims < 2: return x
shift_value_static = np.sign(shift_value_static) * (
abs(shift_value_static) % ndims)
if shift_value_static == 0: return x
perm = np.roll(np.arange(ndims), shift_value_static)
return array_ops.transpose(x, perm=perm)
else:
# Consider if we always had a positive shift, and some specified
# direction.
# When shifting left we want the new array:
# last(x, n-shift) + first(x, shift)
# and if shifting right then we want:
# last(x, shift) + first(x, n-shift)
# Observe that last(a) == slice(a, n) and first(a) == slice(0, a).
# Also, we can encode direction and shift as one: direction * shift.
# Combining these facts, we have:
# a = cond(shift<0, -shift, n-shift)
# last(x, n-a) + first(x, a) == x[a:n] + x[0:a]
# Finally, we transform shift by modulo length so it can be specified
# independently from the array upon which it operates (like python).
ndims = array_ops.rank(x)
shift = array_ops.where(math_ops.less(shift, 0),
math_ops.mod(-shift, ndims),
ndims - math_ops.mod(shift, ndims))
first = math_ops.range(0, shift)
last = math_ops.range(shift, ndims)
perm = array_ops.concat((last, first), 0)
return array_ops.transpose(x, perm=perm)
开发者ID:Jackhuang945,项目名称:tensorflow,代码行数:72,代码来源:distribution_util.py
示例9: __init__
def __init__(self,
batch_size,
total_num_examples,
max_learning_rate=1.0,
preconditioner_decay_rate=0.95,
burnin=25,
burnin_max_learning_rate=1e-6,
use_single_learning_rate=False,
name=None,
variable_scope=None):
default_name = 'VariationalSGDOptimizer'
with ops.name_scope(name, default_name, [
max_learning_rate, preconditioner_decay_rate, batch_size, burnin,
burnin_max_learning_rate
]):
if variable_scope is None:
var_scope_name = ops.get_default_graph().unique_name(
name or default_name)
with varscope_ops.variable_scope(var_scope_name) as scope:
self._variable_scope = scope
else:
self._variable_scope = variable_scope
self._preconditioner_decay_rate = ops.convert_to_tensor(
preconditioner_decay_rate, name='preconditioner_decay_rate')
self._batch_size = ops.convert_to_tensor(batch_size, name='batch_size')
self._total_num_examples = ops.convert_to_tensor(
total_num_examples, name='total_num_examples')
self._burnin = ops.convert_to_tensor(burnin, name='burnin')
self._burnin_max_learning_rate = ops.convert_to_tensor(
burnin_max_learning_rate, name='burnin_max_learning_rate')
self._max_learning_rate = ops.convert_to_tensor(
max_learning_rate, name='max_learning_rate')
self._use_single_learning_rate = use_single_learning_rate
with varscope_ops.variable_scope(self._variable_scope):
self._counter = varscope_ops.get_variable(
'counter', initializer=0, trainable=False)
self._preconditioner_decay_rate = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._preconditioner_decay_rate,
message='`preconditioner_decay_rate` must be non-negative'),
check_ops.assert_less_equal(
self._preconditioner_decay_rate,
1.,
message='`preconditioner_decay_rate` must be at most 1.'),
], self._preconditioner_decay_rate)
self._batch_size = control_flow_ops.with_dependencies([
check_ops.assert_greater(
self._batch_size,
0,
message='`batch_size` must be greater than zero')
], self._batch_size)
self._total_num_examples = control_flow_ops.with_dependencies([
check_ops.assert_greater(
self._total_num_examples,
0,
message='`total_num_examples` must be greater than zero')
], self._total_num_examples)
self._burnin = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._burnin, message='`burnin` must be non-negative'),
check_ops.assert_integer(
self._burnin, message='`burnin` must be an integer')
], self._burnin)
self._burnin_max_learning_rate = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._burnin_max_learning_rate,
message='`burnin_max_learning_rate` must be non-negative')
], self._burnin_max_learning_rate)
self._max_learning_rate = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._max_learning_rate,
message='`max_learning_rate` must be non-negative')
], self._max_learning_rate)
super(VariationalSGDOptimizer, self).__init__(
use_locking=False, name=name or default_name)
开发者ID:AnddyWang,项目名称:tensorflow,代码行数:84,代码来源:variational_sgd_optimizer.py
示例10: __init__
def __init__(self,
learning_rate,
preconditioner_decay_rate=0.95,
num_pseudo_batches=1,
burnin=25,
diagonal_bias=1e-8,
name=None,
variable_scope=None):
default_name = 'SGLDOptimizer'
with ops.name_scope(name, default_name, [
learning_rate, preconditioner_decay_rate, num_pseudo_batches, burnin,
diagonal_bias
]):
if variable_scope is None:
var_scope_name = ops.get_default_graph().unique_name(
name or default_name)
with varscope_ops.variable_scope(var_scope_name) as scope:
self._variable_scope = scope
else:
self._variable_scope = variable_scope
self._preconditioner_decay_rate = ops.convert_to_tensor(
preconditioner_decay_rate, name='preconditioner_decay_rate')
self._num_pseudo_batches = ops.convert_to_tensor(
num_pseudo_batches, name='num_pseudo_batches')
self._burnin = ops.convert_to_tensor(burnin, name='burnin')
self._diagonal_bias = ops.convert_to_tensor(
diagonal_bias, name='diagonal_bias')
self._learning_rate = ops.convert_to_tensor(
learning_rate, name='learning_rate')
with varscope_ops.variable_scope(self._variable_scope):
self._counter = varscope_ops.get_variable(
'counter', initializer=0, trainable=False)
self._preconditioner_decay_rate = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._preconditioner_decay_rate,
message='`preconditioner_decay_rate` must be non-negative'),
check_ops.assert_less_equal(
self._preconditioner_decay_rate,
1.,
message='`preconditioner_decay_rate` must be at most 1.'),
], self._preconditioner_decay_rate)
self._num_pseudo_batches = control_flow_ops.with_dependencies([
check_ops.assert_greater(
self._num_pseudo_batches,
0,
message='`num_pseudo_batches` must be greater than zero')
], self._num_pseudo_batches)
self._burnin = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._burnin, message='`burnin` must be non-negative'),
check_ops.assert_integer(
self._burnin, message='`burnin` must be an integer')
], self._burnin)
self._diagonal_bias = control_flow_ops.with_dependencies([
check_ops.assert_non_negative(
self._diagonal_bias,
message='`diagonal_bias` must be non-negative')
], self._diagonal_bias)
super(SGLDOptimizer, self).__init__(use_locking=False,
name=name or default_name)
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:67,代码来源:sgld_optimizer.py
注:本文中的tensorflow.python.ops.check_ops.assert_integer函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论