Tensorflow中的加性高斯噪声(additive Gaussian noise in Tensorflow)

编程入门 行业动态 更新时间:2024-10-27 02:27:08
Tensorflow中的加性高斯噪声(additive Gaussian noise in 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转换为张量:(?,2600,2000,1)

我的小型机有时需要有不同的大小,所以input_layer张量的大小直到执行时间才会知道。

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

ValueError:形状的变暗必须是已知的,但是没有

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

I'm trying to add Gaussian noise to a layer of my network in the following way.

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

I'm getting the error:

ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 2600, 2000, 1)

My minibatches need to be of different sizes sometimes, so the size of the input_layer tensor will not be known until the execution time.

If I understand correctly, someone answering Cannot convert a partially converted tensor in TensorFlow suggested to set shape to tf.shape(input_layer). However then, when I try to apply a convolutional layer to that noisy layer I get another error:

ValueError: dims of shape must be known but is None

What is the correct way of achieving my goal of adding Gaussian noise to the input layer of a shape unknown until the execution time?

最满意答案

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

To dynamically get the shape of a tensor with unknown dimensions you need to use tf.shape()

For instance

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))})

更多推荐

本文发布于:2023-08-07 22:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:噪声   性高   Tensorflow   noise   Gaussian

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!