Skip to main content

physics.neural

physics.neural

Neural computation: what a dense network costs, and what its activations and loss compute.

A dense (fully connected) network is a list of layer widths [n_0, n_1, ..., n_L]: layer l maps n_{l-1} inputs to n_l outputs with an n_{l-1} by n_l weight matrix and one bias per output. Every count here follows from the widths and the batch size alone (added 2026-10-20).

Functions

Name Description
calc_cross_entropy Evaluate scalar one-hot cross-entropy in natural-log units.
calc_dense_backward_flops Count dense weight-gradient and input-gradient matrix work.
calc_dense_biases Count one bias for each output unit in every layer.
calc_dense_forward_flops Count forward matrix and elementwise work by layer.
calc_dense_inference_activations Count peak activation values using adjacent rolling buffers.
calc_dense_macs Count dense multiply-accumulates for one example.
calc_dense_params Count weights and biases in a dense network.
calc_dense_saved_activations Count input and layer-output values retained for backward work.
calc_dense_traffic_values Count activation values transferred by unfused dense layers.
calc_dense_weights Count the dense weight matrix entries in each layer.
calc_relu Apply the scalar rectified linear activation.
calc_sigmoid Evaluate the logistic sigmoid.
calc_sigmoid_slope Evaluate the derivative of the logistic sigmoid.
calc_training_state Separate weight, gradient, and optimizer storage.
calc_training_step_ops Count launches in the assumed dense ReLU training schedule.

calc_cross_entropy

physics.neural.calc_cross_entropy(p_correct)

Evaluate scalar one-hot cross-entropy in natural-log units.

Parameters

Name Type Description Default
p_correct float Probability assigned to the correct class, in (0, 1]. required

Returns

Name Type Description
float Loss in nats.

Notes

Equation: H = -ln(p_correct).

Assumptions: One-hot target with the correct-class probability already normalized.

Excludes: Label smoothing, class weights, batch reduction, and softmax normalization.

References

Derivation: In -sum_i y_i ln(p_i), a one-hot target leaves only the correct-class term.

Examples

>>> calc_cross_entropy(1.0) == 0.0
True

calc_dense_backward_flops

physics.neural.calc_dense_backward_flops(widths, batch=1)

Count dense weight-gradient and input-gradient matrix work.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required
batch int Positive number of examples processed together; default 1. 1

Returns

Name Type Description
tuple of int Total weight-gradient FLOPs and total input-gradient FLOPs.

Notes

Equation: F_weight = sum_l 2 B n_(l-1) n_l; F_input sums the same work except the first layer.

Assumptions: Training does not request a gradient with respect to the original network input.

Excludes: Activation derivatives, bias gradients, loss work, and optimizer updates.

References

Derivation: F_weight = sum_l 2 B n_(l-1) n_l; F_input sums the same work except the first layer.

Examples

>>> calc_dense_backward_flops([2, 3], batch=2) == (2 * 2 * 2 * 3, 0)
True

calc_dense_biases

physics.neural.calc_dense_biases(widths)

Count one bias for each output unit in every layer.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required

Returns

Name Type Description
list of int Bias parameter counts, one per layer.

Notes

Equation: b_l = n_l.

Assumptions: Every dense layer has a bias.

Excludes: Bias-free layers, normalization, and weight matrices.

References

Derivation: Each output unit adds one independently stored scalar bias; therefore a layer with n_out units contains n_out bias parameters.

Examples

>>> calc_dense_biases([2, 3, 1])
[3, 1]

calc_dense_forward_flops

physics.neural.calc_dense_forward_flops(widths, batch=1)

Count forward matrix and elementwise work by layer.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required
batch int Positive number of examples processed together; default 1. 1

Returns

Name Type Description
tuple of list of int Matrix FLOPs and elementwise FLOPs, each indexed by layer.

Notes

Equation: F_matrix,l = 2 B n_(l-1) n_l; F_element,l = 2 B n_l.

Assumptions: A multiply-accumulate counts as two FLOPs; bias and activation cost one operation each.

