Запуск HMM C с вероятностью тензорного потока (тензорный поток 2) приводит к исключению (исходный код в тензорном потоке 1) - PullRequest
0 голосов
/ 05 мая 2020
• 1000

Код следующий:

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors    
import wget
url = 'https://raw.githubusercontent.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter2_MorePyMC/data/challenger_data.csv'
filename = wget.download(url)
filename

temperature_ = challenger_data_[:, 0]
temperature = tf.convert_to_tensor(temperature_, dtype=tf.float32)
D_ = challenger_data_[:, 1]                # defect or not?
D = tf.convert_to_tensor(D_, dtype=tf.float32)

beta = tfd.Normal(name="beta", loc=0.3, scale=1000.).sample()
alpha = tfd.Normal(name="alpha", loc=-15., scale=1000.).sample()
p_deterministic = tfd.Deterministic(name="p", loc=1.0/(1. + tf.exp(beta * temperature_ + alpha))).sample()

[
    prior_alpha_,
    prior_beta_,
    p_deterministic_,
    D_,
] = evaluate([
    alpha,
    beta,
    p_deterministic,
    D,
])

def challenger_joint_log_prob(D, temperature_, alpha, beta):
    """
    Joint log probability optimization function.

    Args:
      D: The Data from the challenger disaster representing presence or 
         absence of defect
      temperature_: The Data from the challenger disaster, specifically the temperature on 
         the days of the observation of the presence or absence of a defect
      alpha: one of the inputs of the HMC
      beta: one of the inputs of the HMC
    Returns: 
      Joint log probability optimization function.
    """
    rv_alpha = tfd.Normal(loc=0., scale=1000.)
    rv_beta = tfd.Normal(loc=0., scale=1000.)

    # make this into a logit
    logistic_p = 1.0/(1. + tf.exp(beta * tf.cast(temperature_, tf.foat32) + alpha))
    rv_observed = tfd.Bernoulli(probs=logistic_p)

    return (
        rv_alpha.log_prob(alpha)
        + rv_beta.log_prob(beta)
        + tf.reduce_sum(rv_observed.log_prob(D))
    )

number_of_steps = 10000 #@param {type:"slider", min:2500, max:120000, step:100}
burnin = 2000 #@param {type:"slider", min:2000, max:100000, step:100}

# Set the chain's start state.
initial_chain_state = [
    0. * tf.ones([], dtype=tf.float32, name="init_alpha"),
    0. * tf.ones([], dtype=tf.float32, name="init_beta")
]

# Since HMC operates over unconstrained space, we need to transform the
# samples so they live in real-space.
# Alpha is 100x of beta approximately, so apply Affine scalar bijector
# to multiply the unconstrained alpha by 100 to get back to 
# the Challenger problem space
unconstraining_bijectors = [
    tfp.bijectors.AffineScalar(100.),
    tfp.bijectors.Identity()
]

# Define a closure over our joint_log_prob.
unnormalized_posterior_log_prob = lambda *args: challenger_joint_log_prob(D, temperature_, *args)

До этого момента код является общим.

Здесь старая версия продолжается следующим образом:

# Initialize the step_size. (It will be automatically adapted.)
with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):
    step_size = tf.get_variable(
        name='step_size',
        initializer=tf.constant(0.01, dtype=tf.float32),
        trainable=False,
        use_resource=True
    )

# Defining the HMC
hmc=tfp.mcmc.TransformedTransitionKernel(
    inner_kernel=tfp.mcmc.HamiltonianMonteCarlo(
        target_log_prob_fn=unnormalized_posterior_log_prob,
        num_leapfrog_steps=40, #to improve convergence
        step_size=step_size,
        step_size_update_fn=tfp.mcmc.make_simple_step_size_update_policy(
            num_adaptation_steps=int(burnin * 0.8)),
        state_gradients_are_stopped=True),
    bijector=unconstraining_bijectors)

