Вопрос по использованию SoftmaxCentered Bijector - PullRequest
0 голосов
/ 01 декабря 2018

Я играю с биопроектором SofmaxCenter в tenorflow_probability и получаю некоторые ошибки.Поскольку документ для него находится в зачаточном состоянии, я не смог понять, что не так.Я надеюсь, что вы можете мне помочь.

По сути, учитывая, что X - логарифмический случайный вектор из трех компонентов, я хотел бы создать еще один случайный вектор Y, который определяется как преобразование softmax-центра X.

Следующий фрагмент кода не дает никакой ошибки

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

import matplotlib.pyplot as plt; plt.style.use('ggplot')
%matplotlib inline

import numpy as np
import seaborn as sns; sns.set_context('notebook')

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions
tfb = tfp.bijectors

tfe = tf.contrib.eager
tfe.enable_eager_execution()

X = tfd.LogNormal(loc=[[-5.0, 0.0, 4.0]], 
                  scale=[[2.0, 1.0, 1.5]])
Y = tfd.TransformedDistribution(
    distribution=X, 
    bijector=tfb.SoftmaxCentered()
)

Однако, когда я пытаюсь,

Y.sample(10)

Я получаю следующие ошибки,

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-9ed8f482f3c1> in <module>
----> 1 Y.sample(10)

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in sample(self, sample_shape, seed, name)
    684       samples: a `Tensor` with prepended dimensions `sample_shape`.
    685     """
--> 686     return self._call_sample_n(sample_shape, seed, name)
    687 
    688   def _log_prob(self, value):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/transformed_distribution.py in _call_sample_n(self, sample_shape, seed, name, **kwargs)
    405       # returned result.
    406       y = self.bijector.forward(x, **kwargs)
--> 407       y = self._set_sample_static_shape(y, sample_shape)
    408 
    409       return y

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in _set_sample_static_shape(self, x, sample_shape)
   1201     sample_ndims = sample_shape.ndims
   1202     batch_ndims = self.batch_shape.ndims
-> 1203     event_ndims = self.event_shape.ndims
   1204 
   1205     # Infer rank(x).

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py in event_shape(self)
    622       event_shape: `TensorShape`, possibly unknown.
    623     """
--> 624     return tf.TensorShape(self._event_shape())
    625 
    626   def is_scalar_event(self, name="is_scalar_event"):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/distributions/transformed_distribution.py in _event_shape(self)
    345         static_override
    346         if self._is_maybe_event_override
--> 347         else self.distribution.event_shape)
    348 
    349   def _batch_shape_tensor(self):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/bijectors/bijector.py in forward_event_shape(self, input_shape)
    680         after applying `forward`. Possibly unknown.
    681     """
--> 682     return self._forward_event_shape(tf.TensorShape(input_shape))
    683 
    684   def _inverse_event_shape_tensor(self, output_shape):

anaconda3/lib/python3.6/site-packages/tensorflow_probability/python/bijectors/softmax_centered.py in _forward_event_shape(self, input_shape)
     68 
     69   def _forward_event_shape(self, input_shape):
---> 70     if input_shape.ndims is None or input_shape[-1] is None:
     71       return input_shape
     72     return tf.TensorShape([input_shape[-1] + 1])

anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    614         return TensorShape(self._dims[key])
    615       else:
--> 616         return self._dims[key]
    617     else:
    618       if isinstance(key, slice):

IndexError: list index out of range

Спасибо!

1 Ответ

0 голосов
/ 05 декабря 2018

SoftmaxCentered хочет работать с векторной формой события, но распределение LogNormal имеет скалярную форму события.Если вы хотите провести softmax по вектору независимых LogNormals, вы можете сделать следующее:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools

import matplotlib.pyplot as plt; plt.style.use('ggplot')
%matplotlib inline

import numpy as np
import seaborn as sns; sns.set_context('notebook')

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions
tfb = tfp.bijectors

tfe = tf.contrib.eager
tfe.enable_eager_execution()

X = tfd.LogNormal(loc=[[-5.0, 0.0, 4.0]], 
                  scale=[[2.0, 1.0, 1.5]])
Z = tfd.Independent(X, reinterpreted_batch_ndims=1)
Y = tfd.TransformedDistribution(
    distribution=Z, 
    bijector=tfb.SoftmaxCentered()
)
Y.sample(2)

Обратите внимание, конечно, что SoftmaxCentered занимает трехмерное пространство и проецирует его на трехмерный коллектор в4-х мерное пространство.Это должно обеспечить обратимость.

...