【发布时间】:2021-07-26 00:56:53
【问题描述】:
给定以下张量:
<tf.Tensor: shape=(59,), dtype=int64, numpy=
array([1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1,
1, 0, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1])>
重塑它并将其填充到(32, 59) 的正确方法是什么?从 keras 文档我尝试:
keras.preprocessing.sequence.pad_sequences(t, 32)
尽管如此,我得到了:
ValueError: `sequences` must be a list of iterables. Found non-iterable: tf.Tensor(1, shape=(), dtype=int64)
另外,我试过了:
tf.reshape(tf.data.Dataset.from_tensors(a[0]).padded_batch(32), [32,59])
但是,我得到:
ValueError: Attempt to convert a value (<PaddedBatchDataset shapes: (None, 59), types: tf.int64>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.PaddedBatchDataset'>) to a Tensor.
进行 32 填充并将其重塑为 32,59 的正确方法是什么?
【问题讨论】:
-
你可以重复
( 59 , )32 次来生成一个数组( 32 , 59 )。使用tf.repeat() -
你能举个例子吗? @ShubhamPanchal 谢谢
-
它能解决您的问题吗?这与填充不同。
-
如果我填充它并重塑它会更好@ShubhamPnchal
-
查看docs 示例。
标签: python numpy tensorflow keras