({{, (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer])[] 
represents a layer that multiplies its input by a learnable scaling array.}, {, (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer])[opts]
includes options for initial scaling and other parameters. }})

OpenersNotesSection(Spacer1)Details and Options

The scaling array learned by (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) has the same dimensions as the input to (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]).

The input to (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) is multiplied elementwise by the scaling array.

The following optional parameters can be included:

When the scale array is not explicitly specified or is given as (RefLink[Automatic, paclet:ref/Automatic]), it is added automatically when (RefLink[NetInitialize, paclet:ref/NetInitialize]) or (RefLink[NetTrain, paclet:ref/NetTrain]) is used. By default, the scale array will be initialized to consist of ones.

If a scale array has been added, (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer])[…][input] explicitly computes the output from applying the layer.

(RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer])[…][{(Subscript[input, 1]),(Subscript[input, 2]),…}] explicitly computes outputs for each of the (Subscript[input, i]).

When given a (RefLink[NumericArray, paclet:ref/NumericArray]) as input, the output will be a (RefLink[NumericArray, paclet:ref/NumericArray]).

The scaling array can be obtained from an initialized (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) using (RefLink[NetExtract, paclet:ref/NetExtract])[layer,"Scaling"].

(RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) is typically used inside (RefLink[NetChain, paclet:ref/NetChain]), (RefLink[NetGraph, paclet:ref/NetGraph]), etc.

(RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) exposes the following ports for use in (RefLink[NetGraph, paclet:ref/NetGraph]) etc.:

When it cannot be inferred from other layers in a larger net, the option "Input"->{(Subscript[d, 1]),(Subscript[d, 2]),…,(Subscript[d, n])} can be used to fix the input of (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) to be an array of dimensions (Subscript[d, 1])×(Subscript[d, 2])×…×(Subscript[d, n]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]):

ConstantTimesLayer[]

Create a (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) with an explicit scaling:

scale=ConstantTimesLayer["Scaling"->{1,0.8,1.1}]

Apply the layer to an input vector:

scale[{1,2,3}]

OpenersExampleSection1(Spacer1)Scope  (3)

Create a (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) that operates on single numbers by encoding them as length-1 vectors:

scale=ConstantTimesLayer["Scaling"->{0.5},"Input"->NetEncoder["Scalar"],"Output"->NetDecoder["Scalar"]]

Apply the layer to an input:

scale[1]

The layer threads across a batch of inputs:

scale[{1,2,3}]

Plot the output of the layer as a function of the input, compared to y=x:

Plot[{scale[x],x},{x,-2,2}]

Use a (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) in a (RefLink[NetChain, paclet:ref/NetChain]) to independently scale the components of the output of another layer:

net=NetChain[{ElementwiseLayer[Sin],ConstantTimesLayer["Scaling"->{0.33,1,3}]}]

Plot the three output components:

Plot[net[{x,x,x}//Normal],{x,-2Pi,2Pi}]

Create a random (RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) that applies a per-pixel and per-channel scaling to an image:

scale=NetInitialize[ConstantTimesLayer["Input"->"Image","Output"->"Image"],Method->{"Random","Scaling"->NormalDistribution[1,1/3]}]

scale[]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)"Scaling"  (1)

Specify an initial scaling:

scale=ConstantTimesLayer["Scaling"->{1,2,3}]

Apply the layer to an input vector:

scale[{0.,1.,-1.}]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

By default, (RefLink[NetInitialize, paclet:ref/NetInitialize]) will create a scaling that is composed of ones:

scale=NetInitialize[ConstantTimesLayer["Input"->{3}]]

NetExtract[scale,"Scaling"]

This is the identity scaling:

scale[{1,2,3}]

Specify a custom distribution to (RefLink[NetInitialize, paclet:ref/NetInitialize]):

scale=NetInitialize[ConstantTimesLayer["Input"->{3}],Method->{"Random","Scaling"->NormalDistribution[]}]

scale[{1,2,3}]

Alternatively, a scaling array can be supplied directly:

scale=ConstantTimesLayer["Scaling"->RandomVariate[NormalDistribution[],3]]

scale[{1,2,3}]

(RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) is similar to the two-input layer (RefLink[ThreadingLayer, paclet:ref/ThreadingLayer])[(RefLink[Times, paclet:ref/Times])], except that the scaling is learned during training rather than being taken from the output of another layer.

OpenersExampleSection4(Spacer1)Possible Issues  (1)

(RefLink[ConstantTimesLayer, paclet:ref/ConstantTimesLayer]) cannot accept symbolic inputs:

scale=NetInitialize@ConstantTimesLayer["Input"-> 2];
scale[{1,2}]

scale[{1,z}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ConstantArrayLayer, paclet:ref/ConstantArrayLayer])  ▪  (RefLink[ConstantPlusLayer, paclet:ref/ConstantPlusLayer])  ▪  (RefLink[ThreadingLayer, paclet:ref/ThreadingLayer])  ▪  (RefLink[NetChain, paclet:ref/NetChain])  ▪  (RefLink[NetGraph, paclet:ref/NetGraph])  ▪  (RefLink[NetInitialize, paclet:ref/NetInitialize])  ▪  (RefLink[NetTrain, paclet:ref/NetTrain])  ▪  (RefLink[NetExtract, paclet:ref/NetExtract])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Neural Networks in the Wolfram Language, paclet:tutorial/NeuralNetworksOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}})}})