TensorFlow 学习(四)—— computation graph

TensorFlow 的计算需要事先定义一个 computation graph(计算图),该图是一个抽象的结构,只有在评估(evaluate)时,才有数值解,这点和 numpy 不同。这张图由一组节点构成。

>> a = tf.ones((2, 2))
>> tf.reduce_sum(a, reduction_indices=1)
<tf.Tensor 'Sum_2:0' shape=(2,) dtype=float32>
>> tf.reduce_sum(a, reduction_indices=1).eval()
array([ 2.,  2.], dtype=float32)

0. 一个计算图实例


SouthEast

relu = tf.nn.relu(tf.matmul(w, x) + b)

1. TensorFlow 的计算机制

TensorFlow 程序一般可划分为两个流程:

  • construction phase,构建过程,会构建出一个图(graph),即所谓的计算图(computation graph)
  • evaluation phase,执行过程,使用 session 执行构建过程中生成的图中的操作;

2. placeholders:数据的读入

tf.convert_to_tenso() 接口可将 numpy 下的多维数组转化为 tensor,但该函数接收的数据不可规模化。

tf.placeholder() 提供了向计算图(computation graph)读入数据的入口点。

feed_dict关键字参数,类型为 Python 的字典类型,实现了 tf.placeholder() 变量向数据(numpy arrays)的映射。

>> input1 = tf.placeholder(tf.float32)
>> input2 = tf.placeholder(tf.float32)
>> output = tf.mul(input1, input2)
>> with tf.Session() as sess:
        print(sess.run([output], feed_dict={input1: [7.], input2: [2.]}))

3. tf.Graph() 图对象下的成员

  • get_operations():

    graph = tf.Graph()
    names = [op.name for op in model.graph.get_operations() if op.type=='Conv2D']
  • tensor = tf.get_default_graph().get_tensor_by_name(tensor_name)

    • Get the tensor with this name.

转载于:https://www.cnblogs.com/mtcnn/p/9422024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值