Main Content

fxpopt および [HDL コードの生成] を使用したニューラル ネットワーク アルゴリズムの固定小数点への変換

この例では、関数 fxpopt とルックアップ テーブル オプティマイザーを使用して Simulink のニューラル ネットワーク回帰モデルを固定小数点に変換する方法を示します。

概要

Fixed-Point Designer は、設計を浮動小数点データ型から固定小数点データ型に変換できる固定小数点ツールを使用したワークフローを提供します。関数 fxpopt では、指定されたシステム動作の制約に基づいてモデル内のデータ型が最適化されます。詳細については、ドキュメンテーション リンク (https://www.mathworks.com/help/fixedpoint/ref/fxpopt.html) を参照してください。ルックアップ テーブル オプティマイザーでは、explog2 など境界がない関数に対してメモリ効率が高いルックアップ テーブル置換を生成します。この例では、これらのツールを使用して、学習済みの浮動小数点ニューラル ネットワークの回帰モデルを変換し、組み込み効率の良い固定小数点データ型を使用します。

データとニューラル ネットワークの学習

engine_dataset には、エンジンの燃料比と速度、およびそのトルクと排ガスの関係を表すデータが含まれています。

% Use the function fitting tool (nftool) from Deep Learning Toolbox (TM) to
% train a neural network to estimate torque and gas emissions of an engine
% given the fuel rate and speed. Use the following commands to train
% the neural network.
load engine_dataset;
x = engineInputs;
t = engineTargets;
net = fitnet(10);
net = train(net,x,t);
view(net)

ネットワークのビューを閉じます。

nnet.guis.closeAllViews();

固定小数点の変換のためのモデルの準備

ネットワークに学習させたら、Deep Learning Toolbox™ から関数 gensim を使用して Simulink モデルを生成します。

[sysName, netName] = gensim(net, 'Name', 'mTrainedNN');

関数 gensim で生成されたモデルには、重みとバイアスを学習したニューラル ネットワークが含まれます。この生成されたモデルを固定小数点の変換用に準備するためには、ベストプラクティス ガイドラインの準備ステップに従います。https://www.mathworks.com/help/fixedpoint/ug/best-practices-for-using-the-fixed-point-tool-to-propose-data-types-for-your-simulink-model.html

これらの原則を適用した後に、学習済みニューラル ネットワークをさらに変更して、ネットワークの出力で信号のログを有効にし、入力スティミュラスと検証ブロックを追加します。

モデルを開いて検証します。

model = 'ex_fxpdemo_neuralnet_regression';
system_under_design = [model '/Function Fitting Neural Network'];
baseline_output = [model '/yarr'];
open_system(model);

% Set up model for HDL code generation
hdlsetup(model);
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','SingleTaskRateTransMsg')">SingleTaskRateTransMsg</a> value is set from 'none' to 'error' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','SingleTaskRateTransMsg', 'none')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','Solver')">Solver</a> value is set from 'FixedStepAuto' to 'FixedStepDiscrete' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','Solver', 'FixedStepAuto')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','AlgebraicLoopMsg')">AlgebraicLoopMsg</a> value is set from 'warning' to 'error' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','AlgebraicLoopMsg', 'warning')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','BlockReduction')">BlockReduction</a> value is set from 'on' to 'off' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','BlockReduction', 'on')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','ConditionallyExecuteInputs')">ConditionallyExecuteInputs</a> value is set from 'on' to 'off' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','ConditionallyExecuteInputs', 'on')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','DefaultParameterBehavior')">DefaultParameterBehavior</a> value is set from 'Tunable' to 'Inlined' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','DefaultParameterBehavior', 'Tunable')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','ProdHWDeviceType')">ProdHWDeviceType</a> value is set from 'Intel->x86-64 (Windows64)' to 'ASIC/FPGA->ASIC/FPGA' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','ProdHWDeviceType', 'Intel->x86-64 (Windows64)')">revert</a>).
### <a href="matlab:configset.internal.open('ex_fxpdemo_neuralnet_regression','InheritOutputTypeSmallerThanSingle')">InheritOutputTypeSmallerThanSingle</a> value is set from 'off' to 'on' (<a href="matlab:set_param('ex_fxpdemo_neuralnet_regression','InheritOutputTypeSmallerThanSingle', 'off')">revert</a>).
### The listed configuration parameter values are modified as a part of hdlsetup. Please refer to <a href="matlab:helpview(fullfile(docroot, 'hdlcoder', 'helptargets.map'), 'msg_hdlsetup_function')">hdlsetup</a> document for best practices on model settings.

