tensorflow编程: Constants, Sequences, and Random Values

Constant Value Tensors

tf.zeros

tf.zeros (shape, dtype=tf.float32, name=None)

tf.zeros_like

tf.zeros_like (tensor, dtype=None, name=None, optimize=True)

tf.ones

tf.ones (shape, dtype=tf.float32, name=None)

tf.ones_like

tf.ones_like (tensor, dtype=None, name=None, optimize=True)

tf.fill

tf.fill (dims, value, name=None)

import tensorflow as tf

t = tf.fill(dims=[2, 5], value=0.1)

with tf.Session() as sess:
    print sess.run(t)
[[ 0.1  0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1  0.1]]

tf.constant

tf.constant (value, dtype=None, shape=None, name=’Const’, verify_shape=False)

Sequences

tf.linspace

  等间隔 取值。

tf.lin_space (start, stop, num, name=None)

  注意:
    startstop 参数都必须是 浮点型
    取值范围也包括了 stop
    tf.lin_space 等同于 tf.linspace

import tensorflow as tf

t_1 = tf.lin_space(start=10.0, stop=15.0, num=5)
t_2 = tf.lin_space(start=10.0, stop=15.0, num=6)

with tf.Session() as sess:
    print sess.run(t_1)
    print
    print sess.run(t_2)
# 取值个数为 5
[ 10.    11.25  12.5   13.75  15.  ]

# 取值个数为 6
[ 10.  11.  12.  13.  14.  15.]

tf.range

  等价于 np.arange

  完整的接口:

tf.range (start, limit, delta=1, dtype=None, name=’range’)

  专为 0为起点,1为步长 设计的快捷接口(默认start=0, delta=1):

tf.range (limit, delta=1, dtype=None, name=’range’)

import numpy as np
n_1 = np.arange(start=10, stop=0, step=-2)
n_2 = np.arange(5)
print n_1
print n_2


import tensorflow as tf
t_1 = tf.range(start=10, limit=0, delta=-2)
t_2 = tf.range(5)
with tf.Session() as sess:
    print sess.run(t_1)
    print sess.run(t_2)
# 用 np.arange 生成的有序数列
[10  8  6  4  2]
[0 1 2 3 4]

# 用 tf.range 生成的有序数列
[10  8  6  4  2]
[0 1 2 3 4]

Random Tensors

tf.random_normal

  等价于 np.random.normal(均值, 标准差, 个数)

tf.random_normal (shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

import numpy as np
n_1 = np.random.normal(0, 1, size=[2, 5])
print n_1

import tensorflow as tf
t = tf.random_normal(shape=[2, 5], dtype=tf.float32)
with tf.Session() as sess:
    print sess.run(t)
# np.random.normal
[[-0.19322391 -0.34265808  0.06453351  0.8865113   0.52242084]
 [ 0.11956765 -0.64113454 -1.34379807 -0.16189467  0.16823816]]

# tf.random_normal
[[ 1.16703379  0.63120824  1.2659812   0.42991444 -1.09538388]
 [-0.49309424  0.65165377  1.05139613  1.37237358  2.1126318 ]]

tf.random_uniform

  从 均匀分布 中输出 随机值

tf.random_uniform (shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)

  在输出 浮点型定域随机值 时,等同于 np.random.uniform;区别在于, tf.random_uniform 还可以输出 整型定域随机值

import numpy as np
n_1 = np.random.uniform(low=0, high=10, size=[2, 5])
print n_1

import random
n_2 = random.uniform(a=0, b=10)
print n_2

import tensorflow as tf
t_1 = tf.random_uniform(shape=[2, 5], minval=0, maxval=10, dtype=tf.float32)
t_2 = tf.random_uniform(shape=[2, 5], minval=0, maxval=10, dtype=tf.int32)
with tf.Session() as sess:
    print sess.run(t_1)
    print
    print sess.run(t_2)
# np.random.uniform,输出 浮点型定域随机值 数组
[[ 5.90669647  6.84431907  5.67390782  2.13535225  6.17888272]
 [ 1.13832828  2.10978447  0.41073584  5.94850748  6.9064396 ]]

# random_uniform,输出 浮点型定域随机值 **单值**
9.2136666451

# tf.random_uniform,输出 浮点型定域随机值 数组
[[ 8.30479622  3.55791092  4.70838642  5.91044331  2.22215414]
 [ 1.59040809  7.77726269  5.59780979  2.02908754  4.63784933]]

# tf.random_uniform,输出 整型定域随机值 数组
[[0 5 0 5 8]
 [9 9 5 3 7]]

tf.random_shuffle

  洗牌神器

tf.random_shuffle (value, seed=None, name=None)

  • 调用 random库(无返回值,仅在原seq上进行洗牌):
seq = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 5]]

import random
r = seq
random.shuffle(r)
print r
print 'seq: ', seq
[[4, 5], [3, 3], [0, 0], [1, 1], [2, 2]]

# 原seq序列已被彻底改变 
seq:  [[4, 5], [3, 3], [0, 0], [1, 1], [2, 2]]
  • 调用 numpy库(无返回值,仅在原seq上进行洗牌):
seq = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 5]]

import numpy as np
r = seq
np.random.shuffle(r)
print r
print 'seq: ', seq
[[3, 3], [2, 2], [0, 0], [4, 5], [1, 1]]

# 原seq序列已被彻底改变 
seq:  [[3, 3], [2, 2], [0, 0], [4, 5], [1, 1]]
  • 调用 tensorflow库(有返回值,并非在原seq上进行洗牌):
seq = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 5]]

import tensorflow as tf
t = tf.random_shuffle(value=seq)
with tf.Session() as sess:
    print sess.run(t)
    print 'seq: ', seq
[[4 5]
 [0 0]
 [2 2]
 [3 3]
 [1 1]]

# 原seq序列不发生变化 
seq:  [[0, 0], [1, 1], [2, 2], [3, 3], [4, 5]]


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值