# create output tensor after convolution layer
--> 521 out = np.zeros((N, F, outH, outW))
522
523 # padding all input data
TypeError: 'float' object cannot be interpreted as an integer
我正在关注 cs231n 斯坦福教程作业 2 第 4 部分,我遇到了这个问题。
代码是:
N, C, H, W = x.shape
F, C, FH, FW = w.shape
assert (H - FH + 2 * pad) % stride == 0
assert (W - FW + 2 * pad) % stride == 0
outH = 1 + (H - FH + 2 * pad) / stride
outW = 1 + (W - FW + 2 * pad) / stride
# create output tensor after convolution layer
out = np.zeros((N, F, outH, outW))
# padding all input data
x_pad = np.pad(x, ((0,0), (0,0),(pad,pad),(pad,pad)), 'constant')
H_pad, W_pad = x_pad.shape[2], x_pad.shape[3]
# create w_row matrix
w_row = w.reshape(F, C*FH*FW) #[F x C*FH*FW]
# create x_col matrix with values that each neuron is connected to
x_col = np.zeros((C*FH*FW, outH*outW)) #[C*FH*FW x H'*W']
for index in range(N):
neuron = 0
for i in range(0, H_pad-FH+1, stride):
for j in range(0, W_pad-FW+1,stride):
x_col[:,neuron] = x_pad[index,:,i:i+FH,j:j+FW].reshape(C*FH*FW)
neuron += 1
out[index] = (w_row.dot(x_col) + b.reshape(F,1)).reshape(F, outH, outW)
###########################################################################
# END OF YOUR CODE #
###########################################################################
cache = (x_pad, w, b, conv_param)
return out, cache