2017-06-04 72 views
1

我有一些非常大的張量彙總摘要。如果這些使用張量板繪製,我可以從它們下載CSV文件。從Tensorflow摘要中獲取CSV

但是,繪製這些使用張量板需要很長時間。我在the docs中發現有一種方法可以直接在Python中讀取摘要。這種方法是summary_iterator,可以如下使用:

import tensorflow as tf 

for e in tf.train.summary_iterator(path to events file): 
    print(e) 

我可以用這個方法來直接創建CSV文件?如果是這樣,我該怎麼做?這會節省很多時間。這樣做會是這樣的

回答

0

一種可能的方式:

from tensorflow.python.summary import event_accumulator 
import numpy as np 
import pandas as pd 
import sys 

def create_csv(inpath, outpath): 
    sg = {event_accumulator.COMPRESSED_HISTOGRAMS: 1, 
      event_accumulator.IMAGES: 1, 
      event_accumulator.AUDIO: 1, 
      event_accumulator.SCALARS: 0, 
      event_accumulator.HISTOGRAMS: 1} 
    ea = event_accumulator.EventAccumulator(inpath, size_guidance=sg) 
    ea.Reload() 
    scalar_tags = ea.Tags()['scalars'] 
    df = pd.DataFrame(columns=scalar_tags) 
    for tag in scalar_tags: 
     events = ea.Scalars(tag) 
     scalars = np.array(map(lambda x: x.value, events)) 
     df.loc[:, tag] = scalars 
    df.to_csv(outpath) 

if __name__ == '__main__': 
    args = sys.argv 
    inpath = args[1] 
    outpath = args[2] 
    create_csv(inpath, outpath) 

請注意,此代碼將加載整個事件文件到內存中,所以最好在集羣上運行此。有關EventAccumulator的參數sg的信息,請參閱this SO question

一個額外的改進可能不僅是存儲每個標量的value,而且還存儲step