deepLearn

addLayer

Description

The addLayer action adds a layer to a deep learning model. It is part of the Deep Learning action set, which provides a comprehensive suite of tools for modeling and scoring with deep neural networks (DNN), convolutional neural networks (CNN), and recurrent neural networks (RNN). This action is fundamental in constructing the architecture of a neural network, allowing for the sequential addition and configuration of various layer types.

deepLearn.addLayer { layer={...}, modelTable={...}, name='string', nThreads=integer, replace=boolean, sharingWeights='string', srcLayers={'string-1', 'string-2', ...} };
Settings
ParameterDescription
layerSpecifies the layer type and its related parameters. This is a dictionary where the 'type' key determines the kind of layer (e.g., 'INPUT', 'CONV', 'FC') and other keys specify its configuration.
modelTableSpecifies the in-memory table that represents the model to which the layer will be added.
nameSpecifies a unique name for the layer. This name is used to reference the layer, for instance, in the 'srcLayers' parameter of subsequent layers.
nThreadsSpecifies the number of threads to use for the computation.
replaceWhen set to True, if a layer with the same name already exists in the model, it will be replaced by this new layer.
sharingWeightsSpecifies the name of another layer from which to share weights. This is useful for creating models like Siamese networks.
srcLayersSpecifies a list of names of the source layers for this new layer. This defines the data flow and connectivity within the network.
Data Preparation View data prep sheet
Data Creation

This action does not directly use training data. It operates on a model definition table. However, to build a complete, trainable model, you would typically first load your data (e.g., images, text, or tabular data) into a CAS table.

Copied!
1/* No
2data creation code is directly associated with addLayer. It modifies a model table. */

Examples

This example demonstrates how to add a simple 'INPUT' layer to a new model named 'my_model'. This is typically the first step in building a model architecture.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2DEEPLEARN.addLayer /
3 modelTable={name='my_model'}
4 layer={type='input', nchannels=3, width=224, height=224}
5 name='data';
6RUN;
Result :
The action adds an input layer named 'data' to the 'my_model' table. The output log will confirm the successful addition of the layer and display the updated model summary.

This example shows how to add a convolutional layer ('CONVO') to an existing model that already has an input layer named 'data'. The 'srcLayers' parameter is used to connect the new convolutional layer to the input layer.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2DEEPLEARN.addLayer /
3 modelTable={name='my_model'}
4 name='conv1'
5 layer={type='convolution', nFilters=32, width=3, height=3, stride=1, act='relu'}
6 srcLayers={'data'};
7RUN;
Result :
A convolutional layer named 'conv1' is added to the 'my_model' table, taking 'data' as its input. The action's output will show the updated model architecture, including the new 'conv1' layer and its connection.

This example illustrates adding a pooling layer and demonstrates a more advanced concept of sharing weights from another layer, although pooling layers themselves do not have weights. The 'sharingWeights' parameter is shown for conceptual understanding, typically used with layers like 'CONV' or 'FC'.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2DEEPLEARN.addLayer /
3 modelTable={name='my_model'}
4 name='pool1'
5 layer={type='pooling', width=2, height=2, stride=2, pool='max'}
6 srcLayers={'conv1'}
7 sharingWeights='conv1_shared'; /* Note: Conceptual use for pooling layer */
8RUN;
Result :
A max-pooling layer named 'pool1' is added after the 'conv1' layer. The output log will confirm the addition. The 'sharingWeights' parameter is specified, but it would have a functional effect only on layers with trainable weights.

FAQ

What is the purpose of the addLayer action in the Deep Learning action set?
What are the required parameters for the addLayer action?
What are some of the layer types that can be added using the 'layer' parameter?
How do you specify which existing layers connect to the new layer?
Is it possible to replace an existing layer in the model with a new one?
How can I make two layers share the same weights?
What is the function of the 'randomCrop' parameter in an 'INPUT' layer?

Associated Scenarios

Use Case
Standard: Building a CNN for Product Image Classification

A retail company wants to build a Convolutional Neural Network (CNN) to automatically classify images of its products into categories like 'shirts', 'shoes', and 'accessories'. ...

Use Case
Complex: Signature Verification with a Weight-Sharing Siamese Network

A financial institution needs to verify customer signatures by comparing a new signature image with a reference image on file. This requires a Siamese Network, which uses two id...

Use Case
Edge Case: Model Refactoring and Error Handling

A data science team is experimenting with different hyperparameters for a model. They need to replace an existing layer with a new one that has a different configuration. This s...