モデルをシミュレーションして、倍精度浮動小数点データ型を使用するときのモデルのパフォーマンスを確認します。

loggingInfo = get_param(model, 'DataLoggingOverride');
sim_out = sim(model, 'SaveFormat', 'Dataset');

plotRegression(sim_out, baseline_output, system_under_design, 'Regression before conversion');

固定小数点の変換に関するシステム動作の制約の定義

opts = fxpOptimizationOptions();
opts.addTolerance(system_under_design, 1, 'RelTol', 0.05);
opts.addTolerance(system_under_design, 1, 'AbsTol', 50)
opts.AllowableWordLengths = 8:32;

データ型の最適化

関数 fxpopt を使用して設計対象のシステムのデータ型を最適化し、ソリューションを確認します。system_under_design 内のオブジェクトの範囲と opts で指定された語長および許容誤差の制約がソフトウェアで解析され、総ビット幅を最小限に抑えながら異種混合のデータ型がモデルに適用されます。

solution  = fxpopt(model, system_under_design, opts);
best_solution = solution.explore;
	+ Starting data type optimization...
	+ Checking for unsupported constructs.
		- The paths below have constructs that do not support fixed-point data types. These constructs will be surrounded with Data Type Conversion blocks.
    'ex_fxpdemo_neuralnet_regression/Function Fitting Neural Network/Layer 1/tansig/tanh'

	+ Preprocessing
	+ Modeling the optimization problem
		- Constructing decision variables
	+ Running the optimization solver
		- Evaluating new solution: cost 515, does not meet the behavioral constraints.
		- Evaluating new solution: cost 577, does not meet the behavioral constraints.
		- Evaluating new solution: cost 639, does not meet the behavioral constraints.
		- Evaluating new solution: cost 701, does not meet the behavioral constraints.
		- Evaluating new solution: cost 763, does not meet the behavioral constraints.
		- Evaluating new solution: cost 825, does not meet the behavioral constraints.
		- Evaluating new solution: cost 887, does not meet the behavioral constraints.
		- Evaluating new solution: cost 949, meets the behavioral constraints.
		- Updated best found solution, cost: 949
		- Evaluating new solution: cost 945, meets the behavioral constraints.
		- Updated best found solution, cost: 945
		- Evaluating new solution: cost 944, meets the behavioral constraints.
		- Updated best found solution, cost: 944
		- Evaluating new solution: cost 943, meets the behavioral constraints.
		- Updated best found solution, cost: 943
		- Evaluating new solution: cost 942, meets the behavioral constraints.
		- Updated best found solution, cost: 942
		- Evaluating new solution: cost 941, meets the behavioral constraints.
		- Updated best found solution, cost: 941
		- Evaluating new solution: cost 940, meets the behavioral constraints.
		- Updated best found solution, cost: 940
		- Evaluating new solution: cost 939, meets the behavioral constraints.
		- Updated best found solution, cost: 939
		- Evaluating new solution: cost 938, meets the behavioral constraints.
		- Updated best found solution, cost: 938
		- Evaluating new solution: cost 937, meets the behavioral constraints.
		- Updated best found solution, cost: 937
		- Evaluating new solution: cost 936, meets the behavioral constraints.
		- Updated best found solution, cost: 936
		- Evaluating new solution: cost 926, meets the behavioral constraints.
		- Updated best found solution, cost: 926
		- Evaluating new solution: cost 925, meets the behavioral constraints.
		- Updated best found solution, cost: 925
		- Evaluating new solution: cost 924, meets the behavioral constraints.
		- Updated best found solution, cost: 924
		- Evaluating new solution: cost 923, meets the behavioral constraints.
		- Updated best found solution, cost: 923
		- Evaluating new solution: cost 922, meets the behavioral constraints.
		- Updated best found solution, cost: 922
		- Evaluating new solution: cost 917, meets the behavioral constraints.
		- Updated best found solution, cost: 917
		- Evaluating new solution: cost 916, meets the behavioral constraints.
		- Updated best found solution, cost: 916
		- Evaluating new solution: cost 914, meets the behavioral constraints.
		- Updated best found solution, cost: 914
		- Evaluating new solution: cost 909, meets the behavioral constraints.
		- Updated best found solution, cost: 909
		- Evaluating new solution: cost 908, meets the behavioral constraints.
		- Updated best found solution, cost: 908
		- Evaluating new solution: cost 906, meets the behavioral constraints.
		- Updated best found solution, cost: 906
		- Evaluating new solution: cost 898, meets the behavioral constraints.
		- Updated best found solution, cost: 898
		- Evaluating new solution: cost 897, meets the behavioral constraints.
		- Updated best found solution, cost: 897
		- Evaluating new solution: cost 893, does not meet the behavioral constraints.
		- Evaluating new solution: cost 896, meets the behavioral constraints.
		- Updated best found solution, cost: 896
		- Evaluating new solution: cost 895, meets the behavioral constraints.
		- Updated best found solution, cost: 895
		- Evaluating new solution: cost 894, meets the behavioral constraints.
		- Updated best found solution, cost: 894
		- Evaluating new solution: cost 893, meets the behavioral constraints.
		- Updated best found solution, cost: 893
		- Evaluating new solution: cost 892, meets the behavioral constraints.
		- Updated best found solution, cost: 892
		- Evaluating new solution: cost 891, meets the behavioral constraints.
		- Updated best found solution, cost: 891
		- Evaluating new solution: cost 890, meets the behavioral constraints.
		- Updated best found solution, cost: 890
		- Evaluating new solution: cost 889, meets the behavioral constraints.
		- Updated best found solution, cost: 889
		- Evaluating new solution: cost 888, meets the behavioral constraints.
		- Updated best found solution, cost: 888
		- Evaluating new solution: cost 878, meets the behavioral constraints.
		- Updated best found solution, cost: 878
		- Evaluating new solution: cost 877, meets the behavioral constraints.
		- Updated best found solution, cost: 877
		- Evaluating new solution: cost 876, meets the behavioral constraints.
		- Updated best found solution, cost: 876
		- Evaluating new solution: cost 875, meets the behavioral constraints.
		- Updated best found solution, cost: 875
		- Evaluating new solution: cost 874, meets the behavioral constraints.
		- Updated best found solution, cost: 874
		- Evaluating new solution: cost 869, meets the behavioral constraints.
		- Updated best found solution, cost: 869
		- Evaluating new solution: cost 868, does not meet the behavioral constraints.
		- Evaluating new solution: cost 867, meets the behavioral constraints.
		- Updated best found solution, cost: 867
		- Evaluating new solution: cost 862, does not meet the behavioral constraints.
		- Evaluating new solution: cost 866, does not meet the behavioral constraints.
		- Evaluating new solution: cost 865, does not meet the behavioral constraints.
	+ Optimization has finished.
		- Neighborhood search complete.
		- Maximum number of iterations completed.
	+ Fixed-point implementation that satisfies the behavioral constraints found. The best found solution is applied on the model.
		- Total cost: 867
		- Maximum absolute difference: 49.714162
		- Use the explore method of the result to explore the implementation.