Excludes: Architecture-specific activation costs, fused-kernel savings, and backward work.

References

Derivation: F_matrix,l = 2 B n_(l-1) n_l; F_element,l = 2 B n_l.

Examples

>>> calc_dense_forward_flops([2, 3], batch=2) == ([2 * 2 * 2 * 3], [2 * 2 * 3])
True

calc_dense_inference_activations

physics.neural.calc_dense_inference_activations(widths, batch=1)

Count peak activation values using adjacent rolling buffers.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required
batch int Positive number of examples processed together; default 1. 1

Returns

Name Type Description
int Peak simultaneously resident activation values.

Notes

Equation: V_peak = B max_l(n_(l-1) + n_l).

Assumptions: Layers execute serially; consumed buffers can be reused.

Excludes: Skip connections, concurrent execution, weights, and runtime workspace.

References

Derivation: V_peak = B max_l(n_(l-1) + n_l).

Examples

>>> calc_dense_inference_activations([2, 3, 1]) == 2 + 3
True

calc_dense_macs

physics.neural.calc_dense_macs(widths)

Count dense multiply-accumulates for one example.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required

Returns

Name Type Description
int Multiply-accumulate count per example.

Notes

Equation: MACs = sum_l n_(l-1) n_l.

Assumptions: One matrix-vector product is evaluated per layer.

Excludes: Bias additions, nonlinearities, and backward computation.

References

Derivation: Evaluating a dense layer uses each weight once in a multiply-accumulate for one example; summing the matrix entry counts gives the network MAC count.

Examples

>>> calc_dense_macs([2, 3]) == 2 * 3
True

calc_dense_params

physics.neural.calc_dense_params(widths)

Count weights and biases in a dense network.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required

Returns

Name Type Description
int Total trainable parameter count.

Notes

Equation: P = sum_l (n_(l-1) + 1) n_l.

Assumptions: Every dense connection has a weight and every output has a bias.

Excludes: Normalization parameters, shared weights, and sparsity.

References

Derivation: P = sum_l (n_(l-1) + 1) n_l.

Examples

>>> calc_dense_params([2, 3]) == (2 + 1) * 3
True

calc_dense_saved_activations

physics.neural.calc_dense_saved_activations(widths, batch=1)

Count input and layer-output values retained for backward work.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required
batch int Positive number of examples processed together; default 1. 1

Returns

Name Type Description
int Saved activation value count.

Notes

Equation: V = B sum_l n_l, including input width.

Assumptions: Every layer output and the original input remain resident.

Excludes: Recomputation, masks, workspace, and weight or optimizer storage.

References

Derivation: V = B sum_l n_l, including input width.

Examples

>>> calc_dense_saved_activations([2, 3, 1], batch=2) == 2 * (2 + 3 + 1)
True

calc_dense_traffic_values

physics.neural.calc_dense_traffic_values(widths, batch=1)

Count activation values transferred by unfused dense layers.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required
batch int Positive number of examples processed together; default 1. 1

Returns

Name Type Description
int Activation values moved per forward pass, not bytes.

Notes

Equation: V = B [n_0 + 2 sum(hidden widths) + n_L].

Assumptions: Each layer reads its input and writes its output to main memory.

Excludes: Weight reads, cache reuse, fusion, and conversion from values to bytes.

References

Derivation: V = B [n_0 + 2 sum(hidden widths) + n_L].

Examples

>>> calc_dense_traffic_values([2, 3, 1]) == 2 + 2 * 3 + 1
True

calc_dense_weights

physics.neural.calc_dense_weights(widths)

Count the dense weight matrix entries in each layer.

Parameters

Name Type Description Default
widths sequence of int Positive layer widths [input, hidden…, output]; at least two entries. Counts are dimensionless. required

Returns

Name Type Description
list of int Parameter counts, one per layer.

Notes

Equation: W_l = n_(l-1) n_l.

Assumptions: Every input connects to every output in each layer.

Excludes: Biases, sparsity, shared parameters, and convolutional structure.

References

