为神经网络打乱两个 numpy 数组

2024-05-14

我有两个 numpy 数组用于输入数据 X 和输出数据 y。

X = np.array(([2, 3],                    # sample 1 x
              [16, 4]), dtype=float)     # sample 2 x
y = np.array(([1, 0],                    # sample 1 y
              [0, 1]), dtype=float)      # sample 2 y

我想使用小批量来训练神经网络,如何在知道相应输出仍然对齐的情况下对两个数组进行洗牌?


您可以拥有与各自数组形状相同的索引数组,并且每次都对索引数组进行洗牌。在这种情况下,您可以使用打乱索引以相同的方式重新对齐两个数组。

In [122]: indices = np.indices((2, 2))

In [125]: np.random.shuffle(indices)

In [126]: indices
Out[126]: 
array([[[0, 0],
        [1, 1]],

       [[0, 1],
        [0, 1]]])

In [127]: x[indices[0], indices[1]]
Out[127]: 
array([[ 2.,  3.],
       [16.,  4.]])

In [128]: y[indices[0], indices[1]]
Out[128]: 
array([[1., 0.],
       [0., 1.]])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为神经网络打乱两个 numpy 数组 的相关文章

随机推荐