Tensorflow,tf.多项式,获取相关概率失败
我试图使用Tensorflow,tf.多项式,获取相关概率失败,tensorflow,Tensorflow,我试图使用tf.multinomial进行采样,我想得到采样值的相关概率值。这是我的示例代码 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)
tf.multinomial
进行采样,我想得到采样值的相关概率值。这是我的示例代码
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])>
[1]中的:将tensorflow作为tf导入
在[2]中:tf.enable\u eager\u execution()
在[3]中:probs=tf.常数([0.5,0.2,0.1,0.2],[0.6,0.1,0.1,0.1]],dtype=tf.float32)
[4]中:idx=tf.多项式(probs,1)
在[5]:idx#中打印索引
出[5]:
[6]中:probs[tf.range(probs.get_shape()[0],tf.squence(idx)]
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1个问题[tf.范围(probs.get_shape()[0]),tf.挤压(idx)]
C:\ProgramData\Anaconda3\lib\site packages\tensorflow\python\ops\array\u ops.py in\u slice\u helper(tensor,slice\u spec,var)
616新的_轴_掩码|=(1 618个检查索引)
619开始。追加
620结束。追加(s+1)
C:\ProgramData\Anaconda3\lib\site packages\tensorflow\python\ops\array\u ops.py in\u check\u index(idx)
514#TODO(slebedev):索引器在这里似乎更合适,但它
515#将打破"分割"助手"合同。
-->516 raise TypeError(_SLICE_TYPE_ERROR+”,got{!r}).format(idx))
517
518
TypeError:只有整数、切片(`:`)、省略号(`…`)、tf.newaxis(`None`)和标量tf.int32/tf.int64张量是有效的索引,明白了吗
我想要的预期结果是[0.2,0.1]
,如idx
所示
但在Numpy中,此方法的工作原理与
如何修复它?您可以尝试
tf.collection\n然后
,您可以尝试
>>将tensorflow作为tf导入
>>>tf.enable_eager_execution()
>>>probs=tf.常数([[0.5,0.2,0.1,0.2],[0.6,0.1,0.1,0.1]],dtype=tf.float32)
>>>idx=tf.多项式(probs,1)
>>>行索引=tf.range(probs.get\u shape()[0],dtype=tf.int64)
>>>完整索引=tf.堆栈([行索引,tf.压缩(idx)],轴=1)
>>>rs=tf.聚集索引(概率、完整索引)
或者,您可以使用,优点是您不需要关心上述代码中的batch\u size
。当您设置batch\u size=None
时,它可以在不同的batch\u size
下工作
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)
我更喜欢后者,因为它灵活而优雅