Functional
ModuleExpectingFlatParameters
¶
A wrapper which brings a functional interface around a torch module.
Similar to functorch.FunctionalModule
, ModuleExpectingFlatParameters
turns a torch.nn.Module
instance to a function which expects a new
leftmost argument representing the parameters of the network.
Unlike functorch.FunctionalModule
, a ModuleExpectingFlatParameters
instance, as its name suggests, expects the network parameters to be
given as a 1-dimensional (i.e. flattened) tensor.
Also, unlike functorch.FunctionalModule
, an instance of
ModuleExpectingFlatParameters
is NOT an instance of torch.nn.Module
.
PyTorch modules with buffers can be wrapped by this class, but it is assumed that those buffers are constant. If the wrapped module changes the value(s) of its buffer(s) during its forward passes, most probably things will NOT work right.
As an example, let us consider the following linear layer.
The functional counterpart of net
can be obtained via:
from evotorch.neuroevolution.net import ModuleExpectingFlatParameters
fnet = ModuleExpectingFlatParameters(net)
Now, fnet
is a callable object which expects network parameters
and network inputs. Let us call fnet
with randomly generated network
parameters and with a randomly generated input tensor.
param_length = fnet.parameter_length
random_parameters = torch.randn(param_length)
random_input = torch.randn(3)
result = fnet(random_parameters, random_input)
Source code in evotorch/neuroevolution/net/functional.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
buffers
property
¶
Get the stored buffers
__call__(parameter_vector, x, h=None)
¶
Call the wrapped module's forward pass procedure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter_vector
|
Tensor
|
A 1-dimensional tensor which represents the parameters of the tensor. |
required |
x
|
Tensor
|
The inputs. |
required |
h
|
Any
|
Hidden state(s), in case this is a recurrent network. |
None
|
Source code in evotorch/neuroevolution/net/functional.py
__init__(net, *, disable_autograd_tracking=False)
¶
__init__(...)
: Initialize the ModuleExpectingFlatParameters
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net
|
Module
|
The module that is to be wrapped by a functional interface. |
required |
disable_autograd_tracking
|
bool
|
If given as True, all operations
regarding the wrapped module will be performed in the context
|
False
|
Source code in evotorch/neuroevolution/net/functional.py
__transfer_buffers(x)
¶
Transfer the buffer tensors to the device of the given tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The tensor whose device will also store the buffer tensors. |
required |
Source code in evotorch/neuroevolution/net/functional.py
make_functional_module(net, *, disable_autograd_tracking=False)
¶
Wrap a torch module so that it has a functional interface.
Similar to functorch.make_functional(...)
, this function turns a
torch.nn.Module
instance to a function which expects a new leftmost
argument representing the parameters of the network.
Unlike with functorch.make_functional(...)
, the parameters of the
network are expected in a 1-dimensional (i.e. flattened) tensor.
PyTorch modules with buffers can be wrapped by this class, but it is assumed that those buffers are constant. If the wrapped module changes the value(s) of its buffer(s) during its forward passes, most probably things will NOT work right.
As an example, let us consider the following linear layer.
The functional counterpart of net
can be obtained via:
Now, fnet
is a callable object which expects network parameters
and network inputs. Let us call fnet
with randomly generated network
parameters and with a randomly generated input tensor.
param_length = fnet.parameter_length
random_parameters = torch.randn(param_length)
random_input = torch.randn(3)
result = fnet(random_parameters, random_input)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net
|
Module
|
The |
required |
disable_autograd_tracking
|
bool
|
If given as True, all operations
regarding the wrapped module will be performed in the context
|
False
|