代码之家  ›  专栏  ›  技术社区  ›  talos1904

如何在单个会话中从同一随机操作中获取不同的样本

  •  0
  • talos1904  · 技术社区  · 6 年前

    我正在试着从你的电脑上取多个样品 现在,它似乎只给你相同的值。有什么方法可以得到不同的值或 xr公司 一个疗程?

    import tensorflow as tf
    import tensorflow.random as tdr 
    import numpy as np
    
    x = 5. # fixe_input 
    xr = tdr.uniform(shape=[1],minval=0., maxval=x)
    x_list = tf.stack([xr for _ in range(10)]
    with tf.Session() as sess:
        print('xlist', sess.run(x_list))
    

    输出:

    xlist [[2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]
     [2.2005057]]
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   eqzx    6 年前

    有没有一种方法可以在单个会话中获得不同的值或xr?

    shape=[1] 成为 shape=[10] ? 这将为您提供一个包含10个样本的数组,这些样本来自您的发行版,其中一个 sess.run 打电话。

    import tensorflow as tf
    import tensorflow.random as tdr 
    import numpy as np
    
    x = 5. # fixe_input 
    xr = tdr.uniform(shape=[10],minval=0., maxval=x)
    with tf.Session() as sess:
        print('xlist', sess.run(xr))
    

    列表[2.6705563 1.477465 2.2741747 0.44075608 0.41182756 3.652794

        2
  •  0
  •   AirSquid    6 年前

    实际上,您只需生成一次随机数,在变量中捕获它,然后像下面第一部分那样复制它。您希望为列表中的每个项调用random函数,如下面第二部分所示。

    In [2]: from random import randint                                              
    
    In [3]: x = randint(1,1000)                                                     
    
    In [4]: random_nums = [x for _ in range(10)]                                    
    
    In [5]: random_nums                                                             
    Out[5]: [728, 728, 728, 728, 728, 728, 728, 728, 728, 728]
    
    In [6]: random_nums2 = [randint(1, 1000) for _ in range(10)]                    
    
    In [7]: random_nums2                                                            
    Out[7]: [92, 928, 72, 875, 719, 725, 957, 930, 729, 299]
    
    In [8]: