Machine learning 张量流中的加性高斯噪声

Machine learning 张量流中的加性高斯噪声,machine-learning,neural-network,tensorflow,deep-learning,conv-neural-network,Machine Learning,Neural Network,Tensorflow,Deep Learning,Conv Neural Network,我正试图通过以下方式将高斯噪声添加到我的网络层中 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:无法将部分已知的张量形状转换为张量:

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

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

我的小批量有时需要有不同的大小,因此输入层张量的大小直到执行时才知道

如果我理解正确,有人建议将shape设置为tf.shape(input_layer)。然而,当我尝试将卷积层应用于该噪声层时,我得到另一个错误:

ValueError:形状的dims必须已知,但不为空


在执行之前,向未知形状的输入层添加高斯噪声的正确方法是什么?

要动态获取未知尺寸张量的形状,需要使用
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\u normal
替换为
tf.random.normal