簡體   English   中英

如何使用 python 計算這些權重和偏差的正向傳遞輸出

[英]How to compute the forward pass outputs for these weights and biases with python

我正在嘗試使用 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
    

如何在上面的循環中正確引用我的訓練變量以消除廣播錯誤,謝謝。

你非常接近,但有幾個問題。 首先,您需要進行矩陣乘法。 *將進行元素乘法(即np.array([1,2,3]) * np.array([2,3,4]) = np.array([2,6,12]) 。使用numpy進行矩陣乘法,您可以使用@運算符(即 matrix1 matrix1 @ matrix2 matrix2 )或使用np.matmul function。

你的另一個問題是你的輸入的形狀。 我不確定你為什么要添加第三維( train.reshape(train.shape[0],train.shape[1]*train.shape[2],1) 1你應該沒問題將其保留為矩陣(將其更改為train.reshape(train.shape[0],train.shape[1]*train.shape[2]) ,相應地更改test.reshape

最后,您的推理線有點偏離: a2=w1*train[i]+w2*trainX[i]+b1

您首先必須在a2之前計算a1 矩陣乘法的一個重要部分是內部維度必須一致(即,您不能將形狀 [100,50] 和 [100, 50] 的矩陣相乘,但可以將形狀 [100,50] 和 [50, 60] 的矩陣相乘,矩陣乘積的最終形狀是每個矩陣的外部指標,在本例中為 [100,60])。 作為矩陣乘法的結果,您還可以擺脫圍繞訓練示例的 for 循環。 所有的例子都是同時計算的。 因此,要計算a1 ,我們需要轉置w1並將其作為右手變量。

a1 = ( trainX @ w1.transpose() ) + b1.transpose()

那么我們可以將a2計算為a1的 function

a2 = ( a1 @ w2.transpose() ) + b2.transpose()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM