TensorFlow原始碼學習--1 Session API reference
學習TensorFlow原始碼,先把API文件扒出來研究一下整體結構: 一下是文件內容的整理,簡單翻譯一下
原文地址:http://www.tcvpr.com/archives/181
TensorFlow C++ Session API reference documentation
TensorFlow’s public C++ API includes only the API for executing graphs, as of version 0.5. To control the execution of a graph from C++: TensorFlow的C++ API只包含執行圖(graphs)的操作,像V0.5一樣,控制執行圖如下:
- Build the computation graph using the [Python API].
- 使用Python API建立一個圖
- Use [
tf.train.write_graph()
] to write the graph to a file.
- 使用[
tf.train.write_graph()
]把圖寫入檔案
Env
[tensorflow::Env]
- An interface used by the tensorflow implementation to access operating system functionality like the filesystem etc.Callers may wish to provide a custom Env object to get fine grain control.All Env implementations are safe for concurrent access from multiple threads without any external synchronization.
- TensorFlow使用此介面接入作業系統功能,例如檔案系統等。呼叫者可以提供自定義的ENV物件來得到細粒度的控制(fine grain control),ENV所有的執行對於併發多執行緒完全支援,不需要外部同步!
[tensorflow::RandomAccessFile]
- A file abstraction for randomly reading the contents of a file.
- 一個檔案的抽象,為了從檔案中隨機讀取內容
[tensorflow::WritableFile]
- A file abstraction for sequential writing.The implementation must provide buffering since callers may append small fragments at a time to the file.
- 一個檔案的抽象,用於按照序列儲存檔案,此執行必須提供Buffer,因為呼叫者對於一個檔案一次可能只增加小的片段。
[tensorflow::EnvWrapper]
- An implementation of Env that forwards all calls to another Env .May be useful to clients who wish to override just part of the functionality of another Env .
- ENV的一個執行,將ENV的所有呼叫連結到其他ENV。對於想要過載(override)其他ENV部分功能的使用者,此類可能有用。
Session
[tensorflow::Session]
-
A Session instance lets a caller drive a TensorFlow graph computation.When a Session is created with a given target, a new Session object is bound to the universe of resources specified by that target. Those resources are available to this session to perform computation described in the GraphDef. After extending the session with a graph, the caller uses the Run() API to perform the computation and potentially fetch outputs as Tensors.
-
一個Session的例項,可以呼叫者啟動一個TensorFlow圖(graph)的計算功能。當一個有目標的Session建立時,一個新的Session物件一定是所有目標(target)指定的資源的總體。那些資源對於此Session是可用的,用來執行在圖中定義(GraphDef)的計算。
-
Example:
-
例子
//c++ tensorflow::GraphDef graph;
// … Create or load graph into “graph”. // This example uses the default options which connects // to a local runtime.
tensorflow::SessionOptions options; std::unique_ptrtensorflow::Session session(tensorflow::NewSession(options));
// Create the session with this graph.
tensorflow::Status s = session->Create(graph); if (!s.ok()) { … } // Run the graph and fetch the first output of the “output” // operation, and also run to but do not return anything // for the “update_state” operation.
std::vectortensorflow::Tensor outputs; s = session->Run({}, {“output:0”}, {“update_state”}, &outputs); if (!s.ok()) { … } // Map the output as a flattened float tensor, and do something // with it.
auto output_tensor = outputs[0].flat(); if (output_tensor(0) > 0.5) { … } // Close the session to release the resources associated with // this session.
session->Close();
-
A Session allows concurrent calls to Run() , though a Session must be created / extended by a single thread.Only one thread must call Close() , and Close() must only be called after all other calls to Run() have returned.
-
Session允許多個執行緒執行Run,但是Session必須由一個執行緒建立/擴充套件。只有一個執行緒能呼叫Close,並且必須在其他執行緒中的所有Run呼叫完成後。
[tensorflow::SessionOptions]
- Configuration information for a Session
- Session的配置資訊
Status
[tensorflow::Status]
- No description
- 狀態資訊,文件無描述
[tensorflow::Status::State]
- No description
- 狀態資訊,文件無描述
Tensor
[tensorflow::Tensor]
- Represents an n-dimensional array of values.
- 表示一個N維值的陣列
[tensorflow::TensorShape]
- No description
- Tensor形狀,文件無描述
[tensorflow::TensorShapeDim]
- No description
- Tensor形狀維數,文件無描述
[tensorflow::TensorShapeUtils]
- Static helper routines for
TensorShape
. Includes a few common predicates on a tensor shape. - 對於
TensorShape
靜態輔助例子,包括一些對於tensor形狀的常見限定
[tensorflow::PartialTensorShape]
- Manages the partially known dimensions of a Tensor and their sizes.
- 管理一個Tensor部分已知的規模以及大小
[tensorflow::PartialTensorShapeUtils]
- Static helper routines for
PartialTensorShape
. Includes a few common predicates on a partially known tensor shape. - 對於
PartialTensorShape
靜態輔助例子,包括一些對於部分tensor形狀的常見限定
[TF_Buffer]
- No description
- TensorFlow的Buffer,文件無描述
Thread
[tensorflow::Thread]
- No Description
- 執行緒操作,文件無描述
[tensorflow::ThreadOptions]
- Options to configure a Thread .Note that the options are all hints, and the underlying implementation may choose to ignore it.
- 執行緒配置,注意:這些描述都是隱藏的,底層實現可以忽略