如何在不干扰其顺序的情况下打乱数据(4-D 张量 {不能使用 sklearn}) 和标签



我将图像转换为张量(4-D(,现在我想在不干扰顺序的情况下对其进行混洗。

我试过

idx = np.random.permutation(len(data))
x,y = data[idx], classes[idx]

但出现错误:

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got array([135,  80, 178, ..., 253, 103])

以相同的顺序搅乱两个张量

indices = tf.range(start=0, limit=tf.shape(X)[0], dtype=tf.int32)
shuffled_indices = tf.random.shuffle(indices)
shuffled_X = tf.gather(X, shuffled_indices)
shuffled_y = tf.gather(y, shuffled_indices)
print('before')
print('X', X.numpy())
print('y', y.numpy())
print('after')
print('X', shuffled_X.numpy())
print('y', shuffled_y.numpy())

如果数据是张量,则可以执行

data = tf.constant([[i, i] for i in range(10)])
classes = tf.constant([i for i in range(10)])
idx = np.random.permutation(len(data))
x = tf.gather(data, idx)
y = tf.gather(classes, idx)

另请参阅以相同顺序搅乱两个张量

最新更新