# Sampling from the chain.
[
    posterior_alpha,
    posterior_beta
], kernel_results = tfp.mcmc.sample_chain(
    num_results = number_of_steps,
    num_burnin_steps = burnin,
    current_state=initial_chain_state,
    kernel=hmc)

## Initialize any created variables for preconditions
init_g = tf.global_variables_initializer()

Я пытаюсь изменить эту часть кода для работы с tenorflow 2 (заимствуя код из документации - существуют несущественные различия):

# Initialize the HMC transition kernel.
num_results = int(10e3)
num_burnin_steps = int(1e3)
adaptive_hmc = tfp.mcmc.SimpleStepSizeAdaptation(
    tfp.mcmc.HamiltonianMonteCarlo(
        target_log_prob_fn=unnormalized_posterior_log_prob,
        num_leapfrog_steps=3,
        step_size=1.),
    num_adaptation_steps=int(num_burnin_steps * 0.8))

# Run the chain (with burn-in).
@tf.function
def run_chain():
  # Run the chain (with burn-in).
  samples, is_accepted = tfp.mcmc.sample_chain(
      num_results=num_results,
      num_burnin_steps=num_burnin_steps,
      current_state=1.,
      kernel=adaptive_hmc,
      trace_fn=lambda _, pkr: pkr.inner_results.is_accepted)

  sample_mean = tf.reduce_mean(samples)
  sample_stddev = tf.math.reduce_std(samples)
  is_accepted = tf.reduce_mean(tf.cast(is_accepted, dtype=tf.float32))
  return sample_mean, sample_stddev, is_accepted, samples

sample_mean, sample_stddev, is_accepted , samples= run_chain()

print('mean:{:.4f}  stddev:{:.4f}  acceptance:{:.4f}'.format(
    sample_mean.numpy(), sample_stddev.numpy(), is_accepted.numpy()))

Я получаю это сообщение об ошибке: TypeError: challenger_joint_log_prob() missing 1 required positional argument: 'beta'

Полное сообщение об ошибке:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    537         options=options, autograph_module=tf_inspect.getmodule(converted_call))
--> 538     converted_f = conversion.convert(target_entity, program_ctx)
    539 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\conversion.py in convert(entity, program_ctx)
    358   converted_entity_info = _convert_with_cache(entity, program_ctx,
--> 359                                               free_nonglobal_var_names)
    360 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\conversion.py in _convert_with_cache(entity, program_ctx, free_nonglobal_var_names)
    273     nodes, converted_name, entity_info = convert_entity_to_ast(
--> 274         entity, program_ctx)
    275 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\conversion.py in convert_entity_to_ast(o, program_ctx)
    505   elif tf_inspect.isfunction(o):
--> 506     nodes, name, entity_info = convert_func_to_ast(o, program_ctx)
    507   elif tf_inspect.ismethod(o):

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\conversion.py in convert_func_to_ast(f, program_ctx, do_rename)
    706   context = converter.EntityContext(namer, entity_info, program_ctx, new_name)
--> 707   node = node_to_graph(node, context)
    708 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\conversion.py in node_to_graph(node, context)
    740   node = converter.standard_analysis(node, context, is_initial=True)
--> 741   node = converter.apply_(node, context, function_scopes)
    742   node = converter.apply_(node, context, arg_defaults)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\core\converter.py in apply_(node, context, converter_module)
    398   node = standard_analysis(node, context)
--> 399   node = converter_module.transform(node, context)
    400   return node

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\converters\function_scopes.py in transform(node, ctx)
    131 def transform(node, ctx):
--> 132   return FunctionBodyTransformer(ctx).visit(node)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\core\converter.py in visit(self, node)
    344     try:
--> 345       return super(Base, self).visit(node)
    346     finally:

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\pyct\transformer.py in visit(self, node)
    501     if not anno.hasanno(node, anno.Basic.SKIP_PROCESSING):
--> 502       result = super(Base, self).visit(node)
    503     self.ctx.current_origin = parent_origin

~\Anaconda3\envs\tf2\lib\ast.py in visit(self, node)
    270         visitor = getattr(self, method, self.generic_visit)