モデルをシミュレーションして変換後のモデルの精度を確認します。

set_param(model, 'DataLoggingOverride', loggingInfo);
Simulink.sdi.markSignalForStreaming([model '/yarr'], 1, 'on');
Simulink.sdi.markSignalForStreaming([model '/diff'], 1, 'on');
sim_out = sim(model, 'SaveFormat', 'Dataset');

固定小数点モデルの回帰精度をプロットします。

plotRegression(sim_out, baseline_output, system_under_design, 'Regression after conversion');

活性化関数と最適化されたルックアップ テーブルとの置き換え

レイヤ 1 の活性化関数 Tanh をルックアップ テーブルまたは CORDIC 実装に置き換えて、固定小数点のコード生成を効率化することができます。この例では、ルックアップ テーブル オプティマイザーを使用して、tanh を置き換えるルックアップ テーブルを取得します。実行速度を高速化するために EvenPow2Spacing を使用します。詳細については、https://www.mathworks.com/help/fixedpoint/ref/functionapproximation.options-class.html を参照してください。

block_path = [system_under_design '/Layer 1/tansig'];
p = FunctionApproximation.Problem(block_path);
p.Options.WordLengths = 8:32;
p.Options.BreakpointSpecification = 'EvenPow2Spacing';
solution  = p.solve;
solution.replaceWithApproximate;
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             44 |        0 |          2 |              14 |            8 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|   1 |           4124 |        1 |        512 |              14 |            8 |         EvenPow2Spacing |     7.812500e-03, 7.812500e-03 |
|   2 |           4114 |        1 |        512 |               9 |            8 |         EvenPow2Spacing |     7.812500e-03, 7.812500e-03 |
|   3 |           2076 |        0 |        256 |              14 |            8 |         EvenPow2Spacing |     7.812500e-03, 1.831055e-02 |
|   4 |           2064 |        0 |        256 |               8 |            8 |         EvenPow2Spacing |     7.812500e-03, 1.831055e-02 |
|   5 |             46 |        0 |          2 |              14 |            9 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|   6 |           2332 |        0 |        256 |              14 |            9 |         EvenPow2Spacing |     7.812500e-03, 2.563477e-02 |
|   7 |           2320 |        0 |        256 |               8 |            9 |         EvenPow2Spacing |     7.812500e-03, 2.563477e-02 |
|   8 |             48 |        0 |          2 |              14 |           10 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|   9 |           2588 |        0 |        256 |              14 |           10 |         EvenPow2Spacing |     7.812500e-03, 2.416992e-02 |
|  10 |           2576 |        0 |        256 |               8 |           10 |         EvenPow2Spacing |     7.812500e-03, 2.416992e-02 |
|  11 |             50 |        0 |          2 |              14 |           11 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|  12 |           2844 |        0 |        256 |              14 |           11 |         EvenPow2Spacing |     7.812500e-03, 2.319336e-02 |
|  13 |           2832 |        0 |        256 |               8 |           11 |         EvenPow2Spacing |     7.812500e-03, 2.319336e-02 |
|  14 |             52 |        0 |          2 |              14 |           12 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|  15 |           3100 |        0 |        256 |              14 |           12 |         EvenPow2Spacing |     7.812500e-03, 2.319336e-02 |
|  16 |           3088 |        0 |        256 |               8 |           12 |         EvenPow2Spacing |     7.812500e-03, 2.319336e-02 |
|  17 |             54 |        0 |          2 |              14 |           13 |         EvenPow2Spacing |     7.812500e-03, 1.000000e+00 |
|  18 |           3356 |        0 |        256 |              14 |           13 |         EvenPow2Spacing |     7.812500e-03, 2.343750e-02 |
|  19 |           3344 |        0 |        256 |               8 |           13 |         EvenPow2Spacing |     7.812500e-03, 2.343750e-02 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|   2 |           4114 |        1 |        512 |               9 |            8 |         EvenPow2Spacing |     7.812500e-03, 7.812500e-03 |

