【问题标题】:additive Gaussian noise in TensorflowTensorflow 中的加性高斯噪声
【发布时间】:2017-05-01 16:22:10
【问题描述】:

我正在尝试通过以下方式将高斯噪声添加到我的网络层。

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 转换为张量: (?, 2600, 2000, 1)

有时我的 minibatch 需要具有不同的大小,因此 input_layer 张量的大小要到执行时间才能知道。

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

ValueError: 必须知道形状的暗淡但没有

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

【问题讨论】:

    标签: machine-learning neural-network tensorflow deep-learning conv-neural-network


    【解决方案1】:

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

    【讨论】:

    • 在tensorflow 2.0.0 中,您需要将tf.random_normal 替换为tf.random.normal。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2014-08-26
    • 2016-08-17
    • 2011-12-08
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多