python tensorflow tf.session类
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                python tensorflow tf.session类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            @tf_export('Session')
class Session(BaseSession):"""A class for running TensorFlow operations. 用于運行TensorFlow操作的類。A `Session` object encapsulates the environment in which `Operation`objects are executed, and `Tensor` objects are evaluated. Forexample:“Session”對象封裝了執行“操作”對象并評估“張量”對象的環境。 例如:
 
# 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))
# 結果為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”。 當不再需要這些資源時,重要的是釋放它們。 為此,請在會話上調用`tf.Session.close`方法,或將該會話用作上下文管理器。 以下兩個示例是等效的:
 
# 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)協議緩沖區公開了會話的各種配置選項。 
例如,要創建使用軟約束進行設備放置的會話并記錄生成的放置決策,請按以下方式創建會話:
 
# Launch the graph in a session that allows soft device placement and
# logs the placement decisions.
# 在允許軟設備放置的會話中啟動圖形并記錄放置決策。
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. 創建一個新的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.如果在構建session時未指定`graph`參數,則默認圖形將在session中啟動。 如果您在同一過程中使用多個圖(通過`tf.Graph()`創建的),則每個圖必須使用不同的session,但是每個圖可以用于多個session。在這種情況下, 通常更清楚地將要顯式啟動的圖傳遞給會話構造函數。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.要連接的執行引擎。 默認為使用進程內引擎。 有關更多示例,請參見[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.具有會話配置選項的[`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)協議緩沖區。"""super(Session, self).__init__(target, graph, config=config)# NOTE(mrry): Create these on first `__enter__` to avoid a reference cycle.# 在第一個__enter__上創建它們以避免引用循環。self._default_graph_context_manager = Noneself._default_session_context_manager = None
 
                        
                        
                        參考文章:tf.Session
總結
以上是生活随笔為你收集整理的python tensorflow tf.session类的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python namedtuple (命
- 下一篇: python 什么是上下文管理器(Con
