Deep Learning Toolbox 覚えがき

MATLAB の Deep Learning Toolbox に関する覚えがき。

# データセット

https://jp.mathworks.com/help/matlab/datastore.html

# 層

https://jp.mathworks.com/help/deeplearning/ug/list-of-deep-learning-layers.html

# trainingOptions

https://jp.mathworks.com/help/deeplearning/ref/trainingoptions.html

# 例

## もっとも簡単な例

```matlab
train_x = rand(5, 1, 1, 1000);
train_y = sum(train_x, 1) > 2.5;
train_y = categorical(train_y(:) * 1, [0 1]);

val_x = rand(5, 1, 1, 100);
val_y = sum(val_x, 1) > 2.5;
val_y = categorical(val_y(:) * 1, [0 1]);

layers = [
  imageInputLayer([5 1 1])
  fullyConnectedLayer(16)
  reluLayer
  fullyConnectedLayer(2)
  softmaxLayer
  classificationLayer
];

options = trainingOptions('adam',...
  'Plots', 'training-progress', 'Verbose', true,...
  'MaxEpochs', 100, 'MiniBatchSize', 128,...
  'Shuffle', 'every-epoch', 'ValidationData', {val_x val_y});
[nn, info] = trainNetwork(train_x, train_y(:), layers, options);
```

```matlab
score = nn.predict(val_x);
[~, idx] = max(score, [], 2);
mean(nn.Layers(end).Classes(idx) == val_y);
% => 1
```

## DataStore を使った例

```matlab:CustomDataStore.m
classdef CustomDataStore < matlab.io.Datastore & matlab.io.datastore.MiniBatchable & matlab.io.datastore.Shuffleable
  properties
    X
    Y
    Classes
    NumClasses
    Dimension
    MiniBatchSize
  end

  properties(SetAccess = protected)
    NumObservations
  end

  properties(Access = private)
    % DataStore から継承されている
    CurrentFileIndex
  end

  methods
    function ds = CustomDataStore(n)
      ds.X = rand(n, 5);
      ds.Y = categorical(double(sum(ds.X, 2) > 2.5), [0 1]);
      ds.Classes = categories(ds.Y);
      ds.NumClasses = numel(ds.Classes);
      ds.Dimension = [5 1 1];
      ds.MiniBatchSize = n;
      ds.NumObservations = n;
      ds.CurrentFileIndex = 1;
    end

    function tf = hasdata(ds)
      % 読み込めるミニバッチが存在するか?
      tf = ds.CurrentFileIndex + ds.MiniBatchSize - 1 <= ds.NumObservations;
    end

    function [data, info] = read(ds)
      % 読み込んだデータを返す
      % data は table にする
      x = num2cell(ds.X(ds.CurrentFileIndex:ds.CurrentFileIndex + ds.MiniBatchSize - 1, :, :, :), 2:4);
      x = cellfun(@(s) reshape(s, ds.Dimension), x, 'un', 0);
      y = num2cell(ds.Y(ds.CurrentFileIndex:ds.CurrentFileIndex + ds.MiniBatchSize - 1));
      data = table(x, y);
      info = struct();

      ds.CurrentFileIndex = ds.CurrentFileIndex + ds.MiniBatchSize;
    end

    function reset(ds)
      % 先頭に戻す
      ds.CurrentFileIndex = 1;
    end

    function dsNew = shuffle(ds)
      % シャッフルされた新しい DataStore を返す
      dsNew = copy(ds);
      idx = randperm(ds.NumObservations);
      dsNew.X = ds.X(idx, :, :, :);
      dsNew.Y = ds.Y(idx);
    end
  end

  methods (Hidden = true)
    function frac = progress(ds)
      frac = (ds.CurrentFileIndex - 1) / ds.NumObservations;
    end
  end
end
```

```matlab:train.m
ds_train = CustomDataStore(1000);
ds_train.MiniBatchSize = 100;
ds_val = CustomDataStore(100);

layers = [
  imageInputLayer(ds_train.Dimension)
  fullyConnectedLayer(16)
  reluLayer
  fullyConnectedLayer(2)
  softmaxLayer
  classificationLayer
];

options = trainingOptions('adam',...
  'Plots', 'training-progress', 'Verbose', true,...
  'MaxEpochs', 100, 'MiniBatchSize', 100,...
  'Shuffle', 'every-epoch', 'ValidationData', ds_val);
[nn, info] = trainNetwork(ds_train, layers, options);
```