在张量流中将一维张量转换为二维张量



下面是我的示例代码

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

如何将这个1D张量转换为2D张量,我有点困惑。我尝试了多种方法,但它总是将ndim返回为1。

有人能帮吗

您的代码运行良好。您将数组重新整形为3行3列,这是一个二维数组,输出中也显示了同样的内容。

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))

输出:

<tf.Tensor: shape=(3, 3), dtype=float64, numpy=
array([[-7., -4., -1.],
[ 2.,  5.,  8.],
[11., 14., 15.]])>

要检查数组的大小,只需在代码末尾附加.ndm即可。

tf.reshape(sample, shape=(3,3)).ndim        #remove (,X) from the code as X has no assignments

输出:

2

最新更新