本文介绍tf的constant op: 常量,序列,随机序列
参考官方 http://www.tensorfly.cn/tfdoc/api_docs/python/constant_op.html
1 constants —常量
1.1 zeros
tf.zeros(shape, dtype=tf.float32, name=None)
创建元素为0的张量:
tf.zeros([2, 3], int32) ==> [[0, 0, 0], [0, 0, 0]]
1.2 zeros_like
tf.zeros_like(tensor, dtype=None, name=None)
依据一个tensor的shape创建一个新tensor:
# 'tensor_instance' is [[1, 2, 3], [4, 5, 6]]
tf.zeros_like(tensor_instance) ==> [[0, 0, 0], [0, 0, 0]]
1.3 ones
tf.ones(shape, dtype=tf.float32, name=None)
举例:
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
1.4 ones_like
tf.ones_like(tensor, dtype=None, name=None)
1.5 fill
tf.fill(dims, value, name=None)
依据shape填充value;
#'dims' is [2, 3]
fill(dims, 9) ==> [[9, 9, 9]
[9, 9, 9]]
1.6 constant
tf.constant(value, dtype=None, shape=None, name='Const')
创建常量
# Constant 1-D Tensor populated with value list.
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]
# Constant 2-D tensor populated with scalar value -1.
tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
[-1. -1. -1.]]
2 Sequence
2.1 linspace
tf.linspace(start, stop, num, name=None)
创建包含num个元素的张量。 数值interval = stop – start / num-1递增:
tf.linspace(10.0, 12.0, 3, name="l") => [ 10.0 11.0 12.0]
2.2 range
tf.range(start, limit, delta=1, name='range')
[start, limit),左闭又开
创建int类型的tensor:
# 'start' is 3
# 'limit' is 18
# 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
3 Random tensor
3.1 random_normal
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
返回随机数序列,数值服从正态(normal)分布(参数中mean是均值,stddev是标准差)。 其中seed用户控制在op-level 和grapl-level产生相同或不同的序列,详见 :
http://www.tensorfly.cn/tfdoc/api_docs/python/constant_op.html#set_random_seed
3.2 truncated_normal
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
截断的随机正态分布。 所有值都在[mean – 2*div, mean + 2*div]
3.3 random_uniform
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)
产生一个随机数序列,数值服从均匀分布(每个元素取任一值的概率一样)。 minval, maxval分别是最小、最大值
3.4 random_shuffle
tf.random_shuffle(value, seed=None, name=None)
沿着第0维随机调整tensor.
[[1, 2], [[5, 6],
[3, 4], ==> [1, 2],
[5, 6]] [3, 4]]
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/9330.html