Dense Layer — Machine Learning/Neural Networks
Machine_Learning/Neural_Networks/Dense_Layer · 1 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Dense Layer
Machine Learning / Neural Networks
One fully connected layer, evaluated at inference:
y = act(W·u + b). This is what a trained torch.nn.Linear
or a Keras Dense becomes once training is over – the weights
stop being learnable and are simply numbers – so the block takes them as
matrices you paste in, and evaluates the layer in plain arithmetic.
Chain several to build a multi-layer perceptron: the output of one is the input of the next, exactly as the layers stack in the framework you trained in.
Ports
- u – the layer input, a column [m,1]. m must equal the number of COLUMNS of W, and it is checked rather than broadcast.
- Output – y, a column [p,1], where p is the number of ROWS of W. So the layer's shape is read off W alone, and the block resizes its output when you change W.
Parameters
- Weights – W, the [p,m] weight matrix, row per
output unit. This is
layer.weightin PyTorch andlayer.get_weights()[0].Tin Keras – note the transpose: Keras stores it [m,p], PyTorch [p,m], and this block wants PyTorch's orientation. - Bias – b, a [p,1] column, one per output unit.
Set it to a zero column of the right height for a layer trained with
bias=False. - Activation – applied elementwise to W·u + b:
- Linear – none at all, y = W·u + b. The right choice for a final regression layer, and for a layer whose activation you would rather place in its own block.
- ReLU – max(0, v). The usual hidden-layer choice.
- Tanh – squashes to (−1, 1).
- Sigmoid – 1 ÷ (1 + e−v), squashes to (0, 1); a single-unit layer with this is a binary classifier's head.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust,
C, C++, VHDL, Verilog, SystemVerilog and
PLC Structured Text. W and b are baked into the generated code as literals
at full setprecision(17), so the exported layer is bit-comparable
with the in-app run; there is no tunable parameter, because a weight is not
something to retune on the target.
The three HDL targets are simulation-only: they carry the layer
in real arithmetic and quantize only at the port boundary. Tanh and
Sigmoid are transcendental and have no place in a Q16.16 datapath, and a block
whose synthesizability depended on which activation you picked would be worse
than one that says plainly that none of the three is offered for synthesis.
Simulink bridge
None. Simulink's Deep Learning blocks take a trained network
object, not a set of matrices, and no config value can carry an object across
the bridge – so there is nothing this block could be mapped to that would
survive the trip. The bridge reports it rather than dropping it silently, and it
has no parity testbench, which is the documented consequence of
Support::None rather than a gap. Code export verification still
covers it across all ten languages.
Notes
- Algebraic and stateless: the output depends only on the current input, so the layer cannot break an algebraic loop.
- No state space, deliberately. The bias makes the map affine rather than linear, and three of the four activations are nonlinear, so a stored A/B/C/D would be true in at most one configuration. Model reduction reports the block as unmergeable, which is the honest answer.
- The layer is a column map: u is [m,1] and y is [p,1]. Feed a batch one sample at a time – a matrix input is reported rather than treated as a batch, since [p,m]×[m,n] would silently make the output [p,n] and change what the block means.
Code facts#
| Fact | Value |
|---|---|
| registered type | Machine_Learning/Neural_Networks/Dense_Layer |
| family | Machine_Learning/Neural_Networks |
| solver environment class | ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Dense_Layer |
| source | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Neural_Networks/Dense_Layer/ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Dense_Layer.cpp |
| header | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Neural_Networks/Dense_Layer/ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Dense_Layer.h |
| default size on canvas | 120 × 90 px |
| ports at insert | 1 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | u |
| 2 | out | ICoreDouble | y |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Weights | [0.5 -0.2; 0.1 0.4; -0.3 0.6] | — |
Bias | [0.1; -0.05; 0.2] | — |
Activation | Linear%~%ReLU%~%Tanh%~%Sigmoid~~ReLU | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): no Simulink equivalent that could carry the weights: its Deep Learning blocks take a trained network OBJECT rather than weight matrices, and no config value crosses the bridge as an object. Re-create the layer on the Simulink side and paste the same W and b in
Catalog contract: src/ICoreSDK/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The checker has a blind spot here — it could not resolve something (a grouped port bullet, a computed config name), which is reported and never counted as a pass. A reader has to settle it:
B0every stimulus in the sample errored — cross-checks skipped
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Dense Layer — one fully connected layer, evaluated at inference y = act(W*u + b) W [p,m], b [p,1], u [m,1], y [p,1]
The weights are CONFIG, not a file: a trained torch.nn.Linear / Keras Dense is just numbers once training is over, so the layer is plain arithmetic and all ten export targets carry it. See the header for why that decision is what makes the Machine_Learning family verifiable, and for why the block has no state space and no Simulink bridge.
The three HDL targets are SIMULATION-ONLY for the same reason Recursive IIR's are: Tanh and Sigmoid are transcendental, which does not belong in a Q16.16 datapath, so the body converts to
realat the port boundary, works there, and converts back. Linear and ReLU would be perfectly synthesizable, but a block whose export changes character with a combo value is worse than one that is honest about the whole of itself.
Sample results#
No stimulus produced a sampled output in this rig — Invalid input size at: ICore Blocks/Home/Dense Layer. That is a fact about the single-block rig, not a verdict on the block: an offline batch fit, a block whose output only appears at onSolverFinish, or one that needs a driven environment cannot be exercised alone.
Category unsampled · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60
Sample data: docs/generated/samples/Machine_Learning__Neural_Networks__Dense_Layer.json