本文整理汇总了Python中tensorflow.python.util.tf_inspect.getfullargspec函数的典型用法代码示例。如果您正苦于以下问题:Python getfullargspec函数的具体用法?Python getfullargspec怎么用?Python getfullargspec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getfullargspec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _call_and_compute_mask
def _call_and_compute_mask(self, inputs, training=None, mask=None):
if not self.built and self._is_graph_network:
self._init_graph_network(self.inputs, self.outputs, name=self.name)
x = inputs
for layer in self.layers:
kwargs = {}
if 'mask' in tf_inspect.getfullargspec(layer.call).args:
kwargs['mask'] = mask
if 'training' in tf_inspect.getfullargspec(layer.call).args:
kwargs['training'] = training
if isinstance(layer, Network) and layer._compute_output_and_mask_jointly:
x, mask = layer._call_and_compute_mask(x, **kwargs)
else:
if not layer.built:
# Build layer if applicable.
with ops.name_scope(layer._name_scope()):
layer._maybe_build(x)
layer.built = True
x = layer.call(x, **kwargs)
if layer.supports_masking:
mask = layer.compute_mask(x, mask)
else:
mask = None
if not context.executing_eagerly():
x._keras_mask = mask
return x, mask
示例2: test_class_alias
def test_class_alias(self, mock_warning):
class MyClass(object):
"""My docstring."""
init_args = []
def __init__(self, arg):
MyClass.init_args.append(arg)
deprecated_cls = deprecation.deprecated_alias("deprecated.cls",
"real.cls",
MyClass)
print(deprecated_cls.__name__)
print(deprecated_cls.__module__)
print(deprecated_cls.__doc__)
MyClass("test")
self.assertEqual(0, mock_warning.call_count)
deprecated_cls("deprecated")
self.assertEqual(1, mock_warning.call_count)
# Make sure the error points to the right file.
self.assertRegexpMatches(mock_warning.call_args[0][1],
r"deprecation_test\.py:")
deprecated_cls("deprecated again")
self.assertEqual(1, mock_warning.call_count)
self.assertEqual(["test", "deprecated", "deprecated again"],
MyClass.init_args)
# Check __init__ signature matches for doc generation.
self.assertEqual(
tf_inspect.getfullargspec(MyClass.__init__),
tf_inspect.getfullargspec(deprecated_cls.__init__))
示例3: __init__
def __init__(self, original_op, ragged_op, ragged_args):
op_arg_names = tf_inspect.getfullargspec(original_op)[0]
ragged_arg_names = tf_inspect.getfullargspec(ragged_op)[0]
if op_arg_names != ragged_arg_names:
raise AssertionError(
'Signature must exactly match when overriding %s with %s: %s vs %s' %
(original_op, ragged_op, op_arg_names, ragged_arg_names))
self._ragged_op = ragged_op
self._ragged_args = _get_arg_infos(ragged_op, ragged_args)
if _UPDATE_DOCSTRINGS:
arg_list = ' and '.join('`%s`' % arg for arg in ragged_args)
original_op.__doc__ = (
original_op.__doc__.rstrip() + '\n\n' +
' {0} may be a `tf.RaggedTensor`.\n'.format(arg_list))
示例4: test_decorator_preserves_argspec
def test_decorator_preserves_argspec(self):
class TestClass(object):
def called_member(self, a):
if a < 0:
a = -a
return a
called_member_converted = api.convert()(called_member)
tc = TestClass()
self.assertListEqual(
list(tf_inspect.getfullargspec(tc.called_member)),
list(tf_inspect.getfullargspec(tc.called_member_converted)))
示例5: __init__
def __init__(self, func, trainable=False, arguments=None, **kwargs):
# Set self._{non,}_trainable_weights before calling Layer.__init__.
if hasattr(func, 'trainable_variables'):
self._trainable_weights = [v for v in func.trainable_variables]
trainable_variables_set = set(func.trainable_variables)
else:
self._trainable_weights = []
trainable_variables_set = set()
if hasattr(func, 'variables'):
self._non_trainable_weights = [v for v in func.variables
if v not in trainable_variables_set]
else:
self._non_trainable_weights = [] # TODO(arnoegw): Infer from `func`.
# TODO(b/124219898): We should be able to get the embedding dimension from
# the restored model.
if 'output_shape' in kwargs:
self._output_shape = tuple(kwargs.pop('output_shape'))
super(CustomLayer, self).__init__(trainable=trainable, **kwargs)
# Prepare to call `func`.
self._func = func
self._func_fullargspec = tf_inspect.getfullargspec(func.__call__)
self._func_wants_training = (
'training' in self._func_fullargspec.args or
'training' in self._func_fullargspec.kwonlyargs)
self._arguments = arguments or {}
# Forward the callable's regularization losses (if any).
if hasattr(func, 'regularization_losses'):
for l in func.regularization_losses:
if not callable(l):
raise ValueError(
'CustomLayer(func) expects func.regularization_losses to be an '
'iterable of callables, each returning a scalar loss term.')
self.add_loss(l) # Supports callables.
示例6: decorated
def decorated(self, **kwargs):
"""A wrapped test method that treats some arguments in a special way."""
mode = kwargs.pop("mode", "graph")
distribution = kwargs.get("distribution", None)
required_tpu = kwargs.pop("required_tpu", False)
required_gpus = kwargs.pop("required_gpus", None)
if distribution:
assert required_gpus is None, (
"Do not use `required_gpus` and `distribution` together.")
assert required_tpu is False, (
"Do not use `required_tpu` and `distribution` together.")
required_gpus = distribution.required_gpus
required_tpu = distribution.required_tpu
if required_tpu and not TPU_TEST:
self.skipTest("Test requires a TPU, but it's not available.")
if not required_tpu and TPU_TEST:
self.skipTest("Test that doesn't require a TPU.")
if not required_gpus:
if GPU_TEST:
self.skipTest("Test that doesn't require GPUs.")
elif context.num_gpus() < required_gpus:
self.skipTest(
"{} GPUs are not available for this test. {} GPUs are available".
format(required_gpus, context.num_gpus()))
# At this point, `kwargs` doesn't have `required_gpus` or `required_tpu`
# that the user might have specified. `kwargs` still has `mode`, which
# the test is allowed to accept or ignore.
requested_arguments = tf_inspect.getfullargspec(test_method).args
missing_arguments = set(list(kwargs.keys()) + ["self"]).difference(
set(requested_arguments + ["mode"]))
if missing_arguments:
raise ValueError("The test is missing arguments {} .".format(
missing_arguments))
kwargs_to_pass = {}
for arg in requested_arguments:
if arg == "self":
kwargs_to_pass[arg] = self
else:
kwargs_to_pass[arg] = kwargs[arg]
if mode == "eager":
with ops.Graph().as_default(), context.eager_mode():
if distribution:
kwargs_to_pass["distribution"] = distribution.strategy
test_method(**kwargs_to_pass)
elif mode == "graph":
with ops.Graph().as_default(), context.graph_mode():
if distribution:
kwargs_to_pass["distribution"] = distribution.strategy
test_method(**kwargs_to_pass)
else:
raise ValueError(
"'mode' has to be either 'eager' or 'graph' and not {}".format(
mode))
示例7: array_to_img
def array_to_img(x, data_format=None, scale=True, dtype=None):
"""Converts a 3D Numpy array to a PIL Image instance.
Arguments:
x: Input Numpy array.
data_format: Image data format.
either "channels_first" or "channels_last".
scale: Whether to rescale image values
to be within `[0, 255]`.
dtype: Dtype to use.
Returns:
A PIL Image instance.
Raises:
ImportError: if PIL is not available.
ValueError: if invalid `x` or `data_format` is passed.
"""
if data_format is None:
data_format = backend.image_data_format()
kwargs = {}
if 'dtype' in tf_inspect.getfullargspec(image.array_to_img)[0]:
if dtype is None:
dtype = backend.floatx()
kwargs['dtype'] = dtype
return image.array_to_img(x, data_format=data_format, scale=scale, **kwargs)
示例8: __init__
def __init__(self, x, y, image_data_generator,
batch_size=32,
shuffle=False,
sample_weight=None,
seed=None,
data_format=None,
save_to_dir=None,
save_prefix='',
save_format='png',
subset=None,
dtype=None):
if data_format is None:
data_format = backend.image_data_format()
kwargs = {}
if 'dtype' in tf_inspect.getfullargspec(
image.NumpyArrayIterator.__init__)[0]:
if dtype is None:
dtype = backend.floatx()
kwargs['dtype'] = dtype
super(NumpyArrayIterator, self).__init__(
x, y, image_data_generator,
batch_size=batch_size,
shuffle=shuffle,
sample_weight=sample_weight,
seed=seed,
data_format=data_format,
save_to_dir=save_to_dir,
save_prefix=save_prefix,
save_format=save_format,
subset=subset,
**kwargs)
示例9: testGetFullArgsSpecForPartial
def testGetFullArgsSpecForPartial(self):
def func(a, b):
del a, b
partial_function = functools.partial(func, 1)
argspec = tf_inspect.FullArgSpec(
args=['b'], varargs=None, varkw=None, defaults=None,
kwonlyargs=[], kwonlydefaults=None, annotations={})
self.assertEqual(argspec, tf_inspect.getfullargspec(partial_function))
示例10: testPositionsMatchArgGiven
def testPositionsMatchArgGiven(self):
full_dict = tf_upgrade_v2.TFAPIChangeSpec().function_arg_warnings
method_names = full_dict.keys()
for method in method_names:
# doesn't test methods on objects
if not method.startswith("*."):
args = full_dict[method].keys()
method = get_symbol_for_name(tf, method)
arg_spec = tf_inspect.getfullargspec(method)
for (arg, pos) in args:
self.assertEqual(arg_spec[0][pos], arg)
示例11: testGetFullArgSpecOnDecoratorThatChangesFullArgSpec
def testGetFullArgSpecOnDecoratorThatChangesFullArgSpec(self):
argspec = tf_inspect.FullArgSpec(
args=['a', 'b', 'c'],
varargs=None,
varkw=None,
defaults=(1, 'hello'),
kwonlyargs=[],
kwonlydefaults=None,
annotations={})
decorator = tf_decorator.TFDecorator('', test_undecorated_function, '',
argspec)
self.assertEqual(argspec, tf_inspect.getfullargspec(decorator))
示例12: testGetFullArgSpecIgnoresDecoratorsThatDontProvideFullArgSpec
def testGetFullArgSpecIgnoresDecoratorsThatDontProvideFullArgSpec(self):
argspec = tf_inspect.FullArgSpec(
args=['a', 'b', 'c'],
varargs=None,
varkw=None,
defaults=(1, 'hello'),
kwonlyargs=[],
kwonlydefaults=None,
annotations={})
inner_decorator = tf_decorator.TFDecorator('', test_undecorated_function,
'', argspec)
outer_decorator = tf_decorator.TFDecorator('', inner_decorator)
self.assertEqual(argspec, tf_inspect.getfullargspec(outer_decorator))
示例13: _call_and_compute_mask
def _call_and_compute_mask(self, inputs, training=None, mask=None):
if not self.built:
self.build(inputs.shape)
x = inputs
for layer in self.layers:
kwargs = {}
if 'mask' in tf_inspect.getfullargspec(layer.call).args:
kwargs['mask'] = mask
if 'training' in tf_inspect.getfullargspec(layer.call).args:
kwargs['training'] = training
if isinstance(layer, Network) and layer._compute_output_and_mask_jointly:
x, mask = layer._call_and_compute_mask(x, **kwargs)
else:
x = layer.call(x, **kwargs)
if layer.supports_masking:
mask = layer.compute_mask(x, mask)
else:
mask = None
if not context.executing_eagerly():
x._keras_mask = mask
return x, mask
示例14: deserialize_keras_object
def deserialize_keras_object(identifier,
module_objects=None,
custom_objects=None,
printable_module_name='object'):
if identifier is None:
return None
if isinstance(identifier, dict):
# In this case we are dealing with a Keras config dictionary.
config = identifier
(cls, cls_config) = class_and_config_for_serialized_keras_object(
config, module_objects, custom_objects, printable_module_name)
if hasattr(cls, 'from_config'):
arg_spec = tf_inspect.getfullargspec(cls.from_config)
custom_objects = custom_objects or {}
if 'custom_objects' in arg_spec.args:
return cls.from_config(
cls_config,
custom_objects=dict(
list(_GLOBAL_CUSTOM_OBJECTS.items()) +
list(custom_objects.items())))
with CustomObjectScope(custom_objects):
return cls.from_config(cls_config)
else:
# Then `cls` may be a function returning a class.
# in this case by convention `config` holds
# the kwargs of the function.
custom_objects = custom_objects or {}
with CustomObjectScope(custom_objects):
return cls(**cls_config)
elif isinstance(identifier, six.string_types):
object_name = identifier
if custom_objects and object_name in custom_objects:
obj = custom_objects.get(object_name)
elif object_name in _GLOBAL_CUSTOM_OBJECTS:
obj = _GLOBAL_CUSTOM_OBJECTS[object_name]
else:
obj = module_objects.get(object_name)
if obj is None:
raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
# Classes passed by name are instantiated with no args, functions are
# returned as-is.
if tf_inspect.isclass(obj):
return obj()
return obj
else:
raise ValueError('Could not interpret serialized ' + printable_module_name +
': ' + identifier)
示例15: has_arg
def has_arg(fn, name, accept_all=False):
"""Checks if a callable accepts a given keyword argument.
Arguments:
fn: Callable to inspect.
name: Check if `fn` can be called with `name` as a keyword argument.
accept_all: What to return if there is no parameter called `name`
but the function accepts a `**kwargs` argument.
Returns:
bool, whether `fn` accepts a `name` keyword argument.
"""
arg_spec = tf_inspect.getfullargspec(fn)
if accept_all and arg_spec.varkw is not None:
return True
return name in arg_spec.args