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

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

问题描述

我正在用 tensorflow 编写一个去噪自动编码器函数(它有点长,所以我不会发布整个代码)并且一切都运行良好,除非我向批处理添加掩蔽噪声

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

屏蔽噪声只是将特征的随机比例设为 0.所以问题只是将矩阵中的一些值设为 0.(如果我有一个 np.array 用于 exepmle)

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)

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

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

我可以通过在执行编码例程之前向批处理添加噪声来解决这个问题(然后我会处理 np.array 而不是 tf.placeholder),但我发现这个问题有点令人沮丧......

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:也许代码不是最优的,我没有很好地控制随机函数

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

感谢您的关注!!

推荐答案

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

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.

不可变张量的一个特性是它们是不可变的,所以你不能只获取张量的值并像 numpy 数组一样修改它.要使用分散更新,您需要一个缓冲区变量来操作,您可以使用该变量,但我会使用第一个选项或以下选项:

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:

  • 如果你想要屏蔽噪音之类的东西,你可以使用辍学;
  • 对于高斯噪声,您可以使用random_normal.
  • 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天全站免登陆