美文网首页
TensorFlow常用函数

TensorFlow常用函数

作者: aaa小菜鸡 | 来源:发表于2019-06-28 16:29 被阅读0次

    2019-06-28

    import tensorflow as tf 后,tf.什么什么。
    都是tensorflow中需要理解的概念,不涉及神经网络相关概念的理解。

    tf.Variable()

    (对于《TensorFlow实战(黄文坚)》P6,自己的理解)

    • 这个一定是最常见的一个了,不就是“变量”嘛,可是它跟我们平常理解的变量有啥子区别?首先,tensorflow里流动的是tensors,而正因为tensors是流动着的,所以我们是抓不到它的。而variable却是一种你抓或不抓它都在那的tensor,每次建一个variable,在计算图中就多了一个节点。那为啥需要它不随tensors逐流呢?因为随着每次迭代,不可能保留所有数据,比如上一层算出来的feature maps,在下一层用完后就得扔掉;但是像是weights和biases这些参数,只有保留下来,才能做梯度下降等,达到优化参数的目的。

    tf.Session()

    (这边主要参考《TensorFlow实战(黄文坚)》P6)

    • session是tensorflow中非常基本的概念,是用户使用tensorflow时的交互式接口。用户通过Session的extend方法添加新的节点和边,如此创建计算图,然后通过Session的run方法执行计算图。

    tf.add_to_collection()

    参考:tf.add_to_collection
    TensorFlow学习--tf.add_to_collection与tf.get_collection使用

    • tf.add_to_collection('collection_name', tensor)
      将tensor添加到列表collection_name中
    • tf.get_collection('collection_name')
      返回名称为collection_name的列表的tensors
    • tf.add_n(tf.get_collection('collection_name'))
      返回名称为collection_name的列表的tensors的加和
    import tensorflow as tf
    tf.add_to_collection('losses', tf.constant(2.2))
    tf.add_to_collection('losses', tf.constant(3.))
    with tf.Session() as sess:
        print(sess.run(tf.get_collection('losses')))
        print(sess.run(tf.add_n(tf.get_collection('losses'))
    
    结果:
    [2.2, 3.0] 
    5.2
    注意: 
    使用tf.add_n对列表元素进行相加时,列表内元素类型必须一致,否则会报错。
    

    tf.add()、tf.nn.bias_add()、tf.add_n()

    参考:Tensorflow——tf.nn.bias_add和tf.add、tf.add_n

    • tf.add(x, y)
      x和y都必须是tensor,且类型必须一致。
    • tf.nn.bias_add(Wx, b)
      tf.add()的特例,其中b必须是一维的,个数与Wx的最后一维相同。
      比如Wx的shape是[28, 28, 64],那么b是[64],符合神经网络结构。
    • tf.add_n(a_list)
      输入是一个列表,实现列表中所有元素的相加,列表元素可以是tensor、矩阵等。

    tf.reduce_mean()、tf.reduce_sum()、tf.reduce_max()

    • 这几位大兄弟都是一个概念,最终效果是降维了,所以叫reduce。

      直接上这个看了忘忘了看的图:要记住的是0是纵向、1是横向。

    with tf.name_scope('conv1') as scope:

    • 将scope内生成的Variable自动命名为conv1/xxx,便于区分不同卷积层之间的组件

    常用数据类型

    • tf.float32()
    • tf.int32()

    常用参数

    • shape=
    • stddev=
    • wl=
    • padding=
    • name=

    常用函数

    1. 数据:
    • tf.Variable()
    • tf.Variable(tf.constant())
    • tf.Variable(tf.truncated_normal()):截断正态分布
    • tf.Variable(tf.random_nromal())
    • tf.Variable(tf.ones())
    • tf.reshape() tf.reshape().get_shape()
    1. 结构构建:
    • tf.add()
    • tf.matmul()
    • tf.nn.bias_add():专用于Wx+b
    • tf.nn.conv2d()
    • tf.nn.max_pool()
    • tf.nn.relu()
    • tf.nn.lrn():LRN
    • tf.nn.dropout():去掉一定比率的神经元
    1. 损失函数:
    • tf.nn.l2_loss():L2范数损失
    • cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y_conv, y_GT):交叉熵损失
    • tf.reduce_mean(cross_entropy):沿某个轴的平均值,0纵1横,最终效果是reduce降维了
    1. 优化器:
    • tf.gradients(target, parameters):梯度下降
    • tf.train.AdamOptimizer(learning_rate).minimize(loss):Adam优化
    1. 准确率:
    • tf.nn.in_top_k():值最高的k类的值
    1. 运行:
    • sess = tf.Session():定义一个session
    • tf.global_variables_initailizer().run():初始化所有变量
    • sess.run():使tensors流动起来

    相关文章

      网友评论

          本文标题:TensorFlow常用函数

          本文链接:https://www.haomeiwen.com/subject/pvdscctx.html