Issue
This Content is from Stack Overflow. Question asked by ZKS
Below is my sample code
sample = np.array([-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0,15.0])
sample = tf.convert_to_tensor(sample)
tf.reshape(sample, shape=(3,3)),X.ndim
How to convert this 1D tensor to 2D tensor, I am bit confused. I tried multiple ways but it is always returning ndim as 1.
Could anyone please help
Solution
Your code is working fine. You reshaped your array into 3 rows and 3 columns,which is 2-dimensional array and same has shown in the output.
sample = np.array([-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0,15.0])
sample = tf.convert_to_tensor(sample)
tf.reshape(sample, shape=(3,3))
Output:
<tf.Tensor: shape=(3, 3), dtype=float64, numpy=
array([[-7., -4., -1.],
[ 2., 5., 8.],
[11., 14., 15.]])>
and to check the dimensions of the array, you can simply attach the .ndim at the end of the code.
tf.reshape(sample, shape=(3,3)).ndim #remove (,X) from the code as X has no assignments
Output:
2
This Question was asked in StackOverflow by ZKS and Answered by TFer2 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.