Tensorflow函数说明(1)

    xiaoxiao2021-03-25  9

    其一:

    Graph 表示可计算的图,其中包含 operation (节点) 和 tensor (边)对象。开始执行程序时会有默认的图建立,可以用 tf.get_default_graph() 来访问。添加操作到默认的图里面可以用下面的方法。

    c = tf.constant(4.0) assert c.graph is tf.get_default_graph()即不指明创建新图,则所有操作都加到默认的图中。若要创建另外的图,可用下面的方法 # 1. Using Graph.as_default(): g = tf.Graph() with g.as_default(): c = tf.constant(5.0) assert c.graph is g # 2. Constructing and making default: with tf.Graph().as_default() as g: c = tf.constant(5.0) assert c.graph is g在 with tf.Graph().as_default() 下,所有的操作都会加入到该图中而不是默认的图。

    另外需要注意的是

    g1 = tf.Graph() with g1.as_default(): c1 = tf.constant([1.0]) with tf.Graph().as_default() as g2: c2 = tf.constant([2.0]) with tf.Session(graph=g1) as sess1: print sess1.run(c1) with tf.Session(graph=g2) as sess2: print sess2.run(c2) # result: # [ 1.0 ] # [ 2.0 ]在这种情况下如果交换 c1 和 c2 在 Session 中的位置,则会报错,因为  c1 和 c2 是定义在不同的图中的。

    额外要补充的一个是关于 Session 的函数 tf.Session.__init__(target='', graph=none, config=none)

    target: 分布式会用到

    graph: 如果没有指定则放入默认的图,否则放入指定的图,下面的操作都在指定的图中寻找运行

    config: 不介绍

    其二:

    tf.Graph.add_to_collection/tf.Graph.add_to_collections/tf.add_to_collection

    该函数用于将一值加到某一collection上,下面是函数原型。

    tf.Graph.add_to_collection(name, value)    (tf.add_to_collection的用法与此相同

    name: collection的名字,这里collection可以看做一个集合,里面存储了加进去的值

    value: 要加到collection的值。

    tf.Graph.add_to_collections(names, value) 

    names: collections的名字,多个collection

    value: 要加到collections的值。

    其三:

    tf.add_n(inputs, name=none) 该函数返回输入的所有的 tensor 的值的和 

    inputs: 输入为多个 tensor

    此外还有,tf.add(x,y,name=none), tf.add(x,y,name=none), tf.sub(x,y,name=none), tf.mul(x,y,name=none), tf.div(x,y,name=none) 等许多数学函数,可以参考这里。

    其他关于 graph 的函数以后碰到再补充。

    文章参考:

    http://blog.csdn.net/xierhacker/article/details/53860379

    http://www.cnblogs.com/lienhua34/p/5998853.html

    转载请注明原文地址: https://ju.6miu.com/read-149584.html

    最新回复(0)