Re: Встречайте, ChatGPT - бот который может поддерживать беседу
Добавлено: Пн июл 22, 2024 2:29 pm
Certainly! Below are some examples to test the functions `fftn`, `fftshift`, `ifftn`, and `ifftshift` from the `torch.fft` module with CUDA if it's available in your environment.
```python
# Example 1: Test fftn, ifftn
[PYTHON]
import torch
from torch.fft import fftn, ifftn
# Create a random tensor and move it to GPU if available
tensor = torch.randn(4, 4)
if torch.cuda.is_available():
tensor = tensor.cuda()
print("Original Tensor:")
print(tensor)
# Perform FFT
fft_result = fftn(tensor)
print("\nFFT Result:")
print(fft_result)
# Perform inverse FFT
ifft_result = ifftn(fft_result)
print("\nInverse FFT Result:")
print(ifft_result)
[/PYTHON]
# Example 2: Test fftshift, ifftshift
[PYTHON]
import torch
from torch.fft import fftshift, ifftshift
# Create a random tensor and move it to GPU if available
tensor = torch.randn(4, 4)
if torch.cuda.is_available():
tensor = tensor.cuda()
print("Original Tensor:")
print(tensor)
# Perform FFT shift
shifted_tensor = fftshift(tensor)
print("\nFFT Shifted Tensor:")
print(shifted_tensor)
# Perform inverse FFT shift
ishifted_tensor = ifftshift(shifted_tensor)
print("\nInverse FFT Shifted Tensor:")
print(ishifted_tensor)
[/PYTHON]
```
Copy and paste the above code snippets into your Python environment to test whether these functions work with your version of CUDA Torch. Make sure to have a GPU available or the code will use the CPU for tensor operations. The original tensor, results of FFT and iFFT, and their shifted versions will be printed as output.