-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_linspace_vs_arange.py
29 lines (24 loc) · 1.04 KB
/
check_linspace_vs_arange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import torch
# to_int = False
to_int = True
for isize, osize in [
(500, 256), (4567, 123), (123, 3456), (456, 234),
]:
for offset in [0.0, 0.5]:
scale = isize / osize
print("-", isize, osize, offset, scale)
output_indices = torch.arange(osize)
expected_input_indices = ((output_indices + offset) * scale)
input_indices = torch.linspace(offset * scale, (osize - 1 + offset) * scale, steps=osize)
if to_int:
expected_input_indices = expected_input_indices.to(torch.int64)
input_indices = input_indices.to(torch.int64)
try:
torch.testing.assert_close(expected_input_indices, input_indices)
except AssertionError:
print("expected: ", expected_input_indices)
print("output : ", input_indices)
m = (expected_input_indices - input_indices).abs()
print("expected - output : ", m)
print("expected[m] : ", expected_input_indices[m > 0])
print("output[m] : ", input_indices[m > 0])