【发布时间】:2022-01-17 01:09:42
【问题描述】:
我正在尝试使用 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
如何在上面的循环中正确引用我的训练变量以消除广播错误,谢谢。
【问题讨论】:
标签: python arrays machine-learning