tensorflow 的常量
tensorflow 的常量是constant
用法tf.constant(value, dtype, shape, name,verify_shape)
常用的几个个参数。value, dtype, shape, name看名字也应该知道value是必填的选项,可一是,字符串,数组,数字等。。。
dtype是类型。默认为None一般运算是tf.float32,或者tf.float64,当然也有tf.int32, tf.int64, tf.int16…………和c语言差不多。
shape 是形状。默认也是None。一般用于数组的时候用,和numpy差不多。
name是名字。默认也是None。主要是在tensorboard是清晰。一般填字符串
verify_shape 很少用。默认是False,如果修改为True的话表示检查value的形状与shape是否相符,如果不符会报错。
下面是栗子1
这个例子是value = 1
import tensorflow as tf
tensor = tf.constant(value=1)
print('TF对象':,tensor)
with tf.Session() as sess:
print('结果:', sess.run(tensor))
打印结果
TF对象: Tensor(“Const:0”, shape=(), dtype=int32)
结果: 1
栗子2
import tensorflow as tf
tensor = tf.constant(value=[1,2,3])
with tf.Session() as sess:
print('结果:', sess.run(tensor))
打印结果
TF对象: Tensor(“Const:0”, shape=(3,), dtype=int32)
结果: [1 2 3]
栗子3
import tensorflow as tf
tensor = tf.constant(value=[1,2],dtype=tf.float32)
print('TF对象:',tensor)
with tf.Session() as sess:
print('结果:', sess.run(tensor))
打印结果
TF对象: Tensor(“Const:0”, shape=(2,), dtype=float32)
结果: [1. 2.]
栗子 4
import tensorflow as tf
tensor = tf.constant(value=[[1,2]],shape=[2,3],dtype=tf.float32,name='TENSOR')
print('TF对象:',tensor)
with tf.Session() as sess:
print('结果:', sess.run(tensor))
打印结果
TF对象: Tensor(“TENSOR:0”, shape=(2, 3), dtype=float32)
结果: [[1. 2. 2.]
[2. 2. 2.]]
这里name 也打印出来了。并将[1,2]这个数组的性转转换了。转换为shape=(2, 3)。
注意,转换钱的大小要小于转换后的大小。我的理解是。转换前共有两个数字,转换后不能少于两个数字。不然会丢失数字。会报错。这是我的理解。如果小于的话,就用最后的一个数字填充。
除了上面这些还有很多
下面在举些例子
tf.zeros([3, 3], dtype=tf.int32)
表示生成一个3X3全是0的数组。
tf.ones([3, 3], dtype = tf.int32)
表示生成一个3X3全是1的数组。
tf.ones_like()
传入一个数组然后全部转化为1
tf.zeros_like()
传入一个数组然后全部转化为0
tf.range()
tf.range(start, limit, delta=1, dtype=None, name=‘str’)
和原生的range()函数差不多。就是多了一个dtype和name
随机数
随机数很多,有正态分布,均匀分布…………
正态分布:
tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
shape:代表张量的形状。
mean:是正态分布的均值。
stddev:是正态分布的标准差。
dtype: 输出的数据类型。
seed:是随机种子。
栗子
import tensorflow as tf
tensor0 = tf.random_normal(shape=[3,3],mean=0.,stddev=10.,dtype=tf.float32)
with tf.Session() as sess:
print(sess.run(tensor0))
打印结果
[[ -5.347887 2.6160254 9.295669 ]
[-10.650595 7.5505447 5.2306995]
[ -0.9483523 -17.527641 0.6108615]]
均匀分布
tf.random_uniform(shape,minval=0,maxval=None,
dtype=tf.float32,seed=None,name=None
)
shape:输出张量的形状。
minval:生成的随机值范围的下限;默认为0。
maxval:要生成的随机值范围的上限。如果 dtype 是浮点,则默认为1 。
dtype:输出的类型:float16、float32、float64、int32、orint64。
seed:用于为分布创建一个随机种子。
还是举个栗子
import tensorflow as tf
tensor0 = tf.random_uniform(shape=[3,3],minval=0,maxval=10,dtype=tf.int32)
with tf.Session() as sess:
print(sess.run(tensor0))
打印结果
[[2 3 8]
[1 2 4]
[8 1 1]]
随机打乱 洗牌
tf.random_shuffle(value,seed=None,name=None
)
value:将被打乱的张量。
seed:用于为分布创建一个随机种子。
栗子:
import tensorflow as tf
list = [[1,2],[3,4],[5,6],[7,8],[9,10]]
tensor0 = tf.random_shuffle(list,seed=1,name=None)
with tf.Session() as sess:
print(sess.run(tensor0))
打印结果
[[ 5 6]
[ 9 10]
[ 1 2]
[ 7 8]
[ 3 4]]
本来是
[[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]]
不一一全部介绍了
感兴趣的可以去看看官方这个网站
本来还打算在这一篇把变量和占位符也讲了的。。。但是发现过了这么就。下一篇在见吧。