半精度データ型による数字分類
この例では、Simulink® の学習したニューラル ネットワーク分類モデルの結果を倍精度と半精度で比較します。モデルは、MNIST の手書き数字データセットのイメージを分類します。
最初に、モデルからデータを読み込み、テスト データセットのサイズを指定します。
load trainImage.mat
testSetMaxIdx = 10;
モデルを倍精度型でシミュレーションする
このモデルでは、numerictype
オブジェクトを使用してパラメーター、信号、ブロック出力のデータ型を指定します。倍精度データ型を使用してモデルをシミュレーションするには、ベース ワークスペースで numerictype
オブジェクを定義し、オブジェクトのデータ型を 'double'
に設定します。モデルのシミュレーションを実行します。
floatType = numerictype('double'); activationType = numerictype('double'); model = 'ex_imagerecog_half.slx'; open_system(model); sim(model);
倍精度シミュレーションの結果、100% の分類精度が得られます。
[c, ~] = confusion(ttestsubset(:,1:testSetMaxIdx), ytest.Data(2:testSetMaxIdx+1,:)'); fprintf('Percentage Correct Classification : %f%%\n', 100*(1-c)); fprintf('Percentage Incorrect Classification : %f%%\n', 100*c);
Percentage Correct Classification : 100.000000% Percentage Incorrect Classification : 0.000000%
モデルを半精度型でシミュレーションする
モデルを半精度型でシミュレーションするには、numerictype
オブジェクトを再定義し、データ型を 'half'
に設定します。モデルをシミュレートします。
floatType = numerictype('half'); activationType = numerictype('single'); sim(model);
この例では、半精度データ型を使用したときの精度の低下はありません。半精度シミュレーションでも、100% の分類精度が得られます。
[c, ~] = confusion(ttestsubset(:,1:testSetMaxIdx), ytest.Data(2:testSetMaxIdx+1,:)'); fprintf('Running Simulation with half precision :\n'); fprintf('Percentage Correct Classification : %f%%\n', 100*(1-c)); fprintf('Percentage Incorrect Classification : %f%%\n', 100*c);
Running Simulation with half precision : Percentage Correct Classification : 100.000000% Percentage Incorrect Classification : 0.000000%