Skip to content

Instantly share code, notes, and snippets.

@Bollegala
Last active August 29, 2015 14:12
Show Gist options
  • Save Bollegala/b06132f617e831909793 to your computer and use it in GitHub Desktop.
Save Bollegala/b06132f617e831909793 to your computer and use it in GitHub Desktop.
Some handy tricks/tips in Python.
"""
Here are some useful tips/tricks in Python.
Credit: https://gist.github.com/neubig
"""
# To assign unique inter ids to words do the following.
from collections import defaultdict
wids = defaultdict(lambda: len(wids))
# Now when you check for a word in wids, if it does not exist then it will be added
# after assigning the incremented integer ids.
# Softmax function
def softmax(x):
e = np.exp(x - np.max(x)) # prevent overflow
return e / np.sum(e)
# Tanh and derivative
def tanh_prime(x):
y = np.tanh(x)
y_prime = 1 - (y * y)
return y, y_prime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment