Skip to content

Instantly share code, notes, and snippets.

@talolard
Created May 22, 2017 05:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save talolard/1e51ff3946649f687b4059ea5b92786c to your computer and use it in GitHub Desktop.
Save talolard/1e51ff3946649f687b4059ea5b92786c to your computer and use it in GitHub Desktop.
An example of how to do conv1d ourself in Tensorflow
import tensorflow as tf
def conv1d(input_, output_size, width, stride):
'''
:param input_: A tensor of embedded tokens with shape [batch_size,max_length,embedding_size]
:param output_size: The number of feature maps we'd like to calculate
:param width: The filter width
:param stride: The stride
:return: A tensor of the concolved input with shape [batch_size,max_length,output_size]
'''
inputSize = input_.get_shape()[-1] # How many channels on the input (The size of our embedding for instance)
#This is the kicker where we make our text an image of height 1
input_ = tf.expand_dims(input_, axis=1) # Change the shape to [batch_size,1,max_length,output_size]
#Make sure the height of the filter is 1
filter_ = tf.get_variable("conv_filter",shape=[1, width, inputSize, output_size])
#Run the convolution as if this were an image
convolved = tf.nn.conv2d(input_, filter=filter_,strides=[1, 1, stride, 1], padding="SAME")
#Remove the extra dimension, eg make the shape [batch_size,max_length,output_size]
result = tf.squeeze(convolved, axis=1)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment