Skip to content

Instantly share code, notes, and snippets.

@domluna
Created January 19, 2017 00:43
Show Gist options
  • Save domluna/8808192b38194f2b210ec3d6459824bc to your computer and use it in GitHub Desktop.
Save domluna/8808192b38194f2b210ec3d6459824bc to your computer and use it in GitHub Desktop.
import tensorflow as tf
import numpy as np
w = np.arange(1, 10, dtype=np.float32).reshape((3,3,1,1))
f = tf.Variable(tf.constant(w))
input = tf.placeholder(tf.float32, (None, 28, 28, 1))
conv = tf.nn.conv2d(input, f, [1,2,2,1], 'VALID')
s = tf.Session()
x = np.zeros((28, 28), dtype=np.float32)
# rightmost 3x3 block
x[0:3, 25:28] = 1.0
# leftmost 3x3 block
x[0:3, 0:3] = 1.0
s.run(tf.global_variables_initializer())
out = s.run(conv, {input: x.reshape((1,28,28,1))})
print(w.reshape((3,3)))
print(x.reshape((28, 28))[:3, :])
print(out.shape)
nh, nw = out.shape[1], out.shape[2]
print(out.reshape((nh,nw))[0, :])
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment