Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

replaceLayer

層グラフまたはネットワークの層の置き換え

説明

lgraphUpdated = replaceLayer(lgraph,layerName,larray) は、層グラフ lgraph の層 layerNamelarray の層に置き換えます。

replaceLayer は、larray の層を順に結合し、larray を層グラフに結合します。

netUpdated = replaceLayer(net,layerName,larray) は、dlnetwork オブジェクト net の層 layerNamelarray の層に置き換えます。

replaceLayer は、larray の層を順に結合し、larray をネットワークに結合します。

___ = replaceLayer(___,'ReconnectBy',mode) は、さらに、層を再結合する方法を指定します。

すべて折りたたむ

シンプルなネットワーク アーキテクチャを定義してプロットします。

layers = [
    imageInputLayer([28 28 1],'Name','input')
    convolution2dLayer(3,16,'Padding','same')
    reluLayer('Name','relu_1')
    additionLayer(2,'Name','add')
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

lgraph = layerGraph(layers);
lgraph = connectLayers(lgraph,'input','add/in2');

figure
plot(lgraph)

Figure contains an axes object. The axes object contains an object of type graphplot.

ネットワークの ReLU 層を、バッチ正規化層およびそれに続く leaky ReLU 層に置き換えます。

layers = [
    batchNormalizationLayer
    leakyReluLayer(0.1)];
lgraph = replaceLayer(lgraph,'relu_1',layers);

plot(lgraph)

Figure contains an axes object. The axes object contains an object of type graphplot.

この例では、事前学習済みの Keras ネットワークから層をインポートし、サポートされていない層をカスタム層に置き換え、予測の準備が整ったネットワークをこれらの層から組み立てる方法を説明します。

Keras ネットワークのインポート

Keras ネットワーク モデルから層をインポートします。'digitsDAGnetwithnoise.h5' のネットワークは数字のイメージを分類します。

filename = 'digitsDAGnetwithnoise.h5';
lgraph = importKerasLayers(filename,'ImportWeights',true);
Warning: Unable to import some Keras layers, because they are not supported by the Deep Learning Toolbox. They have been replaced by placeholder layers. To find these layers, call the function findPlaceholderLayers on the returned object.

Keras ネットワークには、Deep Learning Toolbox ではサポートされていない層がいくつか含まれています。関数 importKerasLayers は警告を表示して、サポートされていない層をプレースホルダー層に置き換えます。

plot を使用して層グラフをプロットします。

figure
plot(lgraph)
title("Imported Network")

Figure contains an axes object. The axes object with title Imported Network contains an object of type graphplot.

プレースホルダー層の置き換え

プレースホルダー層を置き換えるには、まず、置き換える層の名前を特定します。findPlaceholderLayers を使用してプレースホルダー層を見つけます。

placeholderLayers = findPlaceholderLayers(lgraph)
placeholderLayers = 
  2x1 PlaceholderLayer array with layers:

     1   'gaussian_noise_1'   GaussianNoise   Placeholder for 'GaussianNoise' Keras layer
     2   'gaussian_noise_2'   GaussianNoise   Placeholder for 'GaussianNoise' Keras layer

これらの層の Keras 構成を表示します。

placeholderLayers.KerasConfiguration
ans = struct with fields:
        trainable: 1
             name: 'gaussian_noise_1'
           stddev: 1.5000
    inbound_nodes: {{1x1 cell}}

ans = struct with fields:
        trainable: 1
             name: 'gaussian_noise_2'
           stddev: 0.7000
    inbound_nodes: {{1x1 cell}}

補助関数 gaussianNoiseLayer を使用して、インポートした Keras 層と同じ構成を持つ 2 つのガウス ノイズ層を作成します。

gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1');
gnLayer2 = gaussianNoiseLayer(0.7,'new_gaussian_noise_2');

replaceLayer を使用してプレースホルダー層をカスタム層に置き換えます。

lgraph = replaceLayer(lgraph,'gaussian_noise_1',gnLayer1);
lgraph = replaceLayer(lgraph,'gaussian_noise_2',gnLayer2);

plot を使用して、更新された層グラフをプロットします。

figure
plot(lgraph)
title("Network with Replaced Layers")

Figure contains an axes object. The axes object with title Network with Replaced Layers contains an object of type graphplot.

クラス名の指定

インポートした分類層にクラスが含まれていない場合、予測の前にこれらを指定しなければなりません。クラスを指定しない場合、クラスは 12、...、N に自動的に設定されます。ここで、N はクラスの数です。

層グラフの Layers プロパティを表示して、分類層のインデックスを見つけます。

lgraph.Layers
ans = 
  15x1 Layer array with layers:

     1   'input_1'                            Image Input             28x28x1 images
     2   'conv2d_1'                           2-D Convolution         20 7x7x1 convolutions with stride [1  1] and padding 'same'
     3   'conv2d_1_relu'                      ReLU                    ReLU
     4   'conv2d_2'                           2-D Convolution         20 3x3x1 convolutions with stride [1  1] and padding 'same'
     5   'conv2d_2_relu'                      ReLU                    ReLU
     6   'new_gaussian_noise_1'               Gaussian Noise          Gaussian noise with standard deviation 1.5
     7   'new_gaussian_noise_2'               Gaussian Noise          Gaussian noise with standard deviation 0.7
     8   'max_pooling2d_1'                    2-D Max Pooling         2x2 max pooling with stride [2  2] and padding 'same'
     9   'max_pooling2d_2'                    2-D Max Pooling         2x2 max pooling with stride [2  2] and padding 'same'
    10   'flatten_1'                          Keras Flatten           Flatten activations into 1-D assuming C-style (row-major) order
    11   'flatten_2'                          Keras Flatten           Flatten activations into 1-D assuming C-style (row-major) order
    12   'concatenate_1'                      Depth concatenation     Depth concatenation of 2 inputs
    13   'dense_1'                            Fully Connected         10 fully connected layer
    14   'activation_1'                       Softmax                 softmax
    15   'ClassificationLayer_activation_1'   Classification Output   crossentropyex

分類層の名前は 'ClassificationLayer_activation_1' です。分類層を表示して、Classes プロパティを確認します。

cLayer = lgraph.Layers(end)
cLayer = 
  ClassificationOutputLayer with properties:

            Name: 'ClassificationLayer_activation_1'
         Classes: 'auto'
    ClassWeights: 'none'
      OutputSize: 'auto'

   Hyperparameters
    LossFunction: 'crossentropyex'

この層の Classes プロパティは 'auto' であるため、クラスを手動で指定しなければなりません。クラスを 01、...、9 に設定してから、インポートした分類層を新しい層に置き換えます。

cLayer.Classes = string(0:9)
cLayer = 
  ClassificationOutputLayer with properties:

            Name: 'ClassificationLayer_activation_1'
         Classes: [0    1    2    3    4    5    6    7    8    9]
    ClassWeights: 'none'
      OutputSize: 10

   Hyperparameters
    LossFunction: 'crossentropyex'

lgraph = replaceLayer(lgraph,'ClassificationLayer_activation_1',cLayer);

ネットワークの組み立て

assembleNetwork を使用して層グラフを組み立てます。この関数は、予測に使用する準備が整った DAGNetwork オブジェクトを返します。

net = assembleNetwork(lgraph)
net = 
  DAGNetwork with properties:

         Layers: [15x1 nnet.cnn.layer.Layer]
    Connections: [15x2 table]
     InputNames: {'input_1'}
    OutputNames: {'ClassificationLayer_activation_1'}

入力引数

すべて折りたたむ

層グラフ。LayerGraph オブジェクトとして指定します。層グラフを作成するには、layerGraph を使用します。

ニューラル ネットワーク。dlnetwork オブジェクトとして指定します。

置き換える層の名前。string スカラーまたは文字ベクトルとして指定します。

ネットワーク層。Layer 配列として指定します。

組み込み層の一覧については、深層学習層の一覧を参照してください。

層を再結合する方法。次のいずれかとして指定します。

  • 'name' – 置き換えられた層の入力名と出力名を使用して、larray を再結合します。置き換えられた層の入力に結合された各層について、larray(1) と同じ入力名をもつ入力に層を再結合します。置き換えられた層の出力に結合された各層について、larray(end) と同じ出力名をもつ出力に層を再結合します。

  • 'order'larray(1) の入力名と larray(end) の出力名の順序を使用して、larray を再結合します。置き換えられた層の i 番目の入力に結合されている層を、larray(1)i 番目の入力に再結合します。置き換えられた層の j 番目の出力に結合されている層を、larray(end)j 番目の出力に再結合します。

データ型: char | string

出力引数

すべて折りたたむ

更新された層グラフ。LayerGraph オブジェクトとして返されます。

更新されたネットワーク。未初期化の dlnetwork オブジェクトとして返されます。

dlnetwork オブジェクトの学習可能なパラメーターを初期化するには、関数 initialize を使用します。

関数 replaceLayer は量子化情報を保持しません。入力ネットワークが量子化されたネットワークであった場合でも、出力ネットワークに量子化情報は含まれません。

バージョン履歴

R2018b で導入