Deep learning: Standardize a list

Compute the core operation of every normalization layer.

What to build

A program that standardizes a list of numbers.

Requirements

  • Use the population variance (divide by n) and ε = 0.00001.
  • Print the standardized values with four decimal places, separated by spaces.

Input

One line: the numbers.

Output

One line.

Constraints

At most 1,000 numbers.

Example

Input:

1 2 3

Output:

-1.2247 0.0000 1.2247

Why it works

Subtracting the mean centres the values on zero and dividing by the standard deviation gives them a spread of one, whatever their original scale.

Examples

Example 1 · Input
1 2 3
Expected output
-1.2247 0.0000 1.2247
Example 2 · Input
5 5
Expected output
0.0000 0.0000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Compute the mean, then the variance as the mean of squared differences (divide by n).

Hint 2

Each output is (x − mean) / √(variance + 0.00001).

Solution

Show one correct solution (try it yourself first)
import math

xs = list(map(float, input().split()))
mean = sum(xs) / len(xs)
var = sum((x - mean) ** 2 for x in xs) / len(xs)
print(" ".join(f"{(x - mean) / math.sqrt(var + 0.00001):.4f}" for x in xs))

Lesson: Batch normalization and layer normalization →