简体   繁体   English

重塑数组内不同维度的 numpy arrays

[英]Reshaping numpy arrays of differing dimensions inside an array

So the task is to optimise a Neural Network with a PSO.因此,任务是使用 PSO 优化神经网络。 The PSO needs a one-dimensional list of all the weights and biases, like so [0.1 0.244... 0.214]. PSO 需要一个包含所有权重和偏差的一维列表,例如 [0.1 0.244... 0.214]。 The NN needs an array of arrays with different dimensions, like so [[x,y], [m,n], ...(all the hidden layer matrices)... ,[p,q]] X and y are the dimensions for the input layer, then all the hidden layers and finally p and q - the dimensions of the output layer. NN 需要一个具有不同维度的 arrays 数组,例如 [[x,y], [m,n], ...(所有隐藏层矩阵)... ,[p,q]] X 和 y 是输入层的尺寸,然后是所有隐藏层,最后是 p 和 q - output 层的尺寸。

I can easily flatten the array to pass it to the PSO, but I need a method that takes the modified array and reshapes it back into the same array of arrays with the same dimensions as the starting one from the NN.我可以轻松地将数组展平以将其传递给 PSO,但我需要一种方法来获取修改后的数组并将其重新整形为相同的 arrays 数组,其尺寸与来自 NN 的起始数组相同。

The dimensions depend on the amount of neurons in a layer, we have that information from the start.维度取决于层中神经元的数量,我们从一开始就有这些信息。

I have tried to keep track of the shapes array and create an indices array to know when to stop but it doesn't seem to work.我试图跟踪形状数组并创建一个索引数组以知道何时停止,但它似乎不起作用。 I am trying something with slicing now but no cigar yet.我现在正在尝试切片,但还没有雪茄。 A modification to the NN is also possible but how to create it so it takes a predefined list of weights?也可以对 NN 进行修改,但如何创建它以获取预定义的权重列表? There might be a very nice and efficient way to do it but I just haven't thought of it yet... Any suggestions?可能有一种非常好的和有效的方法来做到这一点,但我还没有想到它......有什么建议吗?

Example:例子:

a = np.array([1,2,3])
b = np.array([7,8,9,10])
c = np.array([12,13,14,15,16])
b.reshape(2,2)
arr = []
arr.append(a)
arr.append(b)
arr.append(c)

This is a very simple example of what the list of weights is as the NN works with it - a list of multi-dimensional array.这是 NN 使用权重列表的一个非常简单的示例 - 多维数组列表。 Arr can be converted into a numpy array of objects if necessary with np.asarray(arr).如果需要,可以使用 np.asarray(arr) 将 arr 转换为 numpy 对象数组。

Flattening is easy, here is how I do it (there might be a better that doesn't need a loop, if you know, I'd be thankful if you shared).展平很容易,这就是我的做法(可能有一个更好的不需要循环的方法,如果您知道,如果您分享,我将不胜感激)。

Flattening:展平:

new_arr = np.array([])
for i in range(len(arr)):
    new_arr = np.append(arr, arr[i].flatten())

My question is how to take new_arr and put it back together to look like arr and is there a beautiful and fast way to do it.我的问题是如何获取 new_arr 并将其重新组合在一起以使其看起来像 arr 并且有一种美丽而快速的方法来做到这一点。

You can save the shape in a variable (it's just a tuple).您可以将形状保存在变量中(它只是一个元组)。 Try something like:尝试类似:

...
old_shape = arr.shape

# ... do flattening here

new_arr.reshape(old_shape)
new_arr = np.array([])
shapes=[]
for i in range(len(arr)):
    new_arr = np.append(new_arr, arr[i].flatten())
    shapes.append(arr[i].shape)

#do whatever

restoredArray =[]
offset=0
for i in range(len(shapes)):
    s = shapes[i]
    n = np.prod(s)
    restoredArray.append(new_arr[offset:(offset+n)].reshape(s))
    offset+=n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM