Networks
Neural networks and optical systems
from svetlanna.networks import ...Classes
ConvLayer4F
Inherits:nn.Module
Diffractive convolutional layer based on a 4f system.
Methods
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, focal_length: OptimizableFloat, conv_diffractive_mask: OptimizableTensor, conv_mask_norm: float = 2 * torch.pi, fs_method: Literal['fresnel', 'AS'] = 'AS')📥 Parameters
| Parameter | Type | Description |
|---|---|---|
None | simulation_parameters: SimulationParameters | Simulation parameters. |
None | focal_length: OptimizableFloat | A focal length for [ThinLens][svetlanna.elements.ThinLens]‘s in a 4f system. |
None | conv_diffractive_mask: OptimizableTensor | An initial mask for a [DiffractiveLayer][svetlanna.elements.DiffractiveLayer] placed between two lenses in the system. |
None | conv_mask_norm: float | A normalization factor for the convolutional mask. |
None | fs_method: Literal['fresnel', 'AS'] | A method for FreeSpace’s in the system. |
Examples
- ```python —
- import svetlanna as sv —
- from svetlanna.visualization import show_structure —
- sim_params = … —
- conv_layer_4f = ConvLayer4F( — simulation_parameters=sim_params, focal_length=0.1, conv_diffractive_mask=torch.rand(sim_params.axis_sizes((“y”, “x”))),
- ) —
- show_structure(conv_layer_4f) —
- ``` —
- Output (in IPython environment): —
- <iframe —
- src=“show_structure_ConvLayer4F.html” —
- style=“width:100%; height:250px; border: 0; color-scheme: inherit;” allowtransparency=“true”></iframe> —
forward
forward(self, input_wavefront: Wavefront)to_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]ConvDiffNetwork4F
Inherits:nn.Module
A simple convolutional network with a 4f system as an optical convolutional layer. It consists of a [ConvLayer4F][svetlanna.networks.ConvLayer4F] and a [LinearOpticalSetup][svetlanna.LinearOpticalSetup] after it.
Methods
__init__ constructor
__init__(self, simulation_parameters: SimulationParameters, network_elements: Iterable[elements.Element], focal_length: OptimizableFloat, conv_diffractive_mask: OptimizableTensor, conv_mask_norm: float = 2 * torch.pi, fs_method: Literal['fresnel', 'AS'] = 'AS')📥 Parameters
| Parameter | Type | Description |
|---|---|---|
simulation_parameters | SimulationParameters | Simulation parameters. |
network_elements | Iterable[elements.Element] | List of Elements for a Network after a convolutional layer (4f system). |
None | focal_length: OptimizableFloat | A focal length for [ThinLens][svetlanna.elements.ThinLens]‘s in a 4f system. |
None | conv_diffractive_mask: OptimizableTensor | An initial mask for a [DiffractiveLayer][svetlanna.elements.DiffractiveLayer] placed between two lenses in the system. |
None | conv_mask_norm: float | A normalization factor for the convolutional mask. |
None | fs_method: Literal['fresnel', 'AS'] | A method for FreeSpace’s in the system. |
Examples
- ```python —
- import svetlanna as sv —
- from svetlanna.visualization import show_structure —
- sim_params = … —
- conv_diff_network_4f = ConvDiffNetwork4F( — simulation_parameters=sim_params, network_elements=( sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.1, method=“AS” ), sv.elements.ThinLens(simulation_parameters=sim_params, focal_length=0.1), sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.1, method=“AS” ), ), focal_length=0.1, conv_diffractive_mask=torch.rand(sim_params.axis_sizes((“y”, “x”))),
- ) —
- show_structure(conv_diff_network_4f) —
- ``` —
- Output (in IPython environment): —
- <iframe —
- src=“show_structure_ConvDiffNetwork4F.html” —
- style=“width:100%; height:25rem; border: 0; color-scheme: inherit;” allowtransparency=“true”></iframe> —
forward
forward(self, input_wavefront: Wavefront) -> Wavefrontto_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]SimpleReservoir
Inherits:torch.nn.Module
Methods
__init__ constructor
__init__(self, nonlinear_element: LinearOpticalSetupLike, delay_element: LinearOpticalSetupLike, feedback_gain: float, input_gain: float, delay: int) -> NoneReservoir network. The main idea is explained in the work . The governing formula is:
x_\text{out}[i] = F_\text{NL}(\beta x_\text{in}[i] + \alpha F_\text{D}(x_\text{out}[i-\tau]))where F_\text{NL} is the nonlinear element, F_\text{D} is the delay element, is the feedback_gain, is the input_gain, is the delay in samples. The user should match the delay in samples with the actual light propagation time in F_\text{D}.
📥 Parameters
| Parameter | Type | Description |
|---|---|---|
nonlinear_element | LinearOpticalSetupLike | The nonlinear element the light passes through. |
delay_element | LinearOpticalSetupLike | The delay line element. |
feedback_gain | float | The feedback (delay line) gain . |
input_gain | float | The input gain |
delay | int | The delay time, measured in samples, that the light spends in the delay line. |
Examples
- ```python —
- import svetlanna as sv —
- from svetlanna.visualization import show_structure —
- sim_params = … —
- reservoir = SimpleReservoir( — nonlinear_element=sv.elements.NonlinearElement( simulation_parameters=sim_params, response_function=lambda x: x**2, ), delay_element=sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.2, method=“AS” ), feedback_gain=0.5, input_gain=0.5, delay=3,
- ) —
- for input_wavefront in input_wavefront_sequence: — output = reservoir(input_wavefront)
- # clear the delay line before the next sequence or batch —
- reservoir.drop_feedback_queue() —
- show_structure(reservoir) —
- ``` —
- Output (in IPython environment): —
- <iframe —
- src=“show_structure_SimpleReservoir.html” —
- style=“width:100%; height:300px; border: 0; color-scheme: inherit;” allowtransparency=“true”></iframe> —
append_feedback_queue
append_feedback_queue(self, field: Wavefront)Append a new wavefront to the feedback queue.
📥 Parameters
| Parameter | Type | Description |
|---|---|---|
field | Wavefront | The new wavefront to be added to the end of the queue. |
pop_feedback_queue
pop_feedback_queue(self) -> None | WavefrontRetrieve and remove the first element from the feedback queue if available.
📤 Returns
None | Wavefront
The first wavefront in the queue if the queue is not empty; otherwise, None.
drop_feedback_queue
drop_feedback_queue(self) -> NoneClear all elements from the feedback queue.
forward
forward(self, input_wavefront: Wavefront) -> Wavefrontto_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]LinearAutoencoder
Inherits:nn.Module
A simple autoencoder network consisting of consistent encoder and decoder for a simultaneous training.
Methods
__init__ constructor
__init__(self, encoder_elements: Iterable[Element], decoder_elements: Iterable[Element])📥 Parameters
| Parameter | Type | Description |
|---|---|---|
encoder_elements | Iterable[Element] | The encoder elements. |
decoder_elements | Iterable[Element] | The decoder elements. |
Examples
- ```python —
- import svetlanna as sv —
- from svetlanna.visualization import show_structure —
- sim_params = … —
- linear_autoencoder = sv.networks.LinearAutoencoder( — encoder_elements=( sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.1, method=“AS” ), sv.elements.ThinLens(simulation_parameters=sim_params, focal_length=0.1), sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.1, method=“AS” ), ), decoder_elements=( sv.elements.FreeSpace( simulation_parameters=sim_params, distance=0.1, method=“AS” ), )
- ) —
- show_structure(linear_autoencoder) —
- ``` —
- Output (in IPython environment): —
- <iframe —
- src=“show_structure_LinearAutoencoder.html” —
- style=“width:100%; height:400px; border: 0; color-scheme: inherit;” allowtransparency=“true”></iframe> —
encode
encode(self, input_wavefront: Wavefront) -> WavefrontPropagation through the encoder part - encode a wavefront (input).
📤 Returns
Wavefront
An encoded input wavefront.
decode
decode(self, wavefront_encoded: Wavefront) -> WavefrontPropagation through the decoder part - decode an encoded wavefront.
📤 Returns
Wavefront
A decoded wavefront.
forward
forward(self, input_wavefront: Wavefront) -> Wavefrontto_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]LinearOpticalSetupLike
Inherits:Protocol
Protocol for objects that behave like linear optical setups.
This protocol provides flexibility when defining optical setups: any callable object (or composition of callables) is valid as long as it accepts a [Wavefront][svetlanna.Wavefront] and returns a [Wavefront][svetlanna.Wavefront].
It generalizes [LinearOpticalSetup][svetlanna.LinearOpticalSetup].
DiffractiveRNN
Inherits:nn.Module
A simple recurrent diffractive network of an architecture proposed in the article: https://www.nature.com/articles/s41566-021-00796-w
Properties
deviceMethods
__init__ constructor
__init__(self, sim_params: SimulationParameters, sequence_len: int, fusing_coeff: float, read_in_layer: nn.Sequential, memory_layer: nn.Sequential, hidden_forward_layer: nn.Sequential, read_out_layer: nn.Sequential, detector_layer: nn.Sequential, device: str | torch.device = torch.get_default_device())sim_params: SimulationParameters
Simulation parameters for the task.
sequence_len: int
A size (number of frames) of sequences (of Wavefronts) for prediction.
fusing_coeff: float
A coefficient in a function for a hidden state (lambda in methods of the article).
read_in_layer, memory_layer: nn.Sequential
Systems of elements for a D-RNN parts (see the article).
hidden_forward_layer: nn.Sequential
System for a hidden state after each frame input.
Comment:
mix_i = (1 - fusing_coeff) * read_in_layer(input_i) + fusing_coeff * hidden_i
hidden_i = hidden_forward_layer(mix_i)
read_out_layer, detector_layer: nn.Sequential
System of elements for a D-RNN output. detector_layer ends with a Detector!
device: torch.device
Specified device.
forward
forward(self, subsequence_wf: Wavefront)📥 Parameters
| Parameter | Type | Description |
|---|---|---|
None | subsequence_wf: Wavefront('batch_size', 'sequence_len', 'y', 'x') | Wavefronts for a sequence. Comment: works for a single wavelength in SimulationParameters! |
to_specs
to_specs(self) -> Iterable[ParameterSpecs | SubelementSpecs]to
to(self, device: str | torch.device | int) -> 'DiffractiveRNN'