ThreadingLayer
✖
ThreadingLayer
represents a net layer that takes several input arrays and applies a function f to corresponding array elements.
allows array shapes to be conformed according to broadcasting specification bspec.
Details and Options



- ThreadingLayer is typically used inside NetGraph.
- In ThreadingLayer[f], the function f can be any one of the following: Plus, Subtract, Times, Divide, Power, Surd, Min, Max, Clip, Mod, ArcTan, Erf.
- In general, f can be any object that when applied to two arguments gives any combination of Plus, Subtract, etc. together with numbers; all functions supported by ElementwiseLayer; and some logical operations using If, And, Or, Which, Piecewise, Equal, Greater, GreaterEqual, Less, LessEqual, Unequal.
- The function f can also contain expressions of the form "name"[input], where name is one of the named functions ("ELU", "SELU", "SoftSign", etc.) accepted by ElementwiseLayer.
- In ThreadingLayer[f,bspec], smaller arrays are implicitly replicated before applying f to match dimensions of larger arrays. The following settings can be given for bspec:
-
None no replication Automatic replicate along axis of size 1 (default) n insert dimension(s) with the right size at level n if needed -n count from the end {spec1,spec2,…} use specific broadcasting settings for the respective input arrays - Option InputPorts can be used to specify the number, names or shapes of input ports. If InputPorts is not specified, the number of ports and their shapes are inferred from NetChain or NetGraph connectivity.
- Besides input ports, ThreadingLayer exposes the following port for use in NetGraph etc.:
-
"Output" an array - Within a NetGraph, a ThreadingLayer can be connected using a single edge of the form {src1,src2,…}threadlayer, where threadlayer is the name or index of the ThreadingLayer, or as multiple separate edges given in the corresponding order, as src1threadlayer,src2threadlayer,…,srcnthreadlayer.
- If no names are explicitly given for input ports, ThreadingLayer[f,…] exposes input ports named "Input1", "Input2", etc.
- ThreadingLayer[Function[… #Name1 … #Name2 …], …] exposes input ports named "Name1", "Name2", etc.
- ThreadingLayer[f,None] forces all input arrays to have the same shape, and the output has this shape. ThreadingLayer[f,Automatic] forces all input arrays to have the same rank, and the respective shapes must be the same or equal to 1. ThreadingLayer[Plus,-1] automatically broadcasts inputs like Plus by inserting dimensions at the deepest level.
- ThreadingLayer[…][{in1,in2,…}] explicitly computes the output from applying the layer, which is effectively given by f[in1,in2,…].
- When given NumericArray as inputs, the output will be a NumericArray.
- Options[ThreadingLayer] gives the list of default options to construct the layer. Options[ThreadingLayer[…]] gives the list of default options to evaluate the layer on some data.
- Information[ThreadingLayer[…]] gives a report about the layer.
- Information[ThreadingLayer[…],prop] gives the value of the property prop of ThreadingLayer[…]. Possible properties are the same as for NetGraph.
Examples
open allclose allBasic Examples (6)Summary of the most common use cases
Create a ThreadingLayer using Times as the function to be threaded:

https://wolfram.com/xid/0bnh3jw6cmuf2-suvpmw

Apply the layer to a pair of inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-l4vjnx

Create a ThreadingLayer that sums arrays with different dimensions, broadcasting the smallest arrays on the first dimensions:

https://wolfram.com/xid/0bnh3jw6cmuf2-hoh3h1

Apply the layer to a pair of inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-1mdza2

Create a ThreadingLayer that sums arrays with different dimensions, broadcasting the smallest arrays on the last dimensions:

https://wolfram.com/xid/0bnh3jw6cmuf2-weyjno

Apply the layer to a pair of inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-0eqsxz

Use ThreadingLayer in a NetGraph:

https://wolfram.com/xid/0bnh3jw6cmuf2-jwzxw3


https://wolfram.com/xid/0bnh3jw6cmuf2-f929t7

Create a NetGraph that computes the outer sum of two arrays with ThreadingLayer:

https://wolfram.com/xid/0bnh3jw6cmuf2-bfjquh

Apply the outer sum to a pair of inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-gqzhga

The names of input ports can be specified using option "Inputs":

https://wolfram.com/xid/0bnh3jw6cmuf2-etg6m9

The names of input ports can be specified directly within the function of ThreadingLayer:

https://wolfram.com/xid/0bnh3jw6cmuf2-1j1tmi

Apply the layer to an association of arrays:

https://wolfram.com/xid/0bnh3jw6cmuf2-g7xhtd

Scope (2)Survey of the scope of standard use cases
Create a ThreadingLayer that takes a specific number of inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-miss03


https://wolfram.com/xid/0bnh3jw6cmuf2-k28wyh

Create a ThreadingLayer that uses a custom transformation to compute a spherical Gaussian:

https://wolfram.com/xid/0bnh3jw6cmuf2-53c5hd

Evaluate the layer on two input vectors to get a vector of outputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-txw6


https://wolfram.com/xid/0bnh3jw6cmuf2-t0axui

Applications (1)Sample problems that can be solved with this function
Define a hinge loss using a ThreadingLayer:

https://wolfram.com/xid/0bnh3jw6cmuf2-idugna
Create a net that computes the hinge loss with a margin of 2:

https://wolfram.com/xid/0bnh3jw6cmuf2-jtngzl

When the target is within distance 2 of the input, the loss is zero:

https://wolfram.com/xid/0bnh3jw6cmuf2-y63zt7

The loss increases linearly beyond a distance of 2:

https://wolfram.com/xid/0bnh3jw6cmuf2-c4hukg

Plot the loss as a function of input for a fixed target of 2:

https://wolfram.com/xid/0bnh3jw6cmuf2-jl4pzb

Perform linear regression using the hinge loss:

https://wolfram.com/xid/0bnh3jw6cmuf2-1kc6gm


https://wolfram.com/xid/0bnh3jw6cmuf2-sils2v

Compare the solutions obtained using the hinge loss, mean absolute and mean squared error:

https://wolfram.com/xid/0bnh3jw6cmuf2-6thr3m

https://wolfram.com/xid/0bnh3jw6cmuf2-zvitfz

Possible Issues (3)Common pitfalls and unexpected behavior
ThreadingLayer cannot accept symbolic inputs:

https://wolfram.com/xid/0bnh3jw6cmuf2-zestcp


Certain choices of f can produce failures for inputs outside their domain:

https://wolfram.com/xid/0bnh3jw6cmuf2-014g3k


Some pure functions are not supported:

https://wolfram.com/xid/0bnh3jw6cmuf2-0xokup


Wolfram Research (2017), ThreadingLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ThreadingLayer.html (updated 2021).
Text
Wolfram Research (2017), ThreadingLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ThreadingLayer.html (updated 2021).
Wolfram Research (2017), ThreadingLayer, Wolfram Language function, https://reference.wolfram.com/language/ref/ThreadingLayer.html (updated 2021).
CMS
Wolfram Language. 2017. "ThreadingLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/ThreadingLayer.html.
Wolfram Language. 2017. "ThreadingLayer." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/ThreadingLayer.html.
APA
Wolfram Language. (2017). ThreadingLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ThreadingLayer.html
Wolfram Language. (2017). ThreadingLayer. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ThreadingLayer.html
BibTeX
@misc{reference.wolfram_2025_threadinglayer, author="Wolfram Research", title="{ThreadingLayer}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/ThreadingLayer.html}", note=[Accessed: 19-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_threadinglayer, organization={Wolfram Research}, title={ThreadingLayer}, year={2021}, url={https://reference.wolfram.com/language/ref/ThreadingLayer.html}, note=[Accessed: 19-June-2025
]}