python中数组的维度_Python数组维度问题
我再次在使用PythonNumPy和數組以在矩陣之間進行一些計算的過程中苦苦掙扎。
可能無法正常工作的代碼部分如下:
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]
當打印這些矩陣信息(np.ndim,np.shape和dtype分別),這是你會得到什么:
2
1
2
1
2
1
(94936, 30)
(94936,)
(94936, 30)
(94936,)
(94935, 30)
(94935,)
float64
float64
float64
float64
float64
float64
我相信它在所有*_output數組中都缺少1維。
我需要的另一個矩陣是通過以下命令創建的:
newMatrix = neuronLayer(30, 94936)
在其中neuronLayer定義為的類:
class neuronLayer():
def __init__(self, neurons, neuron_inputs):
self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1
這是最終的輸出:
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清楚地告訴我矩陣沒有加在一起,但是我不明白問題出在哪里。
有小費嗎?
PS:完整的代碼粘貼到。
解決方案
layer1 = neuronLayer(30, 94936) # 29 neurons with 227908 inputs
layer2 = neuronLayer(1, 30) # 1 Neuron with the previous 29 inputs
NueronLayer在哪里創建
self.weights = 2 * np.random.random((neuron_inputs, neurons)) - 1
2個權重的大小分別為(94936,30)和(30,1)。
這條線沒有任何意義。我很驚訝它沒有給出錯誤
layer1error = layer2delta.dot(self.layer2.weights.np.transpose)
我懷疑你想要np.transpose(self.layer2.weights)還是self.layer2.weights.T。
但是也許它沒有到達那里。train首次致電think(94936,30)inputs
outputLayer1 = self.__sigmoid(np.dot(inputs, self.layer1.weights))
outputLayer2 = self.__sigmoid(np.dot(outputLayer1, self.layer2.weights))
因此,它嘗試對np.dot2個(94936,30),(94936,30)數組進行a運算。它們與點不兼容。您可以換位,產生(94936,94936)數組或(30,30)。一個看起來太大了。(30,30)與第二層的重量兼容。
np.dot(inputs.T, self.layer1.weights)
有工作的機會。
np.dot(outputLayer1, self.layer2.weights)
(30,30) with (30,1) => (30,1)
但是你呢
train_outputs - outputLayer2
不管train_outputs是(94936,)還是(94936,1)都會有問題
您需要確保數組形狀在計算過程中正確流動。不要一開始就檢查它們。然后內部檢查。并確保您了解它們在每個步驟中應具有的形狀。
使用更少的輸入和層(例如10個示例和3個功能)來開發和測試此代碼將容易得多。這樣,您可以查看值和形狀。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python中数组的维度_Python数组维度问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab读入txt数据_教程合集 |
- 下一篇: i云保CSR理念是什么?