python tensorflow tf.session类
生活随笔
收集整理的這篇文章主要介紹了
python tensorflow tf.session类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
@tf_export('Session')
class Session(BaseSession):"""A class for running TensorFlow operations. 用于運(yùn)行TensorFlow操作的類。A `Session` object encapsulates the environment in which `Operation`objects are executed, and `Tensor` objects are evaluated. Forexample:“Session”對象封裝了執(zhí)行“操作”對象并評估“張量”對象的環(huán)境。 例如:
# Build a graph. 建立一個圖
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b# Launch the graph in a session. 在session中啟動這個圖。
sess = tf.Session()# Evaluate the tensor `c`. 評估張量c。
print(sess.run(c))
# 結(jié)果為30
A session may own resources, such as`tf.Variable`, `tf.QueueBase`,and `tf.ReaderBase`. It is important to releasethese resources when they are no longer required. To do this, eitherinvoke the `tf.Session.close` method on the session, or usethe session as a context manager. The following two examples areequivalent:session 可能擁有資源,例如“ tf.Variable”,“ tf.QueueBase”和“ tf.ReaderBase”。 當(dāng)不再需要這些資源時,重要的是釋放它們。 為此,請?jiān)跁捝险{(diào)用`tf.Session.close`方法,或?qū)⒃摃捰米魃舷挛墓芾砥鳌?以下兩個示例是等效的:
# Using the `close()` method. 使用close()方法。
sess = tf.Session()
sess.run(...)
sess.close()# Using the context manager. 使用上下文管理器。
with tf.Session() as sess:sess.run(...)
The[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)protocol buffer exposes various configuration options for asession. For example, to create a session that uses soft constraintsfor device placement, and log the resulting placement decisions,create a session as follows:[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.pr oto)協(xié)議緩沖區(qū)公開了會話的各種配置選項(xiàng)。
例如,要創(chuàng)建使用軟約束進(jìn)行設(shè)備放置的會話并記錄生成的放置決策,請按以下方式創(chuàng)建會話:
# Launch the graph in a session that allows soft device placement and
# logs the placement decisions.
# 在允許軟設(shè)備放置的會話中啟動圖形并記錄放置決策。
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True))
def __init__(self, target='', graph=None, config=None):"""Creates a new TensorFlow session. 創(chuàng)建一個新的TensorFlow session。If no `graph` argument is specified when constructing the session,the default graph will be launched in the session. If you areusing more than one graph (created with `tf.Graph()` in the sameprocess, you will have to use different sessions for each graph,but each graph can be used in multiple sessions. In this case, itis often clearer to pass the graph to be launched explicitly tothe session constructor.如果在構(gòu)建session時未指定`graph`參數(shù),則默認(rèn)圖形將在session中啟動。 如果您在同一過程中使用多個圖(通過`tf.Graph()`創(chuàng)建的),則每個圖必須使用不同的session,但是每個圖可以用于多個session。在這種情況下, 通常更清楚地將要顯式啟動的圖傳遞給會話構(gòu)造函數(shù)。Args:target: (Optional. 可選的) The execution engine to connect to.Defaults to using an in-process engine. See[Distributed TensorFlow](https://tensorflow.org/deploy/distributed)for more examples.要連接的執(zhí)行引擎。 默認(rèn)為使用進(jìn)程內(nèi)引擎。 有關(guān)更多示例,請參見[Distributed TensorFlow](https://tensorflow.org/deploy/distributed)。graph: (Optional. 可選的) The `Graph` to be launched (described above).要啟動的“圖”(如上所述)。config: (Optional. 可選的) A[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)protocol buffer with configuration options for the session.具有會話配置選項(xiàng)的[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)協(xié)議緩沖區(qū)。"""super(Session, self).__init__(target, graph, config=config)# NOTE(mrry): Create these on first `__enter__` to avoid a reference cycle.# 在第一個__enter__上創(chuàng)建它們以避免引用循環(huán)。self._default_graph_context_manager = Noneself._default_session_context_manager = None
參考文章:tf.Session
總結(jié)
以上是生活随笔為你收集整理的python tensorflow tf.session类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python namedtuple (命
- 下一篇: python 什么是上下文管理器(Con