Python源码示例:syntaxnet.util.check.Ne()
示例1
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例2
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例3
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例4
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例5
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例6
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例7
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例8
def _add_hooks_for_trainable_params(component, params):
"""Adds runtime hooks for a variable of trainable parameters.
Ignores parameters that are not statically-deducible as matrices.
Args:
component: Component for which to add hooks.
params: Variable for which to add hooks.
"""
full_name = params.op.name
matrix = component.get_variable(var_params=params)
# Only add hooks for tensors that are statically-deducible as matrices.
if params.shape.ndims != 2:
tf.logging.info('Not adding hooks for trainable params %s', full_name)
return
# Infer the suffix to append to variable names, if any, based on whether the
# possibly-averaged |matrix| is named differently than the |params|.
suffix = re.sub('^' + re.escape(full_name), '', matrix.op.name)
check.Ne(suffix, matrix.op.name,
'Failed to find suffix for params %s' % full_name)
def _hook_name(base_name):
"""Returns a hook node name constructed from a base name."""
return full_name + base_name + suffix
# Add the matrix and its transpose.
transposed = tf.transpose(matrix)
_add_hook_node(matrix, _hook_name('/matrix'))
_add_hook_node(transposed, _hook_name('/transposed'))
# Add blocked versions of the matrix and its transpose.
for blocked, blocked_suffix in _blocked_and_dtype_transformations(matrix):
_add_hook_node(blocked, _hook_name('/matrix' + blocked_suffix))
for blocked, blocked_suffix in _blocked_and_dtype_transformations(transposed):
_add_hook_node(blocked, _hook_name('/transposed' + blocked_suffix))
# Also add hooks for the original shapes, which are obscured by padding.
_add_hook_node(tf.shape(matrix), _hook_name('/matrix/shape'))
_add_hook_node(tf.shape(transposed), _hook_name('/transposed/shape'))
示例9
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例10
def _add_hooks_for_trainable_params(component, params):
"""Adds runtime hooks for a variable of trainable parameters.
Ignores parameters that are not statically-deducible as matrices.
Args:
component: Component for which to add hooks.
params: Variable for which to add hooks.
"""
full_name = params.op.name
matrix = component.get_variable(var_params=params)
# Only add hooks for tensors that are statically-deducible as matrices.
if params.shape.ndims != 2:
tf.logging.info('Not adding hooks for trainable params %s', full_name)
return
# Infer the suffix to append to variable names, if any, based on whether the
# possibly-averaged |matrix| is named differently than the |params|.
suffix = re.sub('^' + re.escape(full_name), '', matrix.op.name)
check.Ne(suffix, matrix.op.name,
'Failed to find suffix for params %s' % full_name)
def _hook_name(base_name):
"""Returns a hook node name constructed from a base name."""
return full_name + base_name + suffix
# Add the matrix and its transpose.
transposed = tf.transpose(matrix)
_add_hook_node(matrix, _hook_name('/matrix'))
_add_hook_node(transposed, _hook_name('/transposed'))
# Add blocked versions of the matrix and its transpose.
for blocked, blocked_suffix in _blocked_and_dtype_transformations(matrix):
_add_hook_node(blocked, _hook_name('/matrix' + blocked_suffix))
for blocked, blocked_suffix in _blocked_and_dtype_transformations(transposed):
_add_hook_node(blocked, _hook_name('/transposed' + blocked_suffix))
# Also add hooks for the original shapes, which are obscured by padding.
_add_hook_node(tf.shape(matrix), _hook_name('/matrix/shape'))
_add_hook_node(tf.shape(transposed), _hook_name('/transposed/shape'))
示例11
def testCheckNe(self):
check.Ne(1, 2, 'foo')
with self.assertRaisesRegexp(ValueError, 'bar'):
check.Ne(1, 1, 'bar')
with self.assertRaisesRegexp(RuntimeError, 'baz'):
check.Ne(1, 1, 'baz', RuntimeError)
示例12
def _get_hook_name(component, variable_name, suffix):
"""Builds the name of a hook node.
Specifically, the name of the hook node is:
<component.name>/<variable_name><suffix><remainder>
where <remainder> is whatever follows <variable_name> in the name of the op
that produces the named variable. Recall that component.get_variable() may
return either the original variable or its moving average. These might have
names like:
foo_component/bar_variable
foo_component/bar_variable/ExponentialMovingAverage
In the examples above, the <remainder> is "" for the original variable and
"/ExponentialMovingAverage" for its moving average. Calling this function
with suffix="/baz_suffix" in either case would add hook nodes named:
foo_component/bar_variable/baz_suffix
foo_component/bar_variable/baz_suffix/ExponentialMovingAverage
Note that the suffix is inserted after the variable name, not necessarily at
the end of the entire op name.
Args:
component: Component that the hook node belongs to.
variable_name: Variable that the hook node name is based on.
suffix: Suffix to append to the variable name.
Returns:
Name of the hook node.
"""
variable = component.get_variable(variable_name)
full_name = variable.op.name
prefix = component.name + '/' + variable_name
hook_name = re.sub('^' + re.escape(prefix), prefix + suffix, full_name)
# If re.sub() did not match anything, it returns the unmodified input (i.e.,
# |full_name|). Enforce that some change was made.
check.Ne(
full_name, hook_name,
'Failed to match expected variable prefix "{}" in variable "{}"'.format(
prefix, full_name))
return hook_name
示例13
def _get_hook_name(component, variable_name, suffix):
"""Builds the name of a hook node.
Specifically, the name of the hook node is:
<component.name>/<variable_name><suffix><remainder>
where <remainder> is whatever follows <variable_name> in the name of the op
that produces the named variable. Recall that component.get_variable() may
return either the original variable or its moving average. These might have
names like:
foo_component/bar_variable
foo_component/bar_variable/ExponentialMovingAverage
In the examples above, the <remainder> is "" for the original variable and
"/ExponentialMovingAverage" for its moving average. Calling this function
with suffix="/baz_suffix" in either case would add hook nodes named:
foo_component/bar_variable/baz_suffix
foo_component/bar_variable/baz_suffix/ExponentialMovingAverage
Note that the suffix is inserted after the variable name, not necessarily at
the end of the entire op name.
Args:
component: Component that the hook node belongs to.
variable_name: Variable that the hook node name is based on.
suffix: Suffix to append to the variable name.
Returns:
Name of the hook node.
"""
variable = component.get_variable(variable_name)
full_name = variable.op.name
prefix = component.name + '/' + variable_name
hook_name = re.sub('^' + re.escape(prefix), prefix + suffix, full_name)
# If re.sub() did not match anything, it returns the unmodified input (i.e.,
# |full_name|). Enforce that some change was made.
check.Ne(
full_name, hook_name,
'Failed to match expected variable prefix "{}" in variable "{}"'.format(
prefix, full_name))
return hook_name