Feb 17, 2018

Very very simple example of TensorFlow conv1d.

import tensorflow as tf
import numpy as np

X = np.zeros((10,10)) + 1
filter = np.zeros((5,),dtype=np.float32) + 1

x = tf.placeholder(tf.float32, shape=[None, 10, 1])
f = tf.placeholder(tf.float32, shape = [5, 1, 1])

conv = tf.nn.conv1d(x, f, 1,'SAME')

session = tf.Session()
result = session.run(conv, feed_dict={x:X.reshape((10,10,1)), f:filter.reshape((5,1,1))})
result.reshape((10,10))

result

array([[ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.],
       [ 3.,  4.,  5.,  5.,  5.,  5.,  5.,  5.,  4.,  3.]], dtype=float32)
You need to change 10x10 input to 10x10x1 array. (last 1 is for channel, and I only have one channel.)
Also 5 linear filter to 5x1x1 array. (middle 1 for input channel and last 1 for output channel.)

No comments: