怎么在TensorBoard中使用graph模塊-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在TensorBoard中使用graph模塊,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

超過十多年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營(yíng)模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,小程序定制開發(fā),微信開發(fā),app軟件開發(fā)公司,同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營(yíng)銷和我們一樣獲得訂單和生意!

TensorBoard中的graph是一種計(jì)算圖,里面的點(diǎn)用于表示Tensor本身或者運(yùn)算符,圖中的邊則代表Tensor的流動(dòng)或者控制關(guān)系。

怎么在TensorBoard中使用graph模塊

本文主要從代碼的層面,分析graph的數(shù)據(jù)來(lái)源與結(jié)構(gòu)。

一般來(lái)說(shuō),我們?cè)趩?dòng)TensorBoard的時(shí)候會(huì)使用--logdir參數(shù)配置文件路徑(或者設(shè)置數(shù)據(jù)庫(kù)位置),這些日志文件為TensorBoard提供了數(shù)據(jù)。于是我們打開一個(gè)日志文件,查看里面的內(nèi)容

怎么在TensorBoard中使用graph模塊

我們看到,文件是通過二進(jìn)制展示的,因此無(wú)法直接讀取文件的內(nèi)容。

回到瀏覽器中,進(jìn)入graph頁(yè)面,通過開發(fā)者工具發(fā)現(xiàn),構(gòu)造圖的時(shí)候調(diào)用了一個(gè)接口

http://localhost:6006/data/plugin/graphs/graph?large_attrs_key=_too_large_attrs&limit_attr_size=1024&run=task1

用瀏覽器打開這個(gè)地址,看到以下內(nèi)容

node {
 name: "Input/X"
 op: "Placeholder"
 attr {
 key: "_output_shapes"
 value {
  list {
  shape {
   unknown_rank: true
  }
  }
 }
 }
 attr {
 key: "dtype"
 value {
  type: DT_FLOAT
 }
 }
 attr {
 key: "shape"
 value {
  shape {
  unknown_rank: true
  }
 }
 }
}
...

每個(gè)node都能夠與圖中的一個(gè)節(jié)點(diǎn)相對(duì)應(yīng),因此我們可以確定,這個(gè)接口里返回的node,就是構(gòu)成圖所需要的數(shù)據(jù)結(jié)構(gòu)。

那么,TensorBoard是如何將日志文件轉(zhuǎn)化為圖的呢?

TesnorBoard中的每個(gè)模塊都是以plugin存在的,我們進(jìn)入tensorboard/plugin/graph/graphs_plungin.py,在這個(gè)文件中定義了graph相關(guān)的接口

def get_plugin_apps(self):
 return {
  '/graph': self.graph_route,
  '/runs': self.runs_route,
  '/run_metadata': self.run_metadata_route,
  '/run_metadata_tags': self.run_metadata_tags_route,
 }

我們可以看到,‘/graph'這個(gè)接口返回值為self.graph_route,在這個(gè)文件中搜索graph_route方法

 @wrappers.Request.application
 def graph_route(self, request):
 """Given a single run, return the graph definition in protobuf format."""
 run = request.args.get('run')
 if run is None:
  return http_util.Respond(
   request, 'query parameter "run" is required', 'text/plain', 400)
 
 limit_attr_size = request.args.get('limit_attr_size', None)
 if limit_attr_size is not None:
  try:
  limit_attr_size = int(limit_attr_size)
  except ValueError:
  return http_util.Respond(
   request, 'query parameter `limit_attr_size` must be an integer',
   'text/plain', 400)
 
 large_attrs_key = request.args.get('large_attrs_key', None)
 
 try:
  result = self.graph_impl(run, limit_attr_size, large_attrs_key)
 except ValueError as e:
  return http_util.Respond(request, e.message, 'text/plain', code=400)
 else:
  if result is not None:
  (body, mime_type) = result # pylint: disable=unpacking-non-sequence
  return http_util.Respond(request, body, mime_type)
  else:
  return http_util.Respond(request, '404 Not Found', 'text/plain',
         code=404)

