Last active
August 29, 2015 14:12
-
-
Save Bollegala/b06132f617e831909793 to your computer and use it in GitHub Desktop.
Some handy tricks/tips in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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