[英]how to shuffle data (4-D Tensor {can't use sklearn}) and label without disturbing their order
我将图像转换为张量(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 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)
另请参阅以相同顺序改组两个张量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.