在這個(gè)方法中,分別取了“run”,”limit_attr_size“和“l(fā)arge_attrs_key”三個(gè)參數(shù),和前面url所調(diào)用的參數(shù)一致,說(shuō)明這個(gè)是我們要找的方法。在方法的最后,調(diào)用了self.graph_impl生成了圖,我們繼續(xù)查看這個(gè)方法

def graph_impl(self, run, limit_attr_size=None, large_attrs_key=None):
 """Result of the form `(body, mime_type)`, or `None` if no graph exists."""
 try:
  graph = self._multiplexer.Graph(run)
 except ValueError:
  return None
 # This next line might raise a ValueError if the limit parameters
 # are invalid (size is negative, size present but key absent, etc.).
 process_graph.prepare_graph_for_ui(graph, limit_attr_size, large_attrs_key)
 return (str(graph), 'text/x-protobuf') # pbtxt

這個(gè)方法調(diào)用了self._multiplexer.Graph(run)生成圖。_multiplexer是一個(gè)event_multiplexer實(shí)例,在graph_plugln初始化時(shí)通過base_plaugin.TBContext獲得。

 def __init__(self, context):
 """Instantiates GraphsPlugin via TensorBoard core.
 Args:
  context: A base_plugin.TBContext instance.
 """
 self._multiplexer = context.multiplexer

進(jìn)入tensorboard/backend/event_processing/event_multiplexer,找到Graph方法

def Graph(self, run):
 """Retrieve the graph associated with the provided run.
 Args:
  run: A string name of a run to load the graph for.
 Raises:
  KeyError: If the run is not found.
  ValueError: If the run does not have an associated graph.
 Returns:
  The `GraphDef` protobuf data structure.
 """
 accumulator = self.GetAccumulator(run)
 return accumulator.Graph()
 
 def GetAccumulator(self, run):
 """Returns EventAccumulator for a given run.
 Args:
  run: String name of run.
 Returns:
  An EventAccumulator object.
 Raises:
  KeyError: If run does not exist.
 """
 with self._accumulators_mutex:
  return self._accumulators[run]

Graph方法獲取了run對(duì)應(yīng)的accumulator實(shí)例,并返回了這個(gè)實(shí)例的Graph方法的返回值。我們進(jìn)入tensorboard/backend/event_processing/event_accumulator,找到Graph()方法

 def Graph(self):
 """Return the graph definition, if there is one.
 If the graph is stored directly, return that. If no graph is stored
 directly but a metagraph is stored containing a graph, return that.
 Raises:
  ValueError: If there is no graph for this run.
 Returns:
  The `graph_def` proto.
 """
 graph = tf.GraphDef()
 if self._graph is not None:
  graph.ParseFromString(self._graph)
  return graph
 raise ValueError('There is no graph in this EventAccumulator')

事實(shí)上,它返回了一個(gè)GraphDef圖,因此我們也可以通過將日志轉(zhuǎn)換為GraphDef的方式讀取日志。

# 導(dǎo)入要用到的基本模塊。為了在python2、python3 中可以使用E侶兼容的 print 函數(shù)
from __future__ import print_function
import numpy as np
import tensorflow as tf
 
# 創(chuàng)建圖和Session
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
 
#日志路徑
model_fn = '/log/events.out.tfevents.1535957014.ubuntu'
 
for e in tf.train.summary_iterator(model_fn):
 if e.HasField('graph_def'):
  graph = e.graph_def;
  graph_def = tf.GraphDef()
  graph_def.ParseFromString(graph)
  print(graph_def)

關(guān)于怎么在TensorBoard中使用graph模塊就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:怎么在TensorBoard中使用graph模塊-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article42/djjcec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、網(wǎng)站導(dǎo)航外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化