tf.Session

来源:互联网 发布:好看的输入法软件 编辑:程序博客网 时间:2024/05/22 03:02

tf.Session

前言

初入Tensorflow,什么都不理解。于是借助sess = tf.Session()提出了为什么这么写的问题,以此为基础了解tf的使用。

官方


class tf.Session

A class for running TensorFlow operations.

A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated. For example:

# Build a graph.a = tf.constant(5.0)b = tf.constant(6.0)c = a * b# Launch the graph in a session.sess = tf.Session()# Evaluate the tensor `c`.print sess.run(c)

A session may own resources, such as variables, queues, and readers. It is important to release these resources when they are no longer required. To do this, either invoke the close() method on the session, or use the session as a context manager. The following two examples are equivale

# Using the `close()` method.sess = tf.Session()sess.run(...)sess.close()# Using the context manager.with tf.Session() as sess:  sess.run(...)

理解

  1. tf.Session():需要在启动session之前构建整个计算图,然后启动该计算图。

Reference

  • 官方文档:Running Graph
原创粉丝点击