-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
38 lines (30 loc) · 951 Bytes
/
utils.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
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import torch.fft as torch_fft
def ifft(x):
x = torch_fft.fftshift(x, dim=(-2, -1))
x = torch_fft.ifft2(x, dim=(-2, -1), norm='ortho')
x = torch_fft.ifftshift(x, dim=(-2, -1))
return x
def fft(x):
x = torch_fft.ifftshift(x, dim=(-2, -1))
x = torch_fft.fft2(x, dim=(-2, -1), norm='ortho')
x = torch_fft.fftshift(x, dim=(-2, -1))
return x
def itemize(x):
"""Converts a Tensor into a list of Python numbers.
"""
if len(x.shape) < 1:
x = x[None]
if x.shape[0] > 1:
return [xx.item() for xx in x]
else:
return x.item()
# Complex dot product of two complex-valued multidimensional Tensors
def zdot_batch(x1, x2):
batch = x1.shape[0]
return torch.reshape(torch.conj(x1)*x2, (batch, -1)).sum(1)
# Same, applied to self --> squared L2-norm
def zdot_single_batch(x):
return zdot_batch(x, x)