Skip to content

Instantly share code, notes, and snippets.

@rjurney rjurney/pad.py
Created Oct 22, 2019

Embed
What would you like to do?
Custom padding of dense vectors with min/max or mean
padded_posts = []
for post in encoded_docs:
# Pad short posts with alternating min/max
if len(post) < MAX_LENGTH:
# Method 1
pointwise_min = np.minimum.reduce(post)
pointwise_max = np.maximum.reduce(post)
padding = [pointwise_max, pointwise_min]
# Method 2
pointwise_avg = np.mean(post)
padding = [pointwise_avg]
post += padding * ceil((MAX_LENGTH - len(post) / 2.0))
# Shorten long posts or those odd number length posts we padded to 51
if len(post) > MAX_LENGTH:
post = post[:MAX_LENGTH]
# Add the post to our new list of padded posts
padded_posts.append(post)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.