3

我試圖按照以下方式將高斯噪聲添加到我的網絡層。Tensorflow中的加性高斯噪聲

def Gaussian_noise_layer(input_layer, std): 
    noise = tf.random_normal(shape = input_layer.get_shape(), mean = 0.0, stddev = std, dtype = tf.float32) 
    return input_layer + noise 

,我發現了錯誤:

ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 2600, 2000, 1)

我minibatches需要是不同的大小,有時,所以input_layer張量的大小不會直到執行時間是已知的。

如果我理解正確,回答Cannot convert a partially converted tensor in TensorFlow的人建議將shape設置爲tf.shape(input_layer)。然而,當我嘗試卷積層適用於嘈雜層我得到另一個錯誤:

ValueError: dims of shape must be known but is None

什麼是實現我加入高斯噪聲未知,直到形狀的輸入層的目標的正確方法執行時間處理時間?

回答

9

動態獲取你需要使用tf.shape()

例如一個未知的尺寸張量的形狀

import tensorflow as tf 
import numpy as np 


def gaussian_noise_layer(input_layer, std): 
    noise = tf.random_normal(shape=tf.shape(input_layer), mean=0.0, stddev=std, dtype=tf.float32) 
    return input_layer + noise 


inp = tf.placeholder(tf.float32, shape=[None, 8], name='input') 
noise = gaussian_noise_layer(inp, .2) 
noise.eval(session=tf.Session(), feed_dict={inp: np.zeros((4, 8))})