如何获得相同的效果?什么使tensorflow.multinomial和pytorch.multinomial区别?

时间:2019-04-16 10:07:05

标签: tensorflow pytorch

tensorflow.multinomialpytorch.multinomial,性能差距很大。特别是,我认为tensorflow.multinomial非常有问题。

我编写了以下代码来比较这两个函数,我将第一个数字设置为10,将其他数字设置为0.0001,然后将softmax设置为选择概率。

import torch
import numpy as np
import  tensorflow as tf

single_data = np.array([10,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,0.0001])


single_data = np.expand_dims(single_data, axis=0)
data =np.repeat(single_data, 50, axis=0)
data = tf.nn.softmax(data,axis=-1)
sampled_word = tf.multinomial(data,1)
sample = tf.reshape(sampled_word,[-1])

with tf.Session() as sess:
    a_data=sess.run(data)
    print("prob:",a_data)
    print("tensorflow.multinomial", sess.run(sample))

    a_data = torch.from_numpy(a_data)
    idx = torch.multinomial(a_data, num_samples=1)
    print("tensorflow.multinomial", np.reshape(idx.data.numpy(),[-1]))
prob: [[9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]
 [9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]
 [9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]
 ...
 [9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]
 [9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]
 [9.93907740e-01 4.51278541e-05 4.51278541e-05 ... 4.51278541e-05
  4.51278541e-05 4.51278541e-05]]
tensorflow.multinomial [121 129  35 104   4 133  60  92 104 129   4  49  35  99 109 111  62  87
  23   5 109  63 103  61  78  43 101  85   2 127   0  36  53   0  74  44
  64  55  51  59 108   0 112  32  36  24  68 135  72  22]
tensorflow.multinomial [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0]

我们发现Tensorflow几乎很难选择数据(99%的时间),而PyTorch则很容易选择。有何评论?

0 个答案:

没有答案