%matplotlib inline
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
sess = tf.InteractiveSession()
image = np.array([[[[1],[2]],
[[3],[4]]]], dtype=np.float32)
print(image.shape)
plt.imshow(image.reshape(2,2), cmap='Greys')
(1, 2, 2, 1)
<matplotlib.image.AxesImage at 0x10e242da0>
weight.shape = 2 filters (1 , 1 , 1)
(TensorFlow For Machine Intelligence: A hands-on introduction to learning algorithms
by Sam Abrahams et al.)
print("imag:\n", image)
weight = tf.constant([[[[2., 0.5]]]])
print("weight.shape", weight.shape)
conv2d = tf.nn.conv2d(image, weight, strides=[1, 1, 1, 1], padding='SAME')
conv2d_img = conv2d.eval()
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_img in enumerate(conv2d_img):
print(one_img.reshape(2,2))
plt.subplot(1,2,i+1), plt.imshow(one_img.reshape(2,2), cmap='gray')
imag: [[[[ 1.] [ 2.]] [[ 3.] [ 4.]]]] weight.shape (1, 1, 1, 2) [[ 2. 4.] [ 6. 8.]] [[ 0.5 1. ] [ 1.5 2. ]]
image = np.array([[[[4],[3]],
[[2],[1]]]], dtype=np.float32)
pool = tf.nn.max_pool(image, ksize=[1, 2, 2, 1],
strides=[1, 1, 1, 1], padding='VALID')
print(pool.shape)
print(pool.eval())
(1, 1, 1, 1) [[[[ 4.]]]]
image = np.array([[[[4],[3]],
[[2],[1]]]], dtype=np.float32)
pool = tf.nn.max_pool(image, ksize=[1, 2, 2, 1],
strides=[1, 1, 1, 1], padding='SAME')
print(pool.shape)
print(pool.eval())
(1, 2, 2, 1) [[[[ 4.] [ 3.]] [[ 2.] [ 1.]]]]
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
img = mnist.train.images[0].reshape(28,28)
plt.imshow(img, cmap='gray')
<matplotlib.image.AxesImage at 0x115029ac8>
sess = tf.InteractiveSession()
img = img.reshape(-1,28,28,1)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 5], stddev=0.01))
conv2d = tf.nn.conv2d(img, W1, strides=[1, 2, 2, 1], padding='SAME')
print(conv2d)
sess.run(tf.global_variables_initializer())
conv2d_img = conv2d.eval()
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_img in enumerate(conv2d_img):
plt.subplot(1,5,i+1), plt.imshow(one_img.reshape(14,14), cmap='gray')
Tensor("Conv2D_1:0", shape=(1, 14, 14, 5), dtype=float32)
pool = tf.nn.max_pool(conv2d, ksize=[1, 2, 2, 1], strides=[
1, 2, 2, 1], padding='SAME')
print(pool)
sess.run(tf.global_variables_initializer())
pool_img = pool.eval()
pool_img = np.swapaxes(pool_img, 0, 3)
for i, one_img in enumerate(pool_img):
plt.subplot(1,5,i+1), plt.imshow(one_img.reshape(7, 7), cmap='gray')
Tensor("MaxPool_2:0", shape=(1, 7, 7, 5), dtype=float32)