2017-02-02 24 views
7

Có một vài câu trả lời SO khi tạo tóm tắt vô hướng tùy chỉnh trong TF (herehere), nhưng tôi không thể tìm thấy bất kỳ thứ gì khi tạo tóm tắt biểu đồ tùy chỉnh tùy chỉnh. Tài liệu dường như rất thiếu cho tóm tắt tùy chỉnh. Tôi có một mảng numpy mà tôi muốn làm một bản tóm tắt của - bất kỳ ý tưởng về cách thức?Tạo tóm tắt biểu đồ Tensorflow tùy chỉnh

(tf.Summary.Value có trường histo mà tôi đã thử sử dụng, nhưng nó yêu cầu một dòng lưu lượng :: HistogramProto; không có tài liệu nào về lớp đó, vì vậy tôi không biết cách tạo nó. đã thử tạo một ví dụ thất bại tối thiểu dưới đây).

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

Có một tệp .proto ở đâu đó xác định loại thông báo biểu đồ –

+0

HistogramProto được định nghĩa ở đây: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/summary.proto. Ngoài ra còn có một số mã thử nghiệm tạo một mã: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tensorboard/scripts/generate_testdata.py nhưng tôi không thể tìm thấy bất kỳ mô hình nào sử dụng mã đó. Bạn có thể gửi một vấn đề Github để cải thiện tài liệu (hoặc gửi một yêu cầu kéo!) –

Trả lời

1

Đoạn mã này hoạt động:

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) 

Các source.

Các vấn đề liên quan