是他们在张量流中占位符的scatter_update() [英] is their a scatter_update() for placeholder in tensorflow

查看:160
本文介绍了是他们在张量流中占位符的scatter_update()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tensorflow编码去噪自动编码器函数(这有点长,所以我不会发布整个代码),并且一切正常,除非我向批处理中添加了屏蔽噪声

掩盖噪声只是将特征的随机比例设为0. 所以问题就是将矩阵中的某个值取为0.

所以我看到,如果它是一个tf.variable,那么感谢tf.scatter_update()如何修改矩阵的一个元素 但是,当我尝试使用占位符时,它会引发错误:"TypeError:'ScatterUpdate'Op要求输入'ref'为可变张量" 这是有问题的

我可以通过在执行编码例程之前将噪声添加到批处理中来解决此问题(我将处理np.array而不是tf.placeholder),但是我发现这种问题令人沮丧...

def bruit_MN(x,p=0.5):
l,c = x.shape
nbr = int(ceil(p*int(c))) #proportion of features to block
for i in range(l):
    temp = np.zeros(c)
    for j in range(nbr):
        u = randint(0,c-1)
        if temp[u]==0:
            aux=tf.Variable(initial_value=x[i])
            indices=tf.constant([u])
            new_value=tf.constant([0.0])
            aux=tf.scatter_update(aux,indices,new_value)
            x=tf.scatter_update(x,i,aux)
            temp[u] = 1
        else:
            j = j-1
return x

ps:也许代码不是最佳的,我不能很好地控制随机函数

感谢您的关注!

解决方案

最简单的方法是在TensorFlow图外创建噪声,对自动编码器的常态进行建模,然后将损坏的输入和预期的输出均馈入(干净的输入)到训练时的图形占位符.

不可变张量的属性之一是,它们是不可变的,因此您不能仅获取张量的值并像numpy数组一样对其进行修改.要使用分散更新,您需要使用一个缓冲区变量,可以使用该变量,但是我将使用第一个选项或以下选项:

  • 如果您想要诸如掩盖噪声之类的东西,可以使用此处获得它.

    I am coding a denoising autoencoder function with tensorflow (which is a little long so i won't post the entire code) and every thing is working well except when i am adding masking noise to a batch

    Masking noise is just taking a random proportion of the features to 0. So the problem is just taking some value in a matrix to 0.(trivial if i had a np.array for exepmle)

    So i see ,if it's a tf.variable, how to modify one element of a matrix thanks to tf.scatter_update() But then when I try with a placeholder it raises the error : "TypeError: 'ScatterUpdate' Op requires that input 'ref' be a mutable tensor" this is kind of problematic

    I could solve this by adding noise to the batch before doing the encoding routine(I would handle then np.array instead of tf.placeholder) but i found this problem kind of frustrating ...

    def bruit_MN(x,p=0.5):
    l,c = x.shape
    nbr = int(ceil(p*int(c))) #proportion of features to block
    for i in range(l):
        temp = np.zeros(c)
        for j in range(nbr):
            u = randint(0,c-1)
            if temp[u]==0:
                aux=tf.Variable(initial_value=x[i])
                indices=tf.constant([u])
                new_value=tf.constant([0.0])
                aux=tf.scatter_update(aux,indices,new_value)
                x=tf.scatter_update(x,i,aux)
                temp[u] = 1
            else:
                j = j-1
    return x
    

    ps :maybe the code is not optimal i don't control random function very well

    thanks for your attention!!

    解决方案

    An easiest way to do this is to create the noise outside the TensorFlow graph, model the auto-encoder normaly and then feed both the corrupted input and the expected output (the clean input) to the graph placeholders when training.

    One of the properties of immutable tensors is that they are, well, immutable, so you can't just take the value of a tensor and modify it in place like a numpy array. To use scatter update you would need a buffer variable to operate on, which you can use but I would use either the first option, or the following options:

    • If you want something like masking noise, you could use dropout;
    • For gaussian noise, you can use the random_normal.

    In case you really need to update an immutable tensor I made these two functions that take a sparse tensor with the updates and modify a given sparse or dense tensor respectively. You can get it here.

    这篇关于是他们在张量流中占位符的scatter_update()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