How do I save a trained deep learning network as plain text?

3 visualizaciones (últimos 30 días)
I have trained a neural network and would like to save it as a text file. How do I save a trained deep learning network as plain text? I am willing to write a script to read and write layers to text but would prefer to use an existing solution if one is available.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 24 de En. de 2025
Editada: MathWorks Support Team el 4 de Feb. de 2025
There is no built-in utility for saving a trained deep learning model to text and back in MATLAB R2020a. However, there are some features of the Deep Network Designer and the function “matlab.io.saveVariablesToScript” that may expedite the process of writing an appropriate utility. The process to create ASCII files with these utilities is:
1. Open the Deep Network Designer by running “deepNetworkDesigner” in the MATLAB Command Window. Then click “From Workspace” on the landing page and select your trained model from the resulting menu to import it from the workspace.
2. This trained network can now be exported by selecting “Export” from the toolstrip and choosing “Generate Code with Initial Parameters.” Note that the “initial parameters” of a trained model are actually trained parameters. This operation will generate a live script in MATLAB containing network structure and a MAT-file with layer parameters. For more information on exporting networks from the Deep Network Designer, execute the following command in MATLAB R2020a:
>> web(fullfile(docroot, 'deeplearning/ug/generate-matlab-code-from-deep-network-designer.html'))
3. The live script can be saved as plain text by clicking “Save As” in the MATLAB toolstrip and selecting “MATLAB Code File (.m)” from the resultant menu.
4. The generated MAT-file is automatically saved and contains a struct of layer properties for each layer of the network. The MAT-file is a binary file. To save its contents as plain text, load the MAT-file into MATLAB with the “load” command or by double-clicking it in the “Current Folder” pane within MATLAB.
5. The workspace will be populated with a struct for each layer in the network. Each struct will likely be empty, contain n-D single arrays, or contain a categorical array with text elements. For example, the pretrained SqueezeNet network included with MATLAB yields a MAT-file with 68 structs. However, 40 of these structs are empty. Of the remaining structs, 19 contain 3-D and 4-D single arrays, and one struct contains a categorical array.
6. The function “matlab.io.saveVariablesToScript” can be used to generate a MATLAB Code File from variables in the workspace. This file will populate the workspace with property variables, much like a MAT-file, when it is run. The parameters ‘MaximumArraySize’ and ‘MaximumTextWidth’ should be maximized when calling “matlab.io.saveVariablesToScript” to accommodate large arrays. The function call may look like:
matlab.io.saveVariablesToScript('properties', 'MaximumArraySize', 10000, 'MaximumTextWidth', 256)
Still, some arrays may contain too many elements and some variables may be of unsupported types. Arrays that are too large (more than 10000 elements) may need to be partitioned and unsupported variables (categorical variables) may need to be converted to a different type before being saved with “matlab.io.saveVariablesToScript.” For additional information on “matlab.io.saveVariablesToScript,” run the following command in MATLAB R2020a:
>> web(fullfile(docroot, 'matlab/ref/matlab.io.savevariablestoscript.html'))
Please follow the link below to search for the required information regarding the current release:

Más respuestas (0)

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by