tensorflow--变量/序列/随机数-汇总

官网:https://tensorflow.google.cn/api_guides/python/constant_op?hl=zh-cn#Constant_Value_Tensors

一、常值张量( Constant Value Tensors )

TensorFlow提供了一些可以用来生成常量的操作。

二、序列( Sequences )

三、随机张量( Random Tensors )

TensorFlow有几个OPS创建随机张量具有不同的分布。随机OPS是有状态的,每次评估时都创建新的随机值。

这些函数中的种子关键字参数与图级随机种子一起作用。使用tf.set_._seed或op-level seed更改图形级种子将更改这些操作的底层种子。既不设置图级也不设置OP级种子,从而为所有操作生成随机种子。有关操作级别和图级随机种子之间交互作用的详细信息,请参见tf.set_random_seed。

例子:

# Create a tensor of shape [2, 3] consisting of random normal values, with mean
# -1 and standard deviation 4.
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# Each time we run these ops, different results are generated
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))

# Set an op-level seed to generate repeatable sequences across sessions.
norm = tf.random_normal([2, 3], seed=1234)
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))
sess = tf.Session()
print(sess.run(norm))
print(sess.run(norm))

随机值的另一个常用用法是变量的初始化。

# Use random uniform values in [0, 1) as the initializer for a variable of shape
# [2, 3]. The default type is float32.
var = tf.Variable(tf.random_uniform([2, 3]), name="var")
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(var))

函数:

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn