Transposing a Complex Tensor with Conjugation
Here, we generate a tensor of complex numbers using NumPy and TensorFlow. It defines the dimensions of the tensor, the range of values for the real and imaginary parts, and then creates the complex tensor. After converting it into a TensorFlow constant, the code transposes the tensor while conjugating its elements using TensorFlow’s tf.transpose()
function with conjugate=True
. Finally, it prints both the original tensor and the transposed tensor with conjugation.
Python3
import numpy as np # Define the dimensions of the complex matrix num_rows = 3 num_cols = 3 #range min_val = 0 max_val = 50 # Generate a tensor of complex numbers complex_tensor = np.random.randint(min_val, max_val + 1 , size = (num_rows, num_cols)) + 1j * np.random.randint(min_val, max_val,size = (num_rows, num_cols)) tensor = tf.constant(complex_tensor) print ( "tensor of complex numbers: " ) print (tensor) # Transpose the tensor with conjugation transposed_conj_x = tf.transpose(tensor, conjugate = True ) print ( "Transposed Conjugate tensor:" ) print (transposed_conj_x) |
Output:
tensor of complex numbers:
tf.Tensor(
[[15. +9.j 12.+27.j 19.+46.j]
[45.+48.j 16.+21.j 49.+27.j]
[12. +5.j 1.+45.j 32.+46.j]], shape=(3, 3), dtype=complex128)
Transposed Conjugate tensor:
tf.Tensor(
[[15. -9.j 45.-48.j 12. -5.j]
[12.-27.j 16.-21.j 1.-45.j]
[19.-46.j 49.-27.j 32.-46.j]], shape=(3, 3), dtype=complex128)
Tensor Transpose in Tensorflow With Example
Tensor transpose is a fundamental operation in TensorFlow that rearranges the dimensions of a tensor according to a specified permutation. This operation is crucial in various machine learning algorithms and data manipulation tasks.
Tensor is useful when dealing with multidimensional data, such as images, time series, and sequences. Transposing a tensor changes the order of its dimensions, providing flexibility in data manipulation and computation.
In this article, we will learn Tensor Transpose in TensorFlow with Example.
Syntax of tf.transpose()
tf.transpose(
a, perm=None, conjugate=False, name=’transpose’
)
Parameters
- a: Input tensor.
- perm: Permutation of dimensions. If not provided, the default permutation is set to (n-1…0), where n is the rank of the input tensor.
- conjugate: Optional parameter for complex tensors. The values are conjugated and transposed if set to True and the tensor dtype is either complex64 or complex128.
- name: Optional parameter for operation name.
Contact Us