首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow,tf.multinomial,获取关联概率失败

Tensorflow,tf.multinomial,获取关联概率失败
EN

Stack Overflow用户
提问于 2020-01-18 19:04:39
回答 1查看 59关注 0票数 1

我正在尝试使用tf.multinomial进行采样,我希望获得采样值的相关概率值。下面是我的示例代码,

代码语言:javascript
复制
In [1]: import tensorflow as tf

In [2]: tf.enable_eager_execution()

In [3]: probs = tf.constant([[0.5, 0.2, 0.1, 0.2], [0.6, 0.1, 0.1, 0.1]], dtype=tf.float32)

In [4]: idx = tf.multinomial(probs, 1)

In [5]: idx  # print the indices
Out[5]:
<tf.Tensor: id=43, shape=(2, 1), dtype=int64, numpy=
array([[3],
       [2]], dtype=int64)>

In [6]: probs[tf.range(probs.get_shape()[0], tf.squeeze(idx)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-56ef51f84ca2> in <module>
----> 1 probs[tf.range(probs.get_shape()[0]), tf.squeeze(idx)]

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in _slice_helper(tensor, slice_spec, var)
    616       new_axis_mask |= (1 << index)
    617     else:
--> 618       _check_index(s)
    619       begin.append(s)
    620       end.append(s + 1)

C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in _check_index(idx)
    514     # TODO(slebedev): IndexError seems more appropriate here, but it
    515     # will break `_slice_helper` contract.
--> 516     raise TypeError(_SLICE_TYPE_ERROR + ", got {!r}".format(idx))
    517
    518

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got <tf.Tensor: id=7, shape=(2,), dtype=int32, numpy=array([3, 2])>

idx所示,我想要的预期结果是[0.2, 0.1]

但在Numpy中,此方法的工作方式与https://stackoverflow.com/a/23435869/5046896中的回答相同

我怎么才能修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-19 11:11:43

你可以试试tf.gather_nd,你可以试试

代码语言:javascript
复制
>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> probs = tf.constant([[0.5, 0.2, 0.1, 0.2], [0.6, 0.1, 0.1, 0.1]], dtype=tf.float32)
>>> idx = tf.multinomial(probs, 1)
>>> row_indices = tf.range(probs.get_shape()[0], dtype=tf.int64)
>>> full_indices = tf.stack([row_indices, tf.squeeze(idx)], axis=1)
>>> rs = tf.gather_nd(probs, full_indices)

或者,您可以使用tf.distributions.Multinomial,优点是您不需要关心上面代码中的batch_size。当您设置batch_size=None时,它可以在不同的batch_size下工作。这里有一个简单的例子,

代码语言:javascript
复制
multinomail = tf.distributions.Multinomial(
                         total_count=tf.constant(1, dtype=tf.float32),  # sample one for each record in the batch, that is [1, batch_size]
                         probs=probs)
sampled_actions = multinomail.sample()  # sample one action for data in the batch
predicted_actions = tf.argmax(sampled_actions, axis=-1) 
action_probs = sampled_actions * predicted_probs 
action_probs = tf.reduce_sum(action_probs, axis=-1)

我更喜欢后者,因为它灵活而优雅。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59799955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档