The positions of the parameters of f to be differentiated in param_args.
(f, params, param_args)
| 273 | |
| 274 | |
| 275 | def _get_arg_spec(f, params, param_args): |
| 276 | """The positions of the parameters of f to be differentiated in param_args.""" |
| 277 | try: |
| 278 | args = tf_inspect.getfullargspec(f).args |
| 279 | except TypeError as e: |
| 280 | # TypeError can happen when f is a callable object. |
| 281 | if params is None: |
| 282 | return range(len(param_args)) |
| 283 | elif all(isinstance(x, int) for x in params): |
| 284 | return params |
| 285 | raise ValueError("Either callable provided is not a function or could not " |
| 286 | "inspect its arguments by name: %s. Original error: %s" |
| 287 | % (f, e)) |
| 288 | if params is None: |
| 289 | if not args: |
| 290 | return range(len(param_args)) |
| 291 | return range(len(args)) |
| 292 | elif all(isinstance(x, six.string_types) for x in params): |
| 293 | return [args.index(n) for n in params] |
| 294 | elif all(isinstance(x, int) for x in params): |
| 295 | return params |
| 296 | else: |
| 297 | raise ValueError( |
| 298 | "params must be all strings or all integers; got %s." % params) |
| 299 | |
| 300 | |
| 301 | def gradients_function(f, params=None): |