首页 文章

Tensorflow中的加性高斯噪声

提问于
浏览
5

我试图通过以下方式将高斯噪声添加到我的网络层 .

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:无法将部分已知的TensorShape转换为Tensor:(?,2600,2000,1)

我的小批量有时需要具有不同的大小,因此在执行时间之前不会知道input_layer张量的大小 .

如果我理解正确,有人回答Cannot convert a partially converted tensor in TensorFlow建议将形状设置为tf.shape(input_layer) . 然而,当我尝试将卷积层应用于该噪声层时,我得到另一个错误:

ValueError:形状的暗淡必须是已知的但是无

在执行时间之前,实现将高斯噪声添加到未知形状的输入层的目标的正确方法是什么?

1 回答

  • 15

    要动态获取具有未知尺寸的张量的形状,您需要使用 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))})
    

相关问题