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

我的小批量数据有时需要不同的大小,因此input_layer张量的大小在执行时才会知道。

如果我理解正确的话,有人回答在TensorFlow中无法转换部分转换的张量时建议将shape设置为tf.shape(input_layer)。然而,当我尝试将卷积层应用到该噪声层时,我又遇到了另一个错误:

ValueError: shape的维度必须已知,但为None

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


回答:

要动态获取具有未知维度的张量的形状,您需要使用tf.shape()

例如

import tensorflow as tfimport numpy as npdef 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 + noiseinp = 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))})

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注