メインコンテンツ

TCN 異常検出器の学習とテスト

ファイル sineWaveAnomalyData.mat を読み込みます。このファイルには、3 チャネルの合成正弦波信号が 2 セット含まれています。

sineWaveNormal には、10 個の安定した周波数と振幅の正弦波が含まれています。各信号に小振幅の影響のような一連の劣化があります。信号ごとに長さと初期位相が異なります。

load sineWaveAnomalyData.mat sineWaveNormal sineWaveAbnormal
s1 = 3;

入力信号のプロット

最初の 3 つの正常な信号をプロットします。各信号に 3 つの入力チャネルがあります。

tiledlayout("vertical")
ax = zeros(s1,1);
for kj = 1:s1
ax(kj) = nexttile;
    plot(sineWaveNormal{kj})
    title("Normal Signal Channels")
end

Figure contains 3 axes objects. Axes object 1 with title Normal Signal Channels contains 3 objects of type line. Axes object 2 with title Normal Signal Channels contains 3 objects of type line. Axes object 3 with title Normal Signal Channels contains 3 objects of type line.

sineWaveAbnormal には、すべて同じ長さの 3 つの信号が含まれています。このセットの各信号には 1 つ以上の異常があります。

  • 1 つ目の信号には、すべてのチャネルに一定の時間にわたって続く周波数の急激な変化があります。

  • 2 つ目の信号には、チャネルの 1 つに一定期間の振幅の変化があります。

  • 3 つ目の信号には、すべてのチャネルにランダムな時点で発生するスパイクがあります。

異常がある 3 つの信号をプロットします。

tiledlayout("vertical")
ax = zeros(s1,1);
for kj = 1:s1
ax(kj) = nexttile;
    plot(sineWaveAbnormal{kj})
    title("Anomalous Signal")
end

Figure contains 3 axes objects. Axes object 1 with title Anomalous Signal contains 3 objects of type line. Axes object 2 with title Anomalous Signal contains 3 objects of type line. Axes object 3 with title Anomalous Signal contains 3 objects of type line.

検出器の作成

tcnAD 関数を使用して、既定のオプションで tcnDetector オブジェクトを作成します。

detector_tcn = tcnAD(3)
detector_tcn = 
  TcnDetector with properties:

               FilterSize: 7
       DropoutProbability: 0.2500
    DetectionWindowLength: 10
               NumFilters: 32
                IsTrained: 0
              NumChannels: 3
                   Layers: [23×1 nnet.cnn.layer.Layer]
                    Dlnet: [1×1 dlnetwork]
                Threshold: []
          ThresholdMethod: "kSigma"
       ThresholdParameter: 3
        ThresholdFunction: []
            Normalization: "zscore"
          DetectionStride: 10

検出器の学習

trainingOptions オプション セットをカスタマイズして detector_tcn に学習させる準備を行います。ソルバーを "adam" に、学習エポック数の制限を 100 に設定します。

trainopts = trainingOptions("adam",MaxEpochs=100);

通常状態のデータと trainopts を使用して detector_tcn に学習させます。

detector_tcn = train(detector_tcn,sineWaveNormal,trainingOpts=trainopts);
    Iteration    Epoch    TimeElapsed    LearnRate    TrainingLoss
    _________    _____    ___________    _________    ____________
            1        1       00:00:00        0.001          2.3547
           50       50       00:00:08        0.001         0.16135
          100      100       00:00:16        0.001         0.10187
Training stopped: Max epochs completed
Computing threshold...
Threshold computation completed.

train によって計算されて detector_tcn 内に保存されたしきい値を確認します。この計算値は、学習に使用されたデータのサブセットなどのランダム因子の影響を受け、学習セッションやマシンの違いによって多少変わることがあります。

thresh = detector_tcn.Threshold
thresh = 
2.9427

通常状態のデータについての異常スコアのヒストグラムをプロットします。各スコアが単一の検出ウィンドウで計算されます。しきい値は垂直な線としてプロットされ、スコアの範囲を常に完全に示すとは限りません。

plotHistogram(detector_tcn,sineWaveNormal)

Figure contains an axes object. The axes object with title Window Anomaly Score Distribution, xlabel Anomaly Scores, ylabel Probability (Histogram) contains 2 objects of type histogram, constantline. This object represents Data 1.

検出器を使用した異常の識別

detect 関数を使用して、異常状態のデータの異常スコアを判定します。

results = detect(detector_tcn, sineWaveAbnormal)
results=3×1 cell array
    130×3 table
    130×3 table
    130×3 table

results は、チャネルごとに 1 つずつ、3 つの table を含む cell 配列です。cell の各 table に 3 つの変数 WindowLabelWindowAnomalyScoreWindowStartIndices が含まれています。table 変数の名前を確認します。

varnames = results{1}.Properties.VariableNames
varnames = 1×3 cell array
    "'Labels'"    "'AnomalyScores'"    "'StartIndices'"

結果のプロット

通常状態のデータ、異常状態のデータ、およびしきい値を示すヒストグラムを 1 つのプロットにプロットします。

plotHistogram(detector_tcn,sineWaveNormal,sineWaveAbnormal)

Figure contains an axes object. The axes object with title Window Anomaly Score Distribution, xlabel Anomaly Scores, ylabel Probability (Histogram) contains 3 objects of type histogram, constantline. These objects represent Data 1, Data 2.

ヒストグラムでは、通常状態のデータと異常状態のデータに異なる色が使用されます。

3 つ目の異常な信号セットについて、検出された異常をプロットします。

plot(detector_tcn,sineWaveAbnormal{3})

Figure contains 2 axes objects. Axes object 1 with title Anomalies, xlabel Samples, ylabel Signal contains 7 objects of type patch, line. These objects represent Labeled Anomalies, Raw Signal (Channel 3), Raw Signal (Channel 2), Raw Signal (Channel 1), Detected Anomalies (Channel 3), Detected Anomalies (Channel 2), Detected Anomalies (Channel 1). Axes object 2 with title Anomaly Scores, xlabel Window Start Index, ylabel Score contains 3 objects of type stem, line, constantline. One or more of the lines displays its values using only markers These objects represent Anomaly Scores, Detected Anomalies.

上のプロットには、赤のオーバーレイで異常の発生箇所が示されています。下のプロットは、信号セット 3 の通常状態と異常スコアの切り分けにしきい値がどの程度効果的であるかを示しています。