在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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