Skip to content

Instantly share code, notes, and snippets.

@1duo
Last active July 21, 2018 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1duo/7344a3c761bcd17c7d71de5a95e047e1 to your computer and use it in GitHub Desktop.
Save 1duo/7344a3c761bcd17c7d71de5a95e047e1 to your computer and use it in GitHub Desktop.
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment