tensorflow 函数

作者: K_Gopher | 来源:发表于2017-06-20 15:33 被阅读401次

    一,tensorflow中有一类在tensor的某一维度上求值的函数。如:
    求最大值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)可忽略
    举例说明:

    'x' is [[1., 2.]

    [3., 4.]]

    x是一个2维数组,分别调用reduce_*函数如下:
    首先求平均值:
    tf.reduce_mean(x) ==> 2.5 #如果不指定第二个参数,那么就在所有的元素中取平均值
    tf.reduce_mean(x, 0) ==> [2., 3.] #指定第二个参数为0,则第一维的元素取平均值,即每一列求平均值
    tf.reduce_mean(x, 1) ==> [1.5, 3.5] #指定第二个参数为1,则第二维的元素取平均值,即每一行求平均值

    同理,还可用tf.reduce_max()求最大值等。

    二。 tf.square(x) 对x内所有的元素进行平方操作

    三。tf.global_variables
    tf.global_variables或者tf.all_variables都是获取程序中的变量,不同的版本是不同的,目前的tensorflow版本应该用前面的那个,返回的值是变量的一个列表

    例如:

    import tensorflow as tf;
    import numpy as np;
    import matplotlib.pyplot as plt;

    v = tf.Variable(tf.constant(0.0, shape=[1], dtype=tf.float32), name='v')
    v1 = tf.Variable(tf.constant(5, shape=[1], dtype=tf.float32), name='v1')

    variables = tf.global_variables()

    print variables[0].name
    print variables[1].name

    输出:

    v:0
    v1:0

    四。 tf.train.Saver函数的用法之保存全部变量和模型
    用于保存模型,以后再用就可以直接导入模型进行计算,方便。
    例如:

    [python] view plain copy

    import tensorflow as tf;
    import numpy as np;
    import matplotlib.pyplot as plt;

    v1 = tf.Variable(tf.constant(1, shape=[1]), name='v1')
    v2 = tf.Variable(tf.constant(2, shape=[1]), name='v2')

    result = v1 + v2

    init = tf.initialize_all_variables()

    saver = tf.train.Saver()

    with tf.Session() as sess:
    sess.run(init)
    saver.save(sess, "/home/penglu/Desktop/lp/model.ckpt")
    # saver.restore(sess, "/home/penglu/Desktop/lp/model.ckpt")
    # print sess.run(result)

    结果:

    下次需要使用模型就可以用下面的代码:

    [python] view plain copy

    import tensorflow as tf;
    import numpy as np;
    import matplotlib.pyplot as plt;

    v1 = tf.Variable(tf.constant(1, shape=[1]), name='v1')
    v2 = tf.Variable(tf.constant(2, shape=[1]), name='v2')

    result = v1 + v2

    init = tf.initialize_all_variables()

    saver = tf.train.Saver()

    with tf.Session() as sess:
    saver.restore(sess, "/home/penglu/Desktop/lp/model.ckpt")
    print sess.run(result)

    [python] view plain copy

    [python] view plain copy

    或者这个代码:

    [python] view plain copy

    import tensorflow as tf;
    import numpy as np;
    import matplotlib.pyplot as plt;

    [python] view plain copy

    saver = tf.train.import_meta_graph('/home/penglu/Desktop/lp/model.ckpt.meta')
    with tf.Session() as sess:
    <span style="white-space:pre"> </span>saver.restore(sess, "/home/penglu/Desktop/lp/model.ckpt")
    <span style="white-space:pre"> </span>print sess.run(tf.get_default_graph().get_tensor_by_name('add:0'))

    [python] view plain copy

    输出:

    三。np.expand_dims()加维

    四。np.mean(data)求data的平均数 np.std(data)计算标准差 np.median(data)计算中位数

    相关文章

      网友评论

        本文标题:tensorflow 函数

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