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))

执行结果:

 

转载于:https://www.cnblogs.com/Yorkisme/p/7604353.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值