--> 271         return visitor(node)
    272 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\converters\function_scopes.py in visit_FunctionDef(self, node)
     98 
---> 99     node = self.generic_visit(node)
    100 

~\Anaconda3\envs\tf2\lib\ast.py in generic_visit(self, node)
    325                     if isinstance(value, AST):
--> 326                         value = self.visit(value)
    327                         if value is None:

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\core\converter.py in visit(self, node)
    344     try:
--> 345       return super(Base, self).visit(node)
    346     finally:

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\pyct\transformer.py in visit(self, node)
    501     if not anno.hasanno(node, anno.Basic.SKIP_PROCESSING):
--> 502       result = super(Base, self).visit(node)
    503     self.ctx.current_origin = parent_origin

~\Anaconda3\envs\tf2\lib\ast.py in visit(self, node)
    270         visitor = getattr(self, method, self.generic_visit)
--> 271         return visitor(node)
    272 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\converters\function_scopes.py in visit_Return(self, node)
     43         function_context_name=self.state[_Function].context_name,
---> 44         value=node.value)
     45 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\pyct\templates.py in replace(template, **replacements)
    261   for k in replacements:
--> 262     replacements[k] = _convert_to_ast(replacements[k])
    263   template_str = parser.STANDARD_PREAMBLE + textwrap.dedent(template)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\pyct\templates.py in _convert_to_ast(n)
    223   if isinstance(n, str):
--> 224     return gast.Name(id=n, ctx=None, annotation=None, type_comment=None)
    225   if isinstance(n, qual_names.QN):

~\Anaconda3\envs\tf2\lib\site-packages\gast\gast.py in create_node(self, *args, **kwargs)
     11             "Bad argument number for {}: {}, expecting {}".\
---> 12             format(Name, nbparam, len(Fields))
     13         self._fields = Fields

AssertionError: Bad argument number for Name: 4, expecting 3

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-180-42754d76f6d2> in <module>
     15   return sample_mean, sample_stddev, is_accepted, samples
     16 
---> 17 sample_mean, sample_stddev, is_accepted , samples= run_chain()
     18 
     19 print('mean:{:.4f}  stddev:{:.4f}  acceptance:{:.4f}'.format(

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py in __call__(self, *args, **kwds)
    569         xla_context.Exit()
    570     else:
--> 571       result = self._call(*args, **kwds)
    572 
    573     if tracing_count == self._get_tracing_count():

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py in _call(self, *args, **kwds)
    616       # This is the first call of __call__, so we have to initialize.
    617       initializers = []
--> 618       self._initialize(args, kwds, add_initializers_to=initializers)
    619     finally:
    620       # At this point we know that the initialization is complete (or less

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
    498     self._concrete_stateful_fn = (
    499         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 500             *args, **kwds))
    501 
    502     def invalid_creator_scope(*unused_args, **unused_kwds):

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2438       args, kwargs = None, None
   2439     with self._lock:
-> 2440       graph_function, _, _ = self._maybe_define_function(args, kwargs)
   2441     return graph_function
   2442 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py in _maybe_define_function(self, args, kwargs)
   2769 
   2770       self._function_cache.missed.add(call_context_key)
-> 2771       graph_function = self._create_graph_function(args, kwargs)
   2772       self._function_cache.primary[cache_key] = graph_function
   2773       return graph_function, args, kwargs

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2659             arg_names=arg_names,
   2660             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2661             capture_by_value=self._capture_by_value),
   2662         self._function_attributes,
   2663         # Tell the ConcreteFunction to clean up its graph once it goes out of

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py in wrapped_fn(*args, **kwds)
    437         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    438         # the function a weak reference to itself to avoid a reference cycle.
--> 439         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    440     weak_wrapped_fn = weakref.ref(wrapped_fn)
    441 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\func_graph.py in wrapper(*args, **kwargs)
    962                     recursive=True,
    963                     optional_features=autograph_options,
