keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.92k stars 19.45k forks source link

Convolution2D's output doesn't match with naive implementation of convolution. #3179

Closed zaburo-ch closed 8 years ago

zaburo-ch commented 8 years ago

I built a small CNN, and checked its output by comparing with the output of naive implementation of convolution. But these outputs don't match. Something wrong my understanding of naive implementation?

I used Keras==1.0.5 and theano==0.8.2. my code is as below.

net = Sequential()
net.add(Convolution2D(1, 5, 5, input_shape=(1, 10, 10)))
net.compile('sgd', 'mse')

X = np.random.randint(0, 2, 10*10).reshape([1, 1, 10, 10])
y = net.predict(X)[0][0]

# naive implementation
w, b = net.layers[0].get_weights()
w, b, X = w[0][0], b[0], X[0][0]
y_hat = np.zeros(y.shape)
for i in range(y_hat.shape[0]):
    for j in range(y_hat.shape[1]):
        y_hat[i, j] = (w * X[i:i+5, j:j+5]).sum() + b

y_hat - y  # large error
zaburo-ch commented 8 years ago

Thank you, I solved the problem myself. my code had a mistake.

y_hat[i, j] = (w * X[i:i+5, j:j+5][::-1, ::-1]).sum() + b
alyato commented 8 years ago

@zaburo-ch Does the net.layers[0].get_weights() return two value (w,b)?