tensorflow笔记

来源:互联网 发布:压花辊雕刻加工编程 编辑:程序博客网 时间:2024/06/15 00:32

1、one_hot

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

意思就是,读(如MNIST)数据集的时候
把标签6的值,变成[0,0,0,0,0,0,1,0,0,0]的标签数组。

而onehot标签则是顾名思义,一个长度为n的数组,只有一个元素是1.0,其他元素是0.0。例如在n为4的情况下,标签2对应的onehot标签就是 0.0 0.0 1.0 0.0 

使用onehot的直接原因是现在多分类cnn网络的输出通常是softmax层,而它的输出是一个概率分布,从而要求输入的标签也以概率分布的形式出现,进而算交叉熵之类。


2、reduce_mean

求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)

求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)

参数1--input_tensor:待求值的tensor。

参数2--reduction_indices:在哪一维上求解。

参数(3)(4)可忽略

第一维表示列  0;第二微表示行 1;

3、cast()

tf.cast(x, dtype, name=None)将x或者x.values转换为dtype
# tensor a is [1.8, 2.2], dtype=tf.float
tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int324、run()和eval()

op.run() is a shortcut for calling tf.get_default_session().run(op)  应用的是操作

t.eval() is a shortcut for calling tf.get_default_session().run(t)   应用的是tensor节点

The difference is in Operations vs. Tensors. Operations use run() and Tensors use eval().

# Using `Session.run()`.
sess
= tf.Session()
c
= tf.constant(5.0)
print(sess.run(c))

# Using `Tensor.eval()`.
c
= tf.constant(5.0)
with tf.Session():
 
print(c.eval())
5、truncated_normal

tf.truncated_normal(shape, mean, stddev) :shape表示生成张量的维度,mean是均值,stddev是标准差。这个函数产生正太分布,均值和标准差自己设定。这是一个截断的产生正太分布的函数,就是说产生正太分布的值如果与均值的差值大于两倍的标准差,那就重新生成。和一般的正太分布的产生随机数据比起来,这个函数产生的随机数与均值的差距不会超过两倍的标准差,但是一般的别的函数是可能的。