195 行
9.2 KiB
Markdown
195 行
9.2 KiB
Markdown
# Article-24337-Working-With-ONNX-Models-In-MQL5-Part-3
|
|
|
|
This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.
|
|
|
|

|
|
|
|
## Overview
|
|
|
|
This repository is derived from the MQL5 article *Working with ONNX Models in MQL5 (Part 3): Drawing the Neurons and the Weights Between Them)*. The article extends an ONNX model viewer for MetaTrader 5 by moving from layer-level graph visualization to neuron-level rendering of dense layers and their weight matrices.
|
|
|
|
The main focus is on interpreting stored ONNX weight tensors as connection maps between neuron rows, handling matrix orientation correctly, drawing weighted links with sign- and magnitude-based styling, and allowing the user to switch between graph view and neuron view. As an article-derived project, this repository serves as a reference for studying the described architecture and, where attached files are available, reusing the provided implementation.
|
|
|
|
## Original Article
|
|
|
|
- **Article ID:** 24337
|
|
- **Title:** Working with ONNX Models in MQL5 (Part 3): Drawing the Neurons and the Weights Between Them
|
|
- **Author:** Allan Munene Mutiiria
|
|
- **Publication date:** Not available
|
|
- **Categories:** Expert Advisors, Statistics
|
|
- **Article URL:** https://www.mql5.com/en/articles/24337
|
|
- **Author URL:** https://www.mql5.com/en/users/29210372
|
|
|
|
## Repository Purpose
|
|
|
|
This repository exists to preserve and organize the technical ideas presented in the article as a reusable MQL5 reference project.
|
|
|
|
A reader can use it to learn how to:
|
|
|
|
- inspect ONNX dense-layer weights inside MetaTrader 5,
|
|
- map weight matrices to neuron-to-neuron connections,
|
|
- distinguish graph structure from neuron-level structure,
|
|
- handle transposed ONNX weight storage using operator attributes,
|
|
- build chart-based visualizations with pan, zoom, hover, and view switching,
|
|
- design honest fallback behavior when a model cannot be represented as dense neuron pairs.
|
|
|
|
## Key Concepts
|
|
|
|
- ONNX model visualization
|
|
- Expert Advisors
|
|
- Statistics
|
|
- Dense neural layers
|
|
- Weight matrices
|
|
- Neuron layout construction
|
|
- Graph view vs neuron view
|
|
- MQL5 chart rendering
|
|
- ONNX attribute handling
|
|
- MetaTrader 5 UI interaction
|
|
|
|
## Algorithm / Architecture Summary
|
|
|
|
1. **Start from the previously parsed ONNX model**
|
|
- The article assumes a model parser already exists from earlier parts.
|
|
- Nodes, tensors, attributes, emitted values, and layer relationships are read from the ONNX model.
|
|
|
|
2. **Represent neuron rows separately from graph nodes**
|
|
- A row structure is used to store:
|
|
- neuron count,
|
|
- label,
|
|
- position,
|
|
- width,
|
|
- rank/depth in the arrangement.
|
|
- A row does not necessarily equal one layer, because some operations modify values in place without changing row width.
|
|
|
|
3. **Represent connection runs between rows**
|
|
- A second structure records the connection relationship between two rows.
|
|
- It stores source row, target row, the tensor index holding weights, target offset, and whether the stored matrix must be treated as transposed.
|
|
|
|
4. **Classify supported operators**
|
|
- Dense operators are explicitly treated as those that carry a 2D weight matrix between neuron rows.
|
|
- Unary operators are treated as operations that transform values on an existing row without creating a new row.
|
|
- Unsupported or ambiguous operators are declined instead of guessed.
|
|
|
|
5. **Build neuron rows from dense layers**
|
|
- When a dense operator is encountered, the code:
|
|
- reads the weight tensor,
|
|
- checks whether the `transB` attribute indicates transposed storage,
|
|
- determines the produced neuron count from the appropriate tensor dimension,
|
|
- opens a new neuron row,
|
|
- records a connection run from the feeding row to the new row.
|
|
|
|
6. **Fold activations and similar operators onto existing rows**
|
|
- Unary, binary, and shape-only operations do not create new neuron rows.
|
|
- Their emitted values are mapped back to the same row.
|
|
- Labels are extended so the row reflects the applied operations, such as a dense operator followed by an activation.
|
|
|
|
7. **Reject unsupported neuron-view cases honestly**
|
|
- If a dense layer obtains weights dynamically rather than from a stored tensor, the article's logic declines neuron rendering.
|
|
- Models composed of operators such as convolutions, recurrent structures, or attention are also not forced into an incorrect dense-connection visualization.
|
|
|
|
8. **Compute rank/depth of each row**
|
|
- The arrangement is refined by repeatedly pushing each target row one level deeper than the row feeding it.
|
|
- This produces a depth-based layout similar in spirit to the graph layout from the previous article.
|
|
|
|
9. **Measure and place rows**
|
|
- Each row width is calculated from its neuron count and spacing settings.
|
|
- Rows sharing the same rank are arranged side by side.
|
|
- The widest rank is used as the centering reference so the full arrangement stays visually balanced.
|
|
|
|
10. **Draw weighted connections**
|
|
- For each pair of source and target neurons, the corresponding weight is fetched from the tensor.
|
|
- The indexing rule depends on whether the matrix is stored transposed.
|
|
- Line color reflects sign:
|
|
- positive weights use one color,
|
|
- negative weights use another.
|
|
- Line opacity is scaled relative to the largest absolute weight in the tensor.
|
|
|
|
11. **Apply practical visualization limits**
|
|
- The article notes that tensors are capped when read, with the strongest structure preserved first.
|
|
- This means the visualization may show a reduced but meaningful subset of connections for very large matrices rather than every single weight.
|
|
|
|
12. **Draw neuron rows and labels**
|
|
- Each neuron is rendered as a circle.
|
|
- Labels are centered above their own row and include the operation label and neuron count.
|
|
|
|
13. **Provide explicit failure messages**
|
|
- If the neuron arrangement cannot be built, the panel displays a reason instead of leaving the canvas blank.
|
|
|
|
14. **Support graph/neuron view switching**
|
|
- A footer control toggles between the existing graph view and the new neuron view.
|
|
- Sizing, fitting, scrolling, hover text, and tooltips all depend on the active view.
|
|
- If neuron view is unavailable, the control remains dimmed and explains why.
|
|
|
|
## Mentioned or Attached Files
|
|
|
|
### Explicitly attached files
|
|
|
|
The processed article input explicitly lists these attachments:
|
|
|
|
1. **make_model.py** — Python script
|
|
Builds the sample classification network and saves it as `model.onnx` under `MQL5\Files\ONNX Model Viewer Part 3\`
|
|
|
|
2. **OMV Protobuf.mqh** — Include file
|
|
Defines protobuf wire-type constants and byte-decoding logic for ONNX parsing
|
|
|
|
3. **OMV Model.mqh** — Include file
|
|
Defines node, tensor, and port structures and implements model parsing logic
|
|
|
|
4. **OMV Canvas.mqh** — Include file
|
|
Provides the drawing surface and rendering helpers
|
|
|
|
5. **OMV Panel.mqh** — Include file
|
|
Arranges graph and neuron views, draws both, and handles mouse/UI interaction
|
|
|
|
6. **ONNX Model Viewer Part 3.mq5** — Expert Advisor
|
|
Main program entry point that initializes the panel and forwards chart events
|
|
|
|
7. **MQL5.zip** — Archive
|
|
Ready-to-extract archive containing the MQL5 folder structure and project files
|
|
|
|
### ZIP attachment
|
|
|
|
- An attached ZIP archive is available: https://www.mql5.com/en/articles/download/24337_270086.zip?s=d0028f3bb4f47dfde7f5dfc917902de7c95a5fd0d6ed782b6f7af0bed76d2f6d&t=1791568242
|
|
|
|
### Files mentioned in text
|
|
|
|
- `model.onnx` is mentioned as the generated ONNX model file produced by the Python script.
|
|
|
|
## Statistics
|
|
|
|
- **Word count:** Not available
|
|
- **Reading time:** Not available
|
|
- **Image count:** Not available
|
|
- **Code block count:** Not available
|
|
- **File count:** Not available
|
|
|
|
## Tags
|
|
|
|
- mql5
|
|
- metatrader-5
|
|
- expert-advisors
|
|
- statistics
|
|
- onnx
|
|
- neural-networks
|
|
- model-visualization
|
|
- difficulty-advanced
|
|
|
|
## Difficulty
|
|
|
|
**Advanced**
|
|
|
|
Reason: the article combines ONNX model interpretation, tensor-orientation handling, custom layout construction, chart-based rendering, UI interaction, and careful support/rejection logic for multiple operator types.
|
|
|
|
## Limitations
|
|
|
|
- This repository is based on an article and should be treated as a reference or reconstruction unless the attached files are actually included here.
|
|
- Full functionality depends on the source files and archives actually attached or provided by the article author.
|
|
- The neuron view described in the article is intentionally limited to cases that can be represented honestly as dense neuron-to-neuron connections.
|
|
- The article explicitly notes that convolutions, recurrent layers, attention layers, and dynamically supplied dense weights are not suitable for this visualization approach.
|
|
- The visualization may not render every stored weight in very large tensors, because the article describes capped tensor retention and prioritization of stronger connections.
|
|
- The project should not be assumed production-ready unless validated independently in your own MetaTrader 5 environment.
|
|
|
|
## Reference
|
|
|
|
- Original article: https://www.mql5.com/en/articles/24337
|
|
- Author profile: https://www.mql5.com/en/users/29210372
|