.. _tabular-exporting-models: ================ Exporting Models ================ This page covers how to export your NeurEco Tabular models for deployment in various formats and environments. .. contents:: On this page :local: :depth: 2 Export Formats Overview ======================= NeurEco Tabular models can be exported to multiple formats: **Native Format** .. list-table:: :header-rows: 1 :widths: 15 15 70 * - Format - Extension - Description * - NeurEco - ``.ednn`` - Native format for continued work in NeurEco or Python API **Deployment Formats** (require *embed* license) .. list-table:: :header-rows: 1 :widths: 15 15 70 * - Format - Extension - Best For * - FMU - ``.fmu`` - Simulation environments (Simulink, Dymola, OpenModelica, etc.) * - ONNX - ``.onnx`` - Cross-platform deployment, ML framework integration * - C Header - ``.h`` - Embedded systems, microcontrollers, high-performance applications * - VBA - ``.bas`` - Microsoft Excel integration Export Workflow =============== .. tabs:: .. tab:: GUI **1. Access the Export panel** Switch to the **Export** tab in NeurEco GUI. **2. Select checkpoint (optional)** Use the checkpoint slider to choose which model to export (default: final model). **3. Choose export format** Click the button for your desired format: - **Save** — NeurEco native format (.ednn) - **FMU** — Functional Mock-up Unit - **ONNX** — Open Neural Network Exchange - **C** — C header file - **VBA** — Visual Basic for Applications **4. Select precision (if applicable)** For some formats, choose the numerical precision: - **float64** — 64-bit (highest accuracy) - **float32** — 32-bit (good balance) - **float16** — 16-bit (smallest size, ONNX only) **5. Specify output path** Choose the destination filename and location. .. tab:: Python API **Save in native format:** .. code-block:: python model.save("./model/regression.ednn") **Export to FMU:** .. code-block:: python model.export_fmu("./model/regression.fmu") **Export to ONNX:** .. code-block:: python model.export_onnx("./model/regression.onnx", precision="float32") **Export to C header:** .. code-block:: python model.export_c("./model/regression.h", precision="float64") **Export to VBA:** .. code-block:: python model.export_vba("./model/regression.bas", precision="float32") .. tab:: CLI **Export commands:** .. code-block:: shell # Export to C neureco Regression exportC ./model/regression.ednn ./model/regression.h float64 # Export to ONNX neureco Regression exportONNX ./model/regression.ednn ./model/regression.onnx float32 # Export to VBA neureco Regression exportVBA ./model/regression.ednn ./model/regression.bas float32 # Export to FMU (last argument: platform flag, 3 = Windows and Linux) neureco Regression exportFMU ./model/regression.ednn ./model/regression.fmu 3 Format Details ============== Native NeurEco Format (.ednn) ----------------------------- The native format preserves all model information and is used for: - Continued work in NeurEco - Python API integration - Re-exporting to other formats later .. code-block:: python # Save model.save("model.ednn") # Load later model = Tabular.Regressor() model.load("model.ednn") FMU (Functional Mock-up Unit) ----------------------------- FMU is a standard for exchanging simulation models. Use it with: - MATLAB/Simulink - Dymola - OpenModelica - AMESim - Other FMI-compatible tools The exported FMU contains the neural network as a self-contained simulation component. **Usage in Simulink:** 1. Export model as FMU 2. In Simulink, add an FMU Import block 3. Browse and select the .fmu file 4. Connect inputs and outputs ONNX (Open Neural Network Exchange) ----------------------------------- ONNX is an open format for ML models, enabling interoperability with: - TensorFlow - PyTorch - ONNX Runtime - Many other frameworks **Using the ONNX model** (with ONNX Runtime; install it with ``pip install onnxruntime`` if needed): .. code-block:: python import onnxruntime as ort import numpy as np # Load the ONNX model session = ort.InferenceSession("model.onnx") # Get input name input_name = session.get_inputs()[0].name # Run inference x_test = np.array([[1.0, 2.0, 3.0]], dtype=np.float32) result = session.run(None, {input_name: x_test}) print(result[0]) C Header File ------------- The C export produces a standalone header file containing: - Network weights and biases - Inference function - No dependency beyond the C standard library (``math.h``) The header defines one function, named ``neureco_`` followed by the name of the header file without its extension: exporting to ``regression.h`` gives ``neureco_regression``. Characters other than letters, digits and ``_``, such as ``-`` or spaces, become ``_``, and a name that starts with a digit gets one more ``_`` in front (``2024.h`` gives ``neureco__2024``). Keep the file name ASCII. .. code-block:: c int neureco_regression(const double *inputData, double *outputData); The function evaluates one sample. ``inputData`` holds the model inputs, in the order of the training columns, and ``outputData`` receives **all** the model outputs: for a field output, every point of the field (2048 values for the AirfRANS tutorial). The arrays are ``double`` for a ``float64`` export and ``float`` for a ``float32`` export. The function returns ``0``. **Using the C header** (a model with 5 inputs and 3 outputs, exported to ``regression.h`` with ``float64``): .. code-block:: c #include #include "regression.h" #define N_INPUTS 5 #define N_OUTPUTS 3 int main(void) { double inputs[N_INPUTS] = {1.0, 2.0, 3.0, 4.0, 5.0}; double outputs[N_OUTPUTS]; int i; neureco_regression(inputs, outputs); for (i = 0; i < N_OUTPUTS; i++) printf("output %d: %f\n", i, outputs[i]); return 0; } The header contains the code of the function, not only its declaration: include it in one source file only. It uses the C math library, so on Linux link with ``-lm`` (for example ``gcc main.c -o main -lm``). The function keeps its working arrays in static memory: do not call it from several threads at the same time. This is ideal for: - Microcontrollers (Arduino, STM32, etc.) - Embedded systems - Real-time applications - Environments without Python VBA (Visual Basic for Applications) ----------------------------------- VBA export enables using NeurEco models directly in Microsoft Excel. **Using in Excel:** 1. Export model to .bas file 2. Open Excel, press Alt+F11 to open VBA editor 3. Import the .bas module 4. Use the function in your spreadsheet. As in C, it is named ``neureco_`` followed by the file name without its extension, and it takes one argument: a range holding the inputs in one row. For ``regression.bas`` and a model with 5 inputs: .. code-block:: text =neureco_regression(A1:E1) With one output, the cell shows the prediction. With several outputs, the function returns all of them in one text value, separated by ``;``. A cell holds at most 32,767 characters, so this does not suit large fields. Precision Options ================= When exporting, you can choose numerical precision: .. list-table:: :header-rows: 1 :widths: 15 25 60 * - Precision - Bits - Use Case * - ``float64`` - 64-bit - Maximum accuracy, scientific computing * - ``float32`` - 32-bit - Good balance of accuracy and size, most applications * - ``float16`` - 16-bit - ONNX only: memory-constrained devices, when slight accuracy loss is acceptable The C and VBA exports accept ``float64`` and ``float32`` only. **Recommendation:** Start with ``float32`` for most applications. Use ``float64`` only if you observe precision issues, and ``float16`` (ONNX) only when memory is critical. .. note:: The legacy names ``float`` and ``double`` remain accepted as aliases for ``float32`` and ``float64`` in the Python API, the CLI and configuration files, so existing scripts keep working. Exporting Compression Models ============================ Compression models produce three separate files: .. code-block:: python # During build, specify all three output paths config = Tabular.CompressorBuildConfig( input_data=x_train, write_model_to="./model/complete.ednn", # Full pipeline write_compression_model_to="./model/encoder.ednn", # Encoder only write_decompression_model_to="./model/decoder.ednn", # Decoder only ) Each can be exported independently: .. code-block:: python # Export encoder for embedded device encoder = Tabular.Compressor() encoder.load("./model/encoder.ednn") encoder.export_c("./embedded/encoder.h", precision="float32") # Export decoder for server decoder = Tabular.Compressor() decoder.load("./model/decoder.ednn") decoder.export_onnx("./server/decoder.onnx", precision="float32") Exporting from Checkpoints ========================== You can export any model from the checkpoint history: .. tabs:: .. tab:: GUI Use the checkpoint slider to select the model, then export normally. .. tab:: Python API .. code-block:: python # Load a specific checkpoint model (index 5 = "Checkpoint 6" in the log and the GUI) model = Tabular.Regressor() model.load_model_from_checkpoint("./model/regression.checkpoint", model_index=5) # Export it model.export_onnx("./model/checkpoint_6.onnx", precision="float32") Best Practices ============== 1. **Choose the right format:** Match the export format to your deployment environment 2. **Test after export:** Verify that the exported model produces the same results as the original 3. **Consider precision tradeoffs:** Lower precision saves memory but may affect accuracy 4. **Keep the native format:** Always save in .ednn format as a backup 5. **Document model inputs/outputs:** Keep track of input feature order and scaling Verifying Exported Models ========================= After exporting, verify the model works correctly: .. code-block:: python import onnxruntime as ort import numpy as np # Original NeurEco evaluation original_output = model.evaluate(x_test) # ONNX evaluation session = ort.InferenceSession("model.onnx") input_name = session.get_inputs()[0].name onnx_output = session.run(None, {input_name: x_test.astype(np.float32)})[0] # Compare max_diff = np.max(np.abs(original_output - onnx_output)) print(f"Maximum difference: {max_diff}") if max_diff < 1e-5: print("Export verification: PASSED") else: print("Export verification: Check precision settings") Next Steps ========== - :ref:`Convert to Keras or PyTorch ` for deep learning frameworks - :ref:`Use advanced features ` for model analysis