%% Use Custom Weighted Addition Layer in Network
% You can use a custom layer in the same way as any other layer in Deep Learning Toolbox. This section shows how to create and train a network for digit classification using the weighted addition layer you created earlier.
% Load the example training data.

[XTrain,YTrain] = digitTrain4DArrayData;

% Define a custom weighted addition layer. To create this layer, save the file weightedAdditionLayer.m in the current folder.
% Create a layer graph including the custom layer weightedAdditionLayer.

layers = [
    imageInputLayer([28 28 1],'Name','in')
    convolution2dLayer(5,20,'Name','conv1')
    reluLayer('Name','relu1')
    convolution2dLayer(3,20,'Padding',1,'Name','conv2')
    reluLayer('Name','relu2')
    convolution2dLayer(3,20,'Padding',1,'Name','conv3')
    reluLayer('Name','relu3')
    weightedAdditionLayer(2,'add')
    fullyConnectedLayer(10,'Name','fc')
    softmaxLayer('Name','softmax')
    classificationLayer('Name','classoutput')];

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

% Set the training options and train the network.

options = trainingOptions('adam','MaxEpochs',10);
net = trainNetwork(XTrain,YTrain,lgraph,options);

% View the weights learned by the weighted addition layer.

net.Layers(8).Weights

% Evaluate the network performance by predicting on new data and calculating the accuracy.

[XTest,YTest] = digitTest4DArrayData;
YPred = classify(net,XTest);
accuracy = sum(YTest==YPred)/numel(YTest)

% Copyright 2018 The MathWorks, Inc.