简体   繁体   English

Python数组维度问题

[英]Python arrays dimension issues

I am struggling once again with Python, NumPy and arrays to compute some calculations between matrices. 我再次努力使用Python, NumPy和数组来计算矩阵之间的一些计算。

The code part that is likely not working properly is as follows: 可能无法正常工作的代码部分如下:

train, test, cv = np.array_split(data, 3, axis = 0) 
train_inputs = train[:,: -1]
test_inputs = test[:,: -1]
cv_inputs = cv[:,: -1]

train_outputs = train[:, -1]
test_outputs = test[:, -1]
cv_outputs = cv[:, -1]

When printing those matrices informations ( np.ndim , np.shape and dtype respectively), this is what you get: 当打印这些矩阵信息( np.ndimnp.shapedtype分别),这是你会得到什么:

2
1
2
1
2
1
(94936, 30)
(94936,)
(94936, 30)
(94936,)
(94935, 30)
(94935,)
float64
float64
float64
float64
float64
float64

I believe it is missing 1 dimension in all *_output arrays. 我相信它在所有*_output数组中都缺少1维。

The other matrix I need is created by this command: 我需要的另一个矩阵是通过以下命令创建的:

newMatrix = neuronLayer(30, 94936) 

In which neuronLayer is a class defined as: 其中neuronLayer是一个定义为的类:

class neuronLayer():
    def __init__(self, neurons, neuron_inputs):
        self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1

Here's the final output: 这是最终的输出:

outputLayer1 = self.__sigmoid(np.dot(inputs, self.layer1.weights))
ValueError: shapes (94936,30) and (94936,30) not aligned: 30 (dim 1) != 94936 (dim 0)

Python is clearly telling me the matrices are not adding up but I am not understanding where is the problem. Python清楚地告诉我矩阵没有加在一起,但是我不明白问题出在哪里。

Any tips? 有小费吗?

PS: The full code is pasted ħere . PS:完整的代码粘贴在erer上

layer1 = neuronLayer(30, 94936)    # 29 neurons with 227908 inputs
layer2 = neuronLayer(1, 30)         # 1 Neuron with the previous 29 inputs

where `nueronLayer creates NueronLayer在哪里创建

self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1 

the 2 weights are (94936,30) and (30,1) in size. 2个权重的大小分别为(94936,30)和(30,1)。

This line does not make any sense. 这条线没有任何意义。 I surprised it doesn't give an error 我很惊讶它没有给出错误

layer1error = layer2delta.dot(self.layer2.weights.np.transpose)

I suspect you want np.transpose(self.layer2.weights) or self.layer2.weights.T . 我怀疑你想要np.transpose(self.layer2.weights)self.layer2.weights.T

But maybe it doesn't get there. 但是也许它没有到达那里。 train first calls think with a (94936,30) inputs train第一个电话以(94936,30)的inputs think

    outputLayer1 = self.__sigmoid(np.dot(inputs, self.layer1.weights))
    outputLayer2 = self.__sigmoid(np.dot(outputLayer1, self.layer2.weights))

So it tries to do a np.dot with 2 (94936,30), (94936,30) arrays. 因此,它尝试使用2个(94936,30),(94936,30)数组创建一个np.dot They aren't compatible for a dot. 它们与点不兼容。 You could transpose one or the other, producing either (94936,94936) array or (30,30). 您可以换位,产生(94936,94936)数组或(30,30)。 One looks too big. 一个看起来太大了。 The (30,30) is compatible with the weights for the 2nd layer. (30,30)与第二层的重量兼容。

np.dot(inputs.T, self.layer1.weights)

has a chance of working right. 有工作的机会。

np.dot(outputLayer1, self.layer2.weights)
(30,30) with (30,1) => (30,1)

But then you do 但是你呢

train_outputs - outputLayer2

That will have problems regardless of whether train_outputs is (94936,) or (94936,1) 不管train_outputs是(94936,)还是(94936,1),都会出现问题。

You need to make sure that arrays shapes flow correctly through the calculation. 您需要确保数组形状在计算过程中正确流动。 Don't just check them at the start. 不要一开始就检查它们。 Check then internally. 然后内部检查。 And make you sure you understand what shapes they should have at each step. 并确保您了解它们在每个步骤中应具有的形状。

It would be a whole lot easier to develop and test this code with much smaller inputs and layers, something like 10 samples and 3 features. 使用更少的输入和层(例如10个示例和3个功能)来开发和测试此代码将容易得多。 That way you can look at the values as well as the shapes. 这样,您可以查看值和形状。

np.dot uses matrix multiplication when its arguments are matrices. 当np.dot的参数为矩阵时,将使用矩阵乘法。 It looks like your code is trying to multiply two non-square matrices together with the same dimensions which doesn't work. 看来您的代码正在尝试将两个具有相同尺寸的非平方矩阵相乘,这是行不通的。 Perhaps you meant to transpose one of the matrices? 也许您是要转置其中一个矩阵? Numpy matrices have a T property that returns the transpose, you could try: numpy矩阵具有返回转置的T属性,您可以尝试:

self.__sigmoid(np.dot(inputs.T, self.layer1.weights))

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

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