関数の置き換え後にモデルの精度を確認します。

sim_out = sim(model, 'SaveFormat', 'Dataset');

関数の置き換え後に回帰精度をプロットします。

plotRegression(sim_out, baseline_output, system_under_design, 'Regression after function replacement');

HDL コードとテスト ベンチの生成

HDL コードを生成するには、HDL Coder™ のライセンスが必要です。

HDL コードとテスト ベンチを生成するモデルを選択します。

systemname = 'ex_fxpdemo_neuralnet_regression/Function Fitting Neural Network';

生成されたファイルには一時ディレクトリを使用します。

workingdir = tempname;

次のコマンドを実行して、HDL コード生成の互換性をチェックします。

checkhdl(systemname,'TargetDirectory',workingdir);
### Begin compilation of the model 'ex_fxpdemo_neuralnet_regression'...
### Running HDL checks on the model 'ex_fxpdemo_neuralnet_regression'.
### Creating HDL Code Generation Check Report file://C:\TEMP\tpdf3381d4_776f_4252_91eb_38f30ac1fbbd\ex_fxpdemo_neuralnet_regression\Function_Fitting_Neural_Network_report.html
### HDL check for 'ex_fxpdemo_neuralnet_regression' complete with 0 errors, 0 warnings, and 0 messages.

次のコマンドを実行して HDL コードを生成します。

makehdl(systemname,'TargetDirectory',workingdir);

次のコマンドを使用してテスト ベンチを生成します。

makehdltb(systemname,'TargetDirectory',workingdir);