You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow C++ API:如何用FileWriter查看TensorBoard及获取对应张量名

Hey there! Let's break down your two TensorFlow C++ API questions clearly, with code examples to make things concrete.

1. Using FileWriter Summary with TensorFlow C++ API for TensorBoard

To log summaries to TensorBoard using the C++ API, you'll work with FileWriter alongside Summary and Event protobuf objects. Here's a straightforward walkthrough:

  • Include necessary headers:

    #include "tensorflow/core/framework/summary.pb.h"
    #include "tensorflow/core/util/event.pb.h"
    #include "tensorflow/core/platform/env.h"
    #include "tensorflow/core/graph/graph_def.h"
    
  • Initialize the FileWriter:
    Specify the directory where TensorBoard will pull logs from—this writer will create the directory automatically if it doesn't exist.

    std::string log_dir = "./tensorboard_logs";
    std::unique_ptr<tensorflow::FileWriter> writer(
        tensorflow::FileWriter::New(log_dir, tensorflow::Env::Default()).release());
    
  • Create and write a Summary entry:
    Let's use a scalar value (like training loss) as an example. Build a Summary::Value object, attach it to a Summary proto, wrap that in an Event, and write it to disk:

    int step = 0;
    float loss = 0.45f;
    
    // Build scalar summary
    tensorflow::Summary summary;
    auto* value = summary.add_value();
    value->set_tag("training/loss"); // This is the name TensorBoard will display
    value->set_simple_value(loss);
    
    // Wrap in Event and write
    tensorflow::Event event;
    event.set_step(step);
    event.set_wall_time(tensorflow::Env::Default()->NowSeconds());
    *event.mutable_summary() = summary;
    
    writer->WriteEvent(event);
    
  • Flush and clean up:
    Make sure to flush the writer to ensure all events are saved to disk, then close it when you're done:

    writer->Flush();
    writer->Close();
    

After running this code, start TensorBoard with tensorboard --logdir=./tensorboard_logs and you'll see the "training/loss" scalar in the Scalars tab.

2. Getting tensor names to write Summaries for TensorBoard

The FileWriter itself doesn't have tied tensor names—you define the display names (via the tag in Summary::Value) when creating your summary entries. That said, if you need to pull names of existing tensors in your graph (for consistency with your model's structure), here's how:

  • If you have direct access to the tensor object:
    Every tensorflow::Tensor has a name() method that returns its full identifier (e.g., "dense_1/BiasAdd:0"). You can use this as the tag in your summary:

    tensorflow::Tensor my_tensor(/* initialize your tensor here */);
    std::string tensor_name = my_tensor.name();
    
    // Use this name as the summary tag
    auto* value = summary.add_value();
    value->set_tag("model_tensors/" + tensor_name);
    // Populate the appropriate field (e.g., histogram_value for histograms)
    
  • If you need to extract tensor names from a GraphDef:
    If you're working with a pre-built graph (like a saved model), iterate over the graph's nodes to get their output tensor names:

    tensorflow::GraphDef graph_def;
    // Load your graph into graph_def (e.g., from a .pb file)
    
    for (const auto& node : graph_def.node()) {
        // Each node's outputs follow the format "<node_name>:<output_index>"
        for (int i = 0; i < node.output_size(); ++i) {
            std::string tensor_name = node.name() + ":" + std::to_string(i);
            std::cout << "Tensor name: " << tensor_name << std::endl;
            // Use this name to create summaries for these tensors
        }
    }
    

When logging non-scalar tensor data (like histograms or images), populate the corresponding field in Summary::Value (e.g., histogram_value() or image_value()) instead of simple_value().


内容的提问来源于stack exchange,提问作者Jianfeng Luo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:32:15