TensorFlow和随机变量 [英] TensorFlow and random variables

查看:202
本文介绍了TensorFlow和随机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对TensorFlow中的随机变量有一个疑问.假设我的损失函数中需要一个随机变量. 在TensorFlow教程中,我发现了用于初始化变量的随机函数,例如权重在训练过程中第二次被修改. 在我的情况下,我需要一个随机的浮点向量(比如说128个值),该向量遵循特定的分布(均匀分布或高斯分布),但是在每次损失计算中都可以改变.

I have one question about random variables in TensorFlow. Let's suppose I need a random variable inside my loss function. In TensorFlow tutorials I find random functions used for initialize variables, like weights that in a second time are modified by training process. In my case I need a random vector of floats (let's say 128 values), that follows a particular distribution (uniform or Gaussian) but that can change in each loss calculation.

在损失函数中定义此变量是我要做的简单事情,因为在每个时期我都会获得新值(无论如何都遵循选定的分布),或者我得到的值始终是相同的在所有迭代中?

Defining this variable in my loss function, is this the simple thing that I need to do, since at each epoch I get new values (that anyway follow the selected distribution) or do I get that the values that are always the same in all the iterations?

推荐答案

TensorFlow中的随机节点每次调用时总是采用不同的值,您可以通过多次调用来验证

A random node in TensorFlow always takes a different value each time it is called, as you can verify by calling it several times

import tensorflow as tf
x = tf.random_uniform(shape=())
sess = tf.Session()
sess.run(x)
# 0.79877698
sess.run(x)
# 0.76016617

它不是张量流术语中的Variable,正如您可以从上面的代码中检查的那样,该代码无需调用变量初始化即可运行.

It is not a Variable in the tensorflow terminology, as you can check from the code above, which runs without calling variable initialization.

这篇关于TensorFlow和随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