I converted images to tensors(4-D) now I want to shuffle it wothout disturbing the order.
I tried
idx = np.random.permutation(len(data))
x,y = data[idx], classes[idx]
but got error:
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])
CodePudding user response:
shuffling two tensors in the same order
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())
CodePudding user response:
If data and classes are tensors, you can do this
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)