NetPort["port"]
represents the specified input or output port for a complete net.
NetPort[{n,"port"}]
represents the specified port for layer number n in a NetGraph or similar construct.
NetPort[{"name","port"}]
represents the specified port for the layer with the specified name.
NetPort[{spec1,spec2,…,"port"}]
permits access to the port of nested layers in a NetGraph or a NetChain.
represents the set of all states in a net associated with recurrent layers such as LongShortTermMemoryLayer.
NetPort
NetPort["port"]
represents the specified input or output port for a complete net.
NetPort[{n,"port"}]
represents the specified port for layer number n in a NetGraph or similar construct.
NetPort[{"name","port"}]
represents the specified port for the layer with the specified name.
NetPort[{spec1,spec2,…,"port"}]
permits access to the port of nested layers in a NetGraph or a NetChain.
represents the set of all states in a net associated with recurrent layers such as LongShortTermMemoryLayer.
Details
- When used in NetGraph, NetPort[{layer,"port"}] refers to the named output port of a layer when used on the left-hand side of a rule, and a named input port of a layer when used on the right-hand side of a rule.
- When used in a NetGraph, NetPort["port"] refers to an input of the entire graph when used on the left-hand side of a rule, and an output of the entire graph when used on the right-hand side of a rule.
- net[data,NetPort[oport]] can be used to obtain the value of the net at oport when it is applied to the specified data. oport must refer to an output port, a subnet of the net, or a layer.
- net[data,{NetPort[oport1],NetPort[oport2],…}] returns an association <|NetPort[oport1]->…,NetPort[oport2]->…,…|> whose values are the values of the net at oporti when the net is applied to the specified data.
- net[data,NetPort[All,"States"]] will return an association of the final recurrent states taken by recurrent layers in a net.
- net[<|…,NetPort[All,"States"]->assoc|>] will set the initial recurrent states used by recurrent layers in a net.
Examples
open all close allBasic Examples (3)
Create a graph with two outputs having custom names:
NetGraph[{LinearLayer[5], SummationLayer[]}, {1 -> NetPort["Output"], 1 -> 2 -> NetPort["Sum"]}]Create a graph with two inputs having custom names:
NetGraph[{TotalLayer[]}, {NetPort["Input1"] -> 1, NetPort["Input2"] -> 1}]Create a graph with a loss layer:
NetGraph[{ElementwiseLayer[Tanh], DropoutLayer[], MeanSquaredLossLayer[]}, {1 -> NetPort[3, "Target"], 2 -> NetPort[3, "Input"]}]Scope (3)
Create a NetChain:
net = NetChain[{ElementwiseLayer[Tanh], ElementwiseLayer[Cos], ElementwiseLayer[Sin]}]Obtain the output of the first layer of the net when applied to data:
net[{-1.2, 3.1}, NetPort[1, "Output"]]Tanh[{-1.2, 3.1}]This is also equivalent to specifying the layer instead:
net[{-1.2, 3.1}, NetPort[1]]Obtain the output of the first two layers of the net:
net[{-1.2, 3.1}, {NetPort[1, "Output"], NetPort[2, "Output"]}]Obtain the outputs of all layers in the net:
net[{-1.2, 3.1}, NetPort[All, "Output"]]Create a chain that contains a recurrent layer:
net = NetInitialize@NetChain[{UnitVectorLayer[], LongShortTermMemoryLayer[2], SoftmaxLayer[]}, "Input" -> NetEncoder["Characters"]]The recurrent layer contains two recurrent states:
Information[net, "RecurrentStatesPositionList"]net["zero day"]Obtain an association of the final states of the recurrent layer:
net["zero day", NetPort[All, "States"]]Obtain both the output and the final states:
net["zero day", {NetPort["Output"], NetPort[All, "States"]}]Set the initial value of the recurrent states:
istates = <|{2, "State"} -> {1, 1}, {2, "CellState"} -> {0, 0}|>;
net[<|"Input" -> "zero day", NetPort[All, "States"] -> istates|>, {NetPort["Output"], NetPort[All, "States"]}]Create a NetGraph with a nested NetChain:
net = NetGraph[{NetChain[{Tanh, Cos, Sin}], CatenateLayer[]}, {{1, NetPort["Input"]} -> 2}]Compute the output of the second layer of the nested NetChain:
net[{0.5, 0.1}, NetPort[{1, 2, "Output"}]]Add this output as a second output of the net:
net2 = NetGraph[net, {NetPort[{1, 2, "Output"}] -> NetPort["Output2"]}]%[{0.5, 0.1}]Connect the second layer of the nested NetChain to another network:
NetGraph[{net, ElementwiseLayer[# + 1&]}, {NetPort[{1, 1, 2, "Output"}] -> 2}]%[{0.5, 0.1}]Properties & Relations (1)
Information can be used to obtain the names of inputs, outputs and recurrent states suitable for use with NetPort:
net = NetGraph[{LongShortTermMemoryLayer[3], SequenceLastLayer[], CrossEntropyLossLayer["Index"]}, {1 -> 2 -> 3, 2 -> NetPort["Output"]}]Information [net, "InputPortNames"]Information [net, "OutputPortNames"]Information [net, "RecurrentStatesPositionList"]Possible Issues (1)
The value of an input port during the evaluation of a net cannot be obtained using NetPort:
n = NetInitialize@NetChain[{1, 2, 3}, "Input" -> 2];n[{1, 1}, NetPort[{2, "Input"}]]Tech Notes
Related Guides
Text
Wolfram Research (2016), NetPort, Wolfram Language function, https://reference.wolfram.com/language/ref/NetPort.html (updated 2021).
CMS
Wolfram Language. 2016. "NetPort." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/NetPort.html.
APA
Wolfram Language. (2016). NetPort. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetPort.html
BibTeX
@misc{reference.wolfram_2026_netport, author="Wolfram Research", title="{NetPort}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/NetPort.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_netport, organization={Wolfram Research}, title={NetPort}, year={2021}, url={https://reference.wolfram.com/language/ref/NetPort.html}, note=[Accessed: 13-June-2026]}