SummaryWriter
Models & Types · AiDotNet.Logging
PyTorch-compatible SummaryWriter for TensorBoard logging.
Implements: IDisposable
For Beginners
This is your interface to TensorBoard visualization.
During training, you use this writer to record:
- Loss values at each step (add_scalar)
- Model weight distributions (add_histogram)
- Sample outputs or feature maps (add_image)
- Model structure (add_graph)
Then you can visualize all this in TensorBoard by running: tensorboard –logdir=your_log_directory
Example usage:
How It Works
This class provides an API similar to PyTorch’s torch.utils.tensorboard.SummaryWriter, making it easy to log training metrics, model weights, images, and more.
Constructors
| Constructor | Summary |
|---|---|
SummaryWriter(string logDir, string comment, int purgeStep, int maxQueue, int flushSecs, string filename) |
Creates a new SummaryWriter. |
Properties
| Property | Summary |
|---|---|
long DefaultStep { get; } |
Gets the current default step number. |
string LogDir { get; } |
Gets the log directory path. |
Methods
| Method | Summary |
|---|---|
void AddCustomScalar(string tag, float value, long? step) |
Adds a custom scalar with layout. |
void AddEmbedding(string tag, float[] embeddings, string[] metadata, float[] labelImg, long? step) |
Adds an embedding with optional metadata and labels. |
void AddHistogram(string tag, ReadOnlySpan<float> values, long? step, int bins) |
Adds a histogram from a span of values. |
void AddHistogram(string tag, float[] values, long? step, int bins) |
Adds a histogram from a span of values. |
void AddHistogram(string tag, float[] values, long? step, int bins) |
Adds a histogram from a span of values. |
void AddHparams(Dictionary<string, object> hparams, Dictionary<string, float> metrics, Dictionary<string, object[]> hparamDomainDiscrete) |
Adds hyperparameters and associated metrics. |
void AddImage(string tag, float[] imageData, long? step, string dataformats) |
Adds an image to the summary. |
void AddImageRaw(string tag, byte[] pixels, int height, int width, int channels, long? step) |
Adds an image from raw pixel data. |
void AddImages(string tag, float[] images, long? step, int nrow, int padding, bool normalize) |
Adds a grid of images. |
void AddPrCurve(string tag, int[] labels, float[] predictions, long? step, int numThresholds) |
Adds a PR curve for binary classification evaluation. |
void AddScalar(string tag, double value, long? step) |
Adds a scalar value (double precision). |
void AddScalar(string tag, float value, long? step) |
Adds a scalar value (double precision). |
void AddScalars(string mainTag, Dictionary<string, float> tagScalarDict, long? step) |
Adds multiple scalars under a main tag. |
void AddText(string tag, string text, long? step) |
Adds text to the summary. |
void Close() |
Closes the writer (alias for Dispose). |
void Dispose() |
Releases resources and closes the writer. |
void Flush() |
Flushes pending writes to disk. |
void LogTrainingStep(float loss, float? accuracy, float? learningRate, long? step) |
Logs training metrics at the current step. |
void LogValidationStep(float loss, float? accuracy, long? step) |
Logs validation metrics. |
void LogWeights(string layerName, float[] weights, float[] gradients, long? step) |
Logs model weight statistics. |