is their a scatter_update() for placeholder in ten

2019-07-20 18:27发布

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!!

1条回答
女痞
2楼-- · 2019-07-20 19:03

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.

查看更多
登录 后发表回答