简体   繁体   English

如何为张量流中每个值的给定概率采样张量值?

[英]How to sample tensor values given probabilities for each value in tensorflow?

We need to sample values from one tensor regarding to another tensor that contains probabilities. 我们需要从一个张量采样到另一个包含概率的张量的值。 Lets say, we have two tensors t1,t2 of shape (?,3), and want to find another tensor t3 of shape (?,1) that contains a sample of each row in t1 regarding to probabilities in t2. 可以说,我们有两个形状为(?,3)的张量t1,t2,并且想要找到另一个形状为(?,1)的张量t3,其中包含与t2中的概率有关的t1中每一行的样本。

You do this in two steps. 您分两步执行此操作。 First, you sample indices 0 , 1 and 2 , then you replace those indices with tensor values. 首先,样本索引012 ,然后用你张量的值替换那些索引。 This can be done with tf.random.categorical (see this question for more information about this function). 这可以通过tf.random.categorical完成(有关此功能的更多信息,请参阅此问题 )。 Note tf.random.categorical was added in version 1.13.1. 注意tf.random.categorical已在1.13.1版中添加。

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    tf.random.set_random_seed(0)
    values = tf.placeholder(tf.float32, [None, 3])
    probabilities = tf.placeholder(tf.float32, [None, 3])
    # You can change the number of samples per row (or make it a placeholder)
    num_samples = 1
    # Use log to get log-probabilities or give logit values (pre-softmax) directly
    logits = tf.log(probabilities)
    # Sample column indices
    col_idx = tf.random.categorical(logits, num_samples, dtype=tf.int32)
    # Make row indices
    num_rows = tf.shape(values)[0]
    row_idx = tf.tile(tf.expand_dims(tf.range(num_rows), 1), (1, num_samples))
    # Gather actual values
    result = tf.gather_nd(values, tf.stack([row_idx, col_idx], axis=-1))
    # Test
    print(sess.run(result, feed_dict={
        values: [[10., 20., 30.], [40., 50., 60.]],
        # Can be actual or "proportional" probabilities not summing 1
        probabilities: [[.1, .3, .6], [0., 4., 2.]]
    }))
    # [[30.]
    #  [60.]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对图像张量进行装箱,以便将每个像素值装箱/存储为张量流中10个值中的1个 - How to bin an image tensor so that each pixel value is binned/bucketed into 1 of 10 values in tensorflow 如何使用 TensorFlow 在给定位置的张量中放置值列表 - How to put a list of values in a tensor at given positions with TensorFlow 给定 PyTorch 中的张量,如何构建边际概率向量 - How to build a vector of marginal probabilities, given a tensor in PyTorch Tensorflow,在给定条件的情况下更改Tensor值 - Tensorflow, change Tensor values given a condition PyTorch:如何从张量中的每个值被选择的可能性不同的张量中进行采样? - PyTorch: How to sample from a tensor where each value in the tensor has a different likelihood of being selected? Tensorflow:如何修改张量值 - Tensorflow: How to modify the value in tensor 如何在Tensorflow中获取张量的值 - How to get the value of the tensor in Tensorflow 如何在 Tensorflow 中保存张量的值 - How to save the value of a tensor in Tensorflow Tensorflow如何获得张量值 - Tensorflow how to get tensor value 在 Pandas 中,如何计算给定另一列值的列值的相对概率? - In Pandas, how to calculate the relative probabilities of values of a column given a value of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM