Skip to Content

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

ParameterTypeDescription
Nonesimulation_parameters: SimulationParametersSimulation parameters.
Nonefocal_length: OptimizableFloatA focal length for [ThinLens][svetlanna.elements.ThinLens]‘s in a 4f system.
Noneconv_diffractive_mask: OptimizableTensorAn initial mask for a [DiffractiveLayer][svetlanna.elements.DiffractiveLayer] placed between two lenses in the system.
Noneconv_mask_norm: floatA normalization factor for the convolutional mask.
Nonefs_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

ParameterTypeDescription
simulation_parametersSimulationParametersSimulation parameters.
network_elementsIterable[elements.Element]List of Elements for a Network after a convolutional layer (4f system).
Nonefocal_length: OptimizableFloatA focal length for [ThinLens][svetlanna.elements.ThinLens]‘s in a 4f system.
Noneconv_diffractive_mask: OptimizableTensorAn initial mask for a [DiffractiveLayer][svetlanna.elements.DiffractiveLayer] placed between two lenses in the system.
Noneconv_mask_norm: floatA normalization factor for the convolutional mask.
Nonefs_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) -> Wavefront

to_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) -> None

Reservoir network. The main idea is explained in the work . The governing formula is:

x_\text&#123;out&#125;[i] = F_\text&#123;NL&#125;(\beta x_\text&#123;in&#125;[i] + \alpha F_\text&#123;D&#125;(x_\text&#123;out&#125;[i-\tau]))

where F_\text&#123;NL&#125; is the nonlinear element, F_\text&#123;D&#125; is the delay element, α\alpha is the feedback_gain, β\beta is the input_gain, τ\tau is the delay in samples. The user should match the delay in samples with the actual light propagation time in F_\text&#123;D&#125;.

📥 Parameters

ParameterTypeDescription
nonlinear_elementLinearOpticalSetupLikeThe nonlinear element the light passes through.
delay_elementLinearOpticalSetupLikeThe delay line element.
feedback_gainfloatThe feedback (delay line) gain α\alpha.
input_gainfloatThe input gain β\beta
delayintThe 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

ParameterTypeDescription
fieldWavefrontThe new wavefront to be added to the end of the queue.

pop_feedback_queue

pop_feedback_queue(self) -> None | Wavefront

Retrieve 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) -> None

Clear all elements from the feedback queue.

forward

forward(self, input_wavefront: Wavefront) -> Wavefront

to_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

ParameterTypeDescription
encoder_elementsIterable[Element]The encoder elements.
decoder_elementsIterable[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) -> Wavefront

Propagation through the encoder part - encode a wavefront (input).

📤 Returns

Wavefront

An encoded input wavefront.

decode

decode(self, wavefront_encoded: Wavefront) -> Wavefront

Propagation through the decoder part - decode an encoded wavefront.

📤 Returns

Wavefront

A decoded wavefront.

forward

forward(self, input_wavefront: Wavefront) -> Wavefront

to_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

device

Methods

__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

ParameterTypeDescription
Nonesubsequence_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'