1

我在 tensorflow_probability 中使用 SofmaxCenter 双射器并得到一些错误。由于它的文档处于起步阶段,我无法弄清楚出了什么问题。我希望你能帮助我。

基本上,假设 X 是三个分量的对数正态随机向量,我想创建另一个随机向量 Y,它被定义为 X 的 softmax 中心变换。

以下代码片段没有给出任何错误

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

谢谢!

4

1 回答 1

1

SoftmaxCentered 希望对矢量事件形状进行操作,但 LogNormal 分布具有标量事件形状。如果你想在一个独立的 LogNormals 向量上取一个 softmax,你可以这样做:

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 采用 3-D 空间并将其投影到 4-D 空间中的 3-D 流形上。这是为了提供可逆性。

于 2018-12-05T20:41:43.447 回答