2017-02-02 4 views
7

TF (herehere)에 사용자 정의 스칼라 요약을 작성하는 데 대한 SO 응답이 몇 가지 있지만 사용자 정의 막대 그래프 요약 작성시 아무것도 찾을 수 없습니다. 설명서에 맞춤 요약이 부족한 것 같습니다. 나는 내가 어떻게 요약 할 수 있는지에 대한 아이디어를 가지고있다.사용자 정의 Tensorflow 히스토그램 요약 작성

(tf.Summary.Value에는 사용하려고 시도한 histo 필드가 있지만 tensorflow :: HistogramProto가 필요합니다. 따라서 해당 클래스에 대한 설명서가 없으므로 작성 방법이 손실됩니다. 아래에서 최소한 실패한 예제를 만들려고 시도했습니다.)

import tensorflow as tf 
import numpy as np 
sess = tf.Session() 
means_placeholder = tf.placeholder(tf.float32) 
tf.summary.histogram('means', means_placeholder) 
summaries = tf.summary.merge_all() 
writer = tf.summary.FileWriter('./summaries') 
means = np.random.random(10)  
writer.add_summary(tf.Summary(value=[tf.Summary.Value(tag='means', histo=means)])) 
+0

히스토그램 메시지 유형을 정의하는 어딘가에 .proto 파일이 있습니다. –

+0

HistogramProto는 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto에서 정의됩니다. 또한 하나의 테스트 코드가 생성됩니다 : https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tensorboard/scripts/generate_testdata.py 그러나 그것을 사용하는 모델을 찾을 수 없습니다. 문서를 개선하기 위해 Github 문제를 제기 할 수 있습니다 (또는 풀 요청 보내기). –

답변

1

이 코드 조각 작품 :

import tensorflow as tf 

import numpy as np 

def log_histogram(writer, tag, values, step, bins=1000): 
    # Convert to a numpy array 
    values = np.array(values) 

    # Create histogram using numpy 
    counts, bin_edges = np.histogram(values, bins=bins) 

    # Fill fields of histogram proto 
    hist = tf.HistogramProto() 
    hist.min = float(np.min(values)) 
    hist.max = float(np.max(values)) 
    hist.num = int(np.prod(values.shape)) 
    hist.sum = float(np.sum(values)) 
    hist.sum_squares = float(np.sum(values**2)) 

    # Requires equal number as bins, where the first goes from -DBL_MAX to bin_edges[1] 
    # See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto#L30 
    # Thus, we drop the start of the first bin 
    bin_edges = bin_edges[1:] 

    # Add bin edges and counts 
    for edge in bin_edges: 
     hist.bucket_limit.append(edge) 
    for c in counts: 
     hist.bucket.append(c) 

    # Create and write Summary 
    summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) 
    writer.add_summary(summary, step) 
    writer.flush() 

sess = tf.Session() 
placeholder = tf.placeholder(tf.float32) 

tf.summary.histogram('N(0,1)', placeholder) 
summaries = tf.summary.merge_all() 
writer = tf.summary.FileWriter('./summaries') 

mu, sigma = 0, 0.1 # mean and standard deviation 
s = np.random.normal(mu, 1, 10000) 
log_histogram(writer, 'N(0,1)', s, 1, bins=100) 

source.

관련 문제