Tensorflow Session&Graph学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tensorflow Session&Graph学习笔记相关的知识,希望对你有一定的参考价值。
tensorflow的核心部分是Graph。Graph可以看成由两部分组成的:Node和Edge。 Node是操作,其中包括生成数据的操作以及数据运算的操作,edge可以看成流动的数据
Graph中tensor的命名有三种方式:1. default是通过Operation命名,如<OP_NAME>:<i>,其中<OP_NAME>是操作的名字,<i>是操作的索引
2. 通过name来命名,如tf.constant(42.0, name="answer")
3. 通过命名域来命名,如with tf.name_scope("answer"): ,会添加answer/到变量名前
Programmers‘ Guide上给的例子:
c_0 = tf.constant(0, name="c") # => operation named "c" # Already-used names will be "uniquified". c_1 = tf.constant(2, name="c") # => operation named "c_1" # Name scopes add a prefix to all operations created in the same context. with tf.name_scope("outer"): c_2 = tf.constant(2, name="c") # => operation named "outer/c" # Name scopes nest like paths in a hierarchical file system. with tf.name_scope("inner"): c_3 = tf.constant(3, name="c") # => operation named "outer/inner/c" # Exiting a name scope context will return to the previous prefix. c_4 = tf.constant(4, name="c") # => operation named "outer/c_1" # Already-used name scopes will be "uniquified". with tf.name_scope("inner"): c_5 = tf.constant(5, name="c") # => operation named "outer/inner_1/c"
print(vars())
使用vars() 可以查看所有变量的名称
Session 是连接客户端和C++实际执行后台的工具
sess.run()可以执行Graph中的运算,一般有两步。1、变量的初始化,2、相关变量的操作
x = tf.constant([[37.0, -23.0], [1.0, 4.0]]) w = tf.Variable(tf.random_uniform([2, 2])) y = tf.matmul(x, w) output = tf.nn.softmax(y) init_op = w.initializer with tf.Session() as sess: # Run the initializer on `w`. sess.run(init_op) # Evaluate `output`. `sess.run(output)` will return a NumPy array containing # the result of the computation. print(sess.run(output))
执行结果:
以上是关于Tensorflow Session&Graph学习笔记的主要内容,如果未能解决你的问题,请参考以下文章
Tensorflow之调试(Debug) && tf.py_func()