import numpy as np
import tensorflow as tf
# np.set_printoptions(threshold=np.nan)
def run(x, w, pad='SAME', stride=(1, 1)):
xx = tf.constant(x, dtype='float32')
ww = tf.constant(w, dtype='float32')
yy = tf.nn.conv2d(xx, ww, strides=[1, stride[0], stride[1], 1], padding=pad)
with tf.Session() as sess:
out = yy.eval().ravel()
return out
if __name__ == '__main__':
np.random.seed(0)
# input [batch, in_height, in_width, in_channels]
x = np.random.rand(1, 224, 224, 3).astype('float32')
# filter [filter_height, filter_width, in_channels, out_channels]
w = np.random.rand(3, 3, 3, 32).astype('float32')
out = run(x, w)
print(out.shape)
print(out)
$ python validate-conv2d.py
(1605632,)
[3.6909204 3.6372736 3.3944104 ... 3.8938167 2.8258123 4.4820447]