【问题标题】:Can I convert a Numpy ndarray to a Tensor?我可以将 Numpy ndarray 转换为张量吗?
【发布时间】:2020-11-15 17:36:56
【问题描述】:

我有一个长度为 40,000 的对象。这 40,000 个数组中的每一个都有不同的长度。数组中的所有值都是整数 (0-25)。

我的 XTrain 数据如下所示:

[array([13,  8,  3,  7, 12, 16, 11,  1,  9, 17,  2, 18,  3,  5, 12, 19,  9,
       10, 20, 14,  1, 15, 12, 19,  2,  2,  6, 20, 14, 19,  2,  7, 12,  2,
        2,  1, 10,  8,  5, 10, 11,  2, 12, 11,  5,  7, 18, 12, 16, 20,  2,
       10, 11,  7,  1,  7,  5,  5,  1,  4,  9, 10,  9, 13, 11, 20,  7, 10,
       15, 15, 12, 13, 16, 20, 16,  8, 14, 13,  8, 19, 11, 12,  8, 12, 16,
       16, 11, 13, 15, 19,  7,  6, 14,  8,  4, 11, 12, 14, 12, 19,  2,  3,
        2,  7, 14, 18,  5,  2,  8, 19, 19, 20,  4, 17, 20,  8, 12,  3, 17,
        1, 12, 10,  6,  4, 19, 10, 12,  9,  6, 11,  7,  7,  4, 12, 13,  8,
        7,  6, 11, 16, 10, 15, 19, 15,  8, 16, 15, 14, 17,  8,  2, 12, 24])
 array([13,  2, 20, 11, 12, 14,  8,  8, 17, 16, 20,  1,  3,  1,  7,  2, 14,
       11,  2, 20,  1,  4, 10, 11,  7, 16,  3,  1,  2,  6,  8,  6, 20,  1,
       17, 20, 11, 16,  1, 15,  1, 12, 10, 17,  3,  9, 11, 20,  1, 13, 10,
        7, 12, 17, 10, 16,  8,  6,  4,  1, 11, 15,  3, 10, 16,  4,  1,  7,
        2, 10, 14,  1,  7, 11, 11, 17,  8, 11,  1,  1,  1,  6, 15,  8, 14,
       15, 11,  1,  6, 11, 12, 17, 14, 20,  4,  6,  7, 14,  1,  6, 10, 12,
        9, 20, 11,  9,  8, 10, 16, 11, 11,  8,  6,  5, 15,  4, 16, 10,  3,
        1,  1, 11, 10,  5,  2,  8,  7, 12, 13, 16, 10,  1, 10, 13,  1,  8,
       20, 11,  7,  1,  2,  8,  9,  3, 20, 17, 20, 10,  4, 15, 20,  7, 12,
       11, 11,  1, 20,  8,  9, 19, 11,  7, 16, 17, 20,  4, 10,  1,  7, 16,
       17,  2,  2, 10,  1,  1, 16,  2, 10, 15,  1,  4,  9, 13, 20, 11, 13,
        1,  8, 14, 17,  1,  8,  3,  7, 12,  8,  7, 11, 20, 20, 11,  8,  2,
        3,  8, 16,  4, 19, 16,  1,  1, 20, 11,  1,  1,  5, 11,  2,  1,  4])
    ...
]

在尝试使用以下方法将此数据转换为张量时:

x_train = tf.convert_to_tensor(
    XTrain
)

我收到以下错误:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

编辑 1:有人建议改用这个:

x_train = tf.convert_to_tensor(
    XTrain, np.float32
)

然后我得到TypeError: only size-1 arrays can be converted to Python scalars & ValueError: setting an array element with a sequence.

我的 XTrain 数据是这样创建的:

XTrain = np.empty([len(AArecords)], dtype=object);
XTest = np.empty([len(AArecords)], dtype=object);
i = 0;
while (i < 40000):
  XTrain[i] = np.array(aa2int(AArecords[i]))
  i += 1
XTrain = np.transpose(XTrain)

AArecords 看起来像这样:

['MGNEKSLAHTRWNCKYHIVFAPKYRRQVFYREKRRAI...'
 'MRVLKFGGTSVANAERFLRVADILESNA...'
 'MVKVYAPASSANMSVGFD...*'
 ...]

【问题讨论】:

  • tf.convert_to_tensor(data, np.float32)
  • @Pygirl 这导致“TypeError:只有 size-1 数组可以转换为 Python 标量”“ValueError:使用序列设置数组元素。”
  • 你能告诉我你的数据长度吗?
  • @Pygirl 长度为 40,000。 40,000 个数组中的每一个都有不同的长度。

标签: python numpy tensorflow multidimensional-array tensor


【解决方案1】:

您可以将非矩形 Python 序列转换为 RaggedTensor 为:

x_train = tf.ragged.constant(XTrain)

【讨论】:

  • 非常好,一直在寻找解决方案几个小时,但在任何地方都找不到正确的答案。 Python 和相关框架的文档很差
  • 这个解决方案也对我有用。此外,在我将 ndarray 序列转换为 RaggedTensor 后,它的形状为 (8732, None, None, None)。为了获得 (8732, 32, 32, 3) 的形状,我使用了to_tensor() 方法。
猜你喜欢
  • 2020-06-27
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多