Derivation: A fully connected mapping from n_in inputs to n_out outputs has one independently stored weight for every input-output pair, giving n_in times n_out entries.

Examples

>>> calc_dense_weights([2, 3]) == [2 * 3]
True

calc_relu

physics.neural.calc_relu(x)

Apply the scalar rectified linear activation.

Parameters

Name Type Description Default
x float Dimensionless scalar activation. required

Returns

Name Type Description
float Zero for a negative input; otherwise the unchanged input.

Notes

Equation: ReLU(x) = max(0, x).

Assumptions: Scalar input.

Excludes: Leaky variants, derivatives, and tensor execution costs.

References

Derivation: Rectification preserves a nonnegative scalar and replaces a negative scalar with zero; the maximum with zero implements this piecewise definition.

Examples

>>> calc_relu(-2.0)
0.0

calc_sigmoid

physics.neural.calc_sigmoid(x)

Evaluate the logistic sigmoid.

Parameters

Name Type Description Default
x float Dimensionless finite activation; extreme negative inputs may overflow the exponential. required

Returns

Name Type Description
float Dimensionless sigmoid activation between zero and one.

Notes

Equation: s(x) = 1 / (1 + exp(-x)).

Assumptions: Scalar arithmetic using the standard exponential implementation.

Excludes: Vectorization and overflow-stabilized evaluation.

References

Derivation: s(x) = 1 / (1 + exp(-x)).

Examples

>>> calc_sigmoid(0.0)
0.5

calc_sigmoid_slope

physics.neural.calc_sigmoid_slope(x)

Evaluate the derivative of the logistic sigmoid.

Parameters

Name Type Description Default
x float Dimensionless finite activation; inherits sigmoid exponential limits. required

Returns

Name Type Description
float Dimensionless local derivative.

Notes

Equation: s_prime(x) = s(x) [1 - s(x)].

Assumptions: The derivative is evaluated at a single scalar activation.

Excludes: Products across layers and numerical overflow stabilization.

References

Derivation: Differentiate s(x) = 1 / (1 + exp(-x)) by the chain rule.

Examples

>>> calc_sigmoid_slope(0.0) == 0.5 * (1 - 0.5)
True

calc_training_state

physics.neural.calc_training_state(
    parameters,
    bytes_per_value,
    optimizer_bytes_per_parameter,
)

Separate weight, gradient, and optimizer storage.

Parameters

Name Type Description Default
parameters int Nonnegative trainable parameter count. required
bytes_per_value float or Quantity Bytes per weight and gradient; use matching storage units across both storage inputs. required
optimizer_bytes_per_parameter float or Quantity Additional optimizer bytes per parameter, supplied explicitly. required

Returns

Name Type Description
tuple Weights, gradients, and optimizer storage; unit-bearing inputs retain their units.

Notes

Equation: M_weights = M_gradients = P b; M_optimizer = P o.

Assumptions: One common weight/gradient precision; optimizer storage is supplied separately.

Excludes: Activation memory, runtime buffers, and any master weights not included in the optimizer input.

References

Derivation: M_weights = M_gradients = P b; M_optimizer = P o.

Examples

>>> calc_training_state(3, 2, 8) == (3 * 2, 3 * 2, 3 * 8)
True

calc_training_step_ops

physics.neural.calc_training_step_ops(layers)

Count launches in the assumed dense ReLU training schedule.

Parameters

Name Type Description Default
layers int Positive number of dense layers. required

Returns

Name Type Description
int Operation-launch count per training step, not FLOPs.

Notes

Equation: N = L + 1 + (2L - 1) + (L - 1) + 1 = 4L.

Assumptions: Each forward matrix operation fuses bias and ReLU; loss and optimizer each use one launch. Backward computes all weight gradients and all but the first input gradient.

Excludes: Compiler fusion across layers, communication, and optimizer implementations with multiple launches.

References

Derivation: N = L + 1 + (2L - 1) + (L - 1) + 1 = 4L.

Examples

>>> calc_training_step_ops(3) == 4 * 3
True
Back to top