1.简单实例:向量相加

import tensorflow as tf
with tf.Session():
  input1 = tf.constant([1.0 1.0 1.0 1.0])
  input2 = tf.constant([2.0 2.0 2.0 2.0])
  output = tf.add(input1, input2)
  result = output.eval()
  print result

结果:

两个 tf.() 语句在计算图中创建两个。调用 tf.() 创建两个指定的维度并初始化它们

tf.add() 语句向计算图添加一个加法操作,但不会立即执行

最后调用.eval()时,会触发计算图的执行,获取计算节点的结果(类似于spark)

2. 的使用(变量

import tensorflow as tf
with tf.Session() as sess:
    # Set up two variables, total and weights, that we'll change repeatedly.
    total = tf.Variable(tf.zeros([1, 2]))
    weights = tf.Variable(tf.random_uniform([1, 2]))
    # Initialize the variables we defined above.
    tf.global_variables_initializer().run()
    # This only adds the operators to the graph right now. The assignment
    # and addition operations are not performed yet.
    update_weights = tf.assign(weights, tf.random_uniform([1, 2], -1.0, 1.0))
    update_total = tf.assign(total, tf.add(total, weights))
    for _ in range(5):
        # Actually run the operation graph, so randomly generate weights and then
        # add them into the total. Order does matter here. We need to update
        # the weights before updating the total.
        sess.run(update_weights)
        sess.run(update_total)
        print(weights.eval(), total.eval())

结果:

创建了两个变量 total 和(都是一维的)。total 的所有元素都被初始化为 0,而 的元素被初始化为 -1 和 1 之间的随机数。然后在一些迭代中,变量的元素被更新为 -1 和 1 之间的随机数,然后添加到可变总。

在开始图形计算之前,需要初始化所有变量。调用 tf.bles().run() 来初始化所有变量。

为执行和评估提供环境。

import tensorflow as tf
# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor 'c'.
print (sess.run(c))
sess.close()

一个人可能有一些资源,比如 或 Queue。当我们不再需要它时,我们需要释放这些资源。有两种方式,

调用 .close() 方法;与 tf.() 一起使用来创建一个 () 来执行,它在上下文退出时自动释放。

上面的例子可以写成:

import tensorflow as tf
# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b
with tf.Session() as sess:
    print (sess.run(c))

该类的构造函数如下所示:

tf..(=”,图=无,=无)

如果在创建时未指定 Graph,则将加载默认 Graph。如果在一个进程中创建了多个Graph,则需要创建不同的Graph来加载每个Graph,并且每个Graph可以被加载多个进行计算。

有两种执行或评估的方法:

调用.run()方法:该方法的定义如下,参数为一个或多个or。

tf..run(, =无)

调用 .run() 或 .eval() 方法:这两个方法都接收参数以指定要评估的位置。但是这个参数是可选的,默认为None,也就是说它是在流程中默认计算的。

那么如何设置一个为默认值呢?有两种方法:

在 with 语句中定义的 1. 成为该上下文中的默认值;上面的例子可以修改为:

import tensorflow as tf
# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b
with tf.Session():
   print (c.eval())

2. 在 with 语句中调用 .() 方法。上面的例子可以修改为:

import tensorflow as tf
# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b
sess = tf.Session()
with sess.as_default():
    print (c.eval())
sess.close()

4.图表

使用类来表示可计算的图。图由操作和张量组成,它们表示图的节点(即计算单元)和图的边(即在它们之间流动的数据单元)。

tf.Graph.()

创建一个新的空图表

在 中,总是有一个默认图表。如果要添加到默认图表,只需调用定义的函数(例如 tf.add() )。如果我们需要定义多个Graph,我们需要在with语句中调用Graph.()方法将一个graph设置为默认Graph,这样with语句块中调用的or就会被添加到Graph中。

import tensorflow as tf
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)

如果上例中的sess1.run(c1)和sess2.run(c2)中的c1和c2的位置互换的话,操作会报错. 因为sess1加载的g1中没有c2,同样sess2加载的g2中也没有c1。

5.

一个是图中的计算节点。它将零个或多个对象作为输入,并产生零个或多个对象作为输出。对象是通过直接调用方法(例如 tf.())或 Graph.() 来创建的。

例如 c = tf.(a, b) 意味着创建 a ,它接受 a 和 b 作为输入并产生 c 作为输出。

当一个 Graph 被加载到其中时,你可以调用 .run(op) 来执行 op,或者调用 op.run() 来执行(op.run() 是 tf.().run() 的缩写)。

6.

() 表示 yes 的输出结果。但是,它只是一个符号句柄,不保存输出结果的值。该值可以通过调用 .run() 或 .eval() 获得。一个张量主要保存三个属性:name(名称)、(形状)、type(类型)

import tensorflow as tf
with tf.Session():
    a = tf.constant([1.0, 2.0], name="a1")
    b = tf.constant([3.0, 4.0], name="b1")
    result = tf.add(a, b, name="add1")
    print(result)

1>张量的名称可以以“node:”的形式给出。node 是节点的名称,表示当前张量来自哪个输出。add1:0 表示这个张量是计算节点“add”输出的第一个结果(编号从0开始)

2>shape=(2,) 表示张量是一个数组,这个数组的长度是2

3> 每个张量都有唯一的类型,当类型不匹配时会报错,例如:

© 版权声明
THE END
喜欢就支持一下吧
点赞5赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容