我正在嘗試用 python 做一些密集的神經網路機器學習。我在完成代碼以計算權重和偏差的輸出時遇到問題。當我*在某個索引處的權重和矩陣元素之間應用運算元時,我得到的錯誤是ValueError: operands could not be broadcast together with shapes (100,784) (1000,784,1). 我是否對回圈應用了錯誤的索引,或者我做錯了什么,請幫忙。
##initialize the training and test arrays
train=np.empty((1000,28,28),dtype='float64')
trainY=np.zeros((1000,10,1))
test=np.empty((10000,28,28), dtype='float64')
testY=np.zeros((10000,10,1))
##reading image data into the training array
#load the images
i=0
for filename in os.listdir('Data/Training1000/'):
y=int(filename[0])
trainY[i,y]=1.0
train[i]=cv2.imread('Data/Training1000/{0}'.format(filename),0)/255.0
i =1
##reading image data into the testing array
i=0
for filename in os.listdir('Data/Test10000'):
y = int(filename[0])
testY[i,y] = 1.0
test[i] = cv2.imread('Data/Test10000/{0}'.format(filename),0)/255.0
i=i 1
##reshape the training and testing arrays
trainX = train.reshape(train.shape[0],train.shape[1]*train.shape[2],1)
testX = test.reshape(test.shape[0],test.shape[1]*test.shape[2],1)
##section to declare the weights and the biases
w1 = np.random.uniform(low=-0.1,high=0.1,size=(numNeuronsLayer1,784))
b1 = np.random.uniform(low=-1,high=1,size=(numNeuronsLayer1,1))
w2 = np.random.uniform(low=-0.1,high=0.1,size=(numNeuronsLayer2,numNeuronsLayer1))
b2 = np.random.uniform(low=-0.1,high=0.1,size=(numNeuronsLayer2,1))
##declare the hidden layers
numNeuronsLayer1=100
numNeuronsLayer2=10
numEpochs=100
##declare a learning rate
learningRate = 0.1;
##do the forward pass on the weights and the biases
for n in range(0,numEpochs):
loss=0
trainX,trainY = shuffle(trainX, trainY)
for i in range(trainX.shape[0]):
##this is where I have a problem, the line below throws the error described above
##my first pass is declared a2
a2=w1*train[i] w2*trainX[i] b1
我如何在上面的回圈中正確參考我的訓練變數以消除廣播錯誤,謝謝。
uj5u.com熱心網友回復:
你非常接近,但有幾個問題。首先,您需要進行矩陣乘法。*將進行逐元素乘法(即,np.array([1,2,3]) * np.array([2,3,4]) = np.array([2,6,12])。要進行矩陣乘法,numpy您可以使用@運算子(即,matrix1 @ matrix2)或使用np.matmul函式。
您的另一個問題是輸入的形狀。我不知道為什么要添加第三個尺寸(1在末尾train.reshape(train.shape[0],train.shape[1]*train.shape[2],1),你應該罰款保持它作為一個矩陣(改變它train.reshape(train.shape[0],train.shape[1]*train.shape[2]),改變test.reshape相應。
最后,你的推理線有點偏離: a2=w1*train[i] w2*trainX[i] b1
你首先必須計算a1之前a2。矩陣乘法的一個重要部分是內部維度必須一致(即,您不能將形狀 [100,50] 和 [100, 50] 的矩陣相乘,但可以將形狀 [100,50] 和 [50, 60] 的矩陣相乘,矩陣乘積的最終形狀是每個矩陣的外部索引,在這種情況下 [100,60])。作為矩陣乘法的結果,您還可以擺脫圍繞訓練示例的 for 回圈。所有的例子都是同時計算的。所以要計算a1,我們需要轉置我們的w1并將其作為右手變數。
a1 = ( trainX @ w1.transpose() ) b1.transpose()
那么我們可以計算a2為a1
a2 = ( a1 @ w2.transpose() ) b2.transpose()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381663.html
