以相同的顺序打乱两个张量 [英] shuffling two tensors in the same order

查看:40
本文介绍了以相同的顺序打乱两个张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上.我尝试了那些无济于事:

As above. I tried those to no avail:

tf.random.shuffle( (a,b) )
tf.random.shuffle( zip(a,b) )

我曾经连接它们并进行改组,然后取消连接/解包.但是现在我处于 (a) 是 4D 秩张量而 (b) 是 1D 的情况,因此,无法连接.

I used to concatenate them and do the shuffling, then unconcatenate / unpack. But now I'm in a situation where (a) is 4D rank tensor while (b) is 1D, so, no way to concatenate.

我还尝试将种子参数提供给 shuffle 方法,以便它重现相同的洗牌,我使用它两次 => 失败.还尝试用随机洗牌的数字范围自己进行洗牌,但 TF 在花哨的索引和东西方面不如 numpy 灵活 ==> 失败.

I also tried to give the seed argument to the shuffle method so it reproduces the same shuffling and I use it twice => Failed. Also tried to do the shuffling myself with randomly shuffled range of numbers, but TF is not as flexible as numpy in fancy indexing and stuff ==> failed.

我现在要做的是,将所有内容转换回 numpy,然后使用 sklearn 中的 shuffle,然后通过重铸返回张量.这完全是愚蠢的方式.这应该发生在图表内.

What I'm doing now is, convert everything back to numpy then use shuffle from sklearn then go back to tensors by recasting. It is sheer stupid way. This is supposed to happen inside a graph.

推荐答案

您可以只对索引进行混洗,然后使用 tf.gather() 提取与这些混洗后的索引对应的值:

You could just shuffle the indices and then use tf.gather() to extract values corresponding to those shuffled indices:

TF2.x(更新)

import tensorflow as tf
import numpy as np

x = tf.convert_to_tensor(np.arange(5))
y = tf.convert_to_tensor(['a', 'b', 'c', 'd', 'e'])

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())
# before
# x [0 1 2 3 4]
# y [b'a' b'b' b'c' b'd' b'e']
# after
# x [4 0 1 2 3]
# y [b'e' b'a' b'b' b'c' b'd']

TF1.x

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, (None, 1, 1, 1))
y = tf.placeholder(tf.int32, (None))

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)

确保在同一会话运行中计算 shuffled_xshuffled_y.否则它们可能会得到不同的索引顺序.

Make sure that you compute shuffled_x, shuffled_y in the same session run. Otherwise they might get different index orderings.

# Testing
x_data = np.concatenate([np.zeros((1, 1, 1, 1)),
                         np.ones((1, 1, 1, 1)),
                         2*np.ones((1, 1, 1, 1))]).astype('float32')
y_data = np.arange(4, 7, 1)

print('Before shuffling:')
print('x:')
print(x_data.squeeze())
print('y:')
print(y_data)

with tf.Session() as sess:
  x_res, y_res = sess.run([shuffled_x, shuffled_y],
                          feed_dict={x: x_data, y: y_data})
  print('After shuffling:')
  print('x:')
  print(x_res.squeeze())
  print('y:')
  print(y_res)

Before shuffling:
x:
[0. 1. 2.]
y:
[4 5 6]
After shuffling:
x:
[1. 2. 0.]
y:
[5 6 4]

这篇关于以相同的顺序打乱两个张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