--> 964                     user_requested=True,
    965                 ))
    966           except Exception as e:  # pylint:disable=broad-except

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    575           'Cause: %s', target_entity, e)
    576 
--> 577     return _call_unconverted(f, args, kwargs, options)
    578 
    579   with StackTraceMapper(converted_f), tf_stack.CurrentModuleFilter():

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\api.py in _call_unconverted(f, args, kwargs, options, update_cache)
    343 
    344   if kwargs is not None:
--> 345     return f(*args, **kwargs)
    346   else:
    347     return f(*args)

<ipython-input-180-42754d76f6d2> in run_chain()
      8       current_state=1.,
      9       kernel=adaptive_hmc,
---> 10       trace_fn=lambda _, pkr: pkr.inner_results.is_accepted)
     11 
     12   sample_mean = tf.reduce_mean(samples)

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\sample.py in sample_chain(num_results, current_state, previous_kernel_results, kernel, num_burnin_steps, num_steps_between_results, trace_fn, return_final_kernel_results, parallel_iterations, name)
    322         current_state)
    323     if previous_kernel_results is None:
--> 324       previous_kernel_results = kernel.bootstrap_results(current_state)
    325 
    326     if trace_fn is None:

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\simple_step_size_adaptation.py in bootstrap_results(self, init_state)
    409     with tf.name_scope(mcmc_util.make_name(
    410         self.name, 'simple_step_size_adaptation', 'bootstrap_results')):
--> 411       inner_results = self.inner_kernel.bootstrap_results(init_state)
    412       step_size = self.step_size_getter_fn(inner_results)
    413       return SimpleStepSizeAdaptationResults(

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\hmc.py in bootstrap_results(self, init_state)
    556   def bootstrap_results(self, init_state):
    557     """Creates initial `previous_kernel_results` using a supplied `state`."""
--> 558     kernel_results = self._impl.bootstrap_results(init_state)
    559     if self.step_size_update_fn is not None:
    560       step_size_assign = self.step_size_update_fn(self.step_size, None)  # pylint: disable=not-callable

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\metropolis_hastings.py in bootstrap_results(self, init_state)
    262     with tf.name_scope(mcmc_util.make_name(
    263         self.name, 'mh', 'bootstrap_results')):
--> 264       pkr = self.inner_kernel.bootstrap_results(init_state)
    265       if not has_target_log_prob(pkr):
    266         raise ValueError(

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\hmc.py in bootstrap_results(self, init_state)
    763           init_target_log_prob,
    764           init_grads_target_log_prob,
--> 765       ] = mcmc_util.maybe_call_fn_and_grads(self.target_log_prob_fn, init_state)
    766       if self._store_parameters_in_results:
    767         return UncalibratedHamiltonianMonteCarloKernelResults(

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\internal\util.py in maybe_call_fn_and_grads(fn, fn_arg_list, result, grads, check_non_none_grads, name)
    263     fn_arg_list = (list(fn_arg_list) if is_list_like(fn_arg_list)
    264                    else [fn_arg_list])
--> 265     result, grads = _value_and_gradients(fn, fn_arg_list, result, grads)
    266     if not all(dtype_util.is_floating(r.dtype)
    267                for r in (result if is_list_like(result) else [result])):  # pylint: disable=superfluous-parens

~\Anaconda3\envs\tf2\lib\site-packages\tensorflow_probability\python\mcmc\internal\util.py in _value_and_gradients(fn, fn_arg_list, result, grads, name)
    223 
    224     if result is None:
--> 225       result = fn(*fn_arg_list)
    226       if grads is None and tf.executing_eagerly():
    227         # Ensure we disable bijector cacheing in eager mode.

<ipython-input-177-4cf63f858bae> in <lambda>(*args)
     19 
     20 # Define a closure over our joint_log_prob.
---> 21 unnormalized_posterior_log_prob = lambda *args: challenger_joint_log_prob(D, temperature_, *args)

TypeError: challenger_joint_log_prob() missing 1 required positional argument: 'beta'

Как можно исправить код?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...