2017-02-02 7 views
7
बनाएं

अतः एक कस्टम अदिश (here और here) TF में सारांश बनाने पर जवाब की एक जोड़ी रहे हैं, लेकिन मैं एक कस्टम हिस्टोग्राम सारांश बनाने पर कुछ भी नहीं मिल सकता है। प्रलेखन कस्टम सारांश के लिए बहुत कमी लग रहा है। मेरे पास एक सुस्त सरणी है जिसका मैं सारांश बनाना चाहता हूं - इस पर कोई विचार कैसे?एक कस्टम Tensorflow हिस्टोग्राम सारांश

(tf.Summary.Value में एक हिस्टो फ़ील्ड है जिसका मैंने उपयोग करने का प्रयास किया है, लेकिन इसके लिए एक tensorflow :: हिस्टोग्रामप्रोटो की आवश्यकता है; उस वर्ग पर कोई दस्तावेज़ीकरण नहीं है, इसलिए मुझे इसे बनाने के तरीके पर नुकसान हो रहा है। नीचे एक न्यूनतम असफल उदाहरण बनाने की कोशिश की है)।

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

परिभाषित करता है हिस्टोग्रामप्रोटो को यहां परिभाषित किया गया है: 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 लेकिन मुझे इसका उपयोग करने वाले कोई भी मॉडल नहीं मिल रहे हैं। प्रलेखन में सुधार करने के लिए आप एक गिथब मुद्दा दर्ज कर सकते हैं (या पुल अनुरोध भेजें!) –

उत्तर

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