【问题标题】:Converting 1 D tensor to 2 D tensor in tensor flow在张量流中将一维张量转换为二维张量
【发布时间】:2022-09-29 17:46:39
【问题描述】:

下面是我的示例代码

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

如何将此一维张量转换为二维张量,我有点困惑。我尝试了多种方法,但它总是将 ndim 返回为 1。

谁能帮忙

  • 您没有将结果分配给任何东西。你可能想要sample = tf.reshape(sample, shape=(3,3))

标签: python numpy tensorflow2.0 tensor


【解决方案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.]])>

要检查数组的尺寸,您只需在代码末尾附加 .ndim 即可。

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

输出:

2

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 2018-05-10
    • 1970-01-01
    • 2020-09-26
    • 2022-07-20
    • 1970-01-01
    • 2019-05-30
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多