The forward process of diffusion mixes a little noise into the image at every step.
What to build
A program that applies a given number of noising steps to a row of pixels, using the noise values it is given.
Requirements
- At each step replace every pixel p by
(1 - a) * p + a * r, whereais the strength andris that pixel's noise value for the step. - After each step print
step T:and the pixels with three decimal places. - Finally print
mean change C: the average of the absolute differences between the last pixels and the original ones, to three decimal places.
Input
Line 1: the pixels. Line 2: the strength a. Line 3: the number of steps T. Then T lines of noise values, one per pixel.
Output
T + 1 lines.
Constraints
1 ≤ T ≤ 10; 1 to 20 pixels; all numbers from 0 to 1.
Example
Input:
1.0 0.0 1.0 0.0
0.5
2
0.5 0.5 0.5 0.5
1.0 0.0 0.0 1.0
Output:
step 1: 0.750 0.250 0.750 0.250
step 2: 0.875 0.125 0.375 0.625
mean change 0.375
Why it works
Each step keeps a share 1 − a of the old image and adds a share a of noise, so the picture drifts further from the original, and the mean change measures how far.
Examples
1.0 0.0 1.0 0.0 0.5 2 0.5 0.5 0.5 0.5 1.0 0.0 0.0 1.0
step 1: 0.750 0.250 0.750 0.250 step 2: 0.875 0.125 0.375 0.625 mean change 0.375
0.2 0.4 1.0 1 0.9 0.1
step 1: 0.900 0.100 mean change 0.500
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
One step is `p = (1 - a) * p + a * noise` for every pixel, using that step's noise values.
Hint 2
Print every pixel to three decimal places, and compare with the original pixels for the last line.
Solution
Show one correct solution (try it yourself first)
pixels = list(map(float, input().split()))
original = pixels[:]
a = float(input())
steps = int(input())
for t in range(1, steps + 1):
noise = list(map(float, input().split()))
pixels = [(1 - a) * p + a * r for p, r in zip(pixels, noise)]
print(f"step {t}: " + " ".join(f"{p:.3f}" for p in pixels))
change = sum(abs(p - o) for p, o in zip(pixels, original)) / len(pixels)
print(f"mean change {change:.3f}")