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
class Base(object): | |
def __init__(self, a): | |
self._a = a | |
class Derived(Base): | |
def __init__(self, a={"a": 3}, b=10): | |
super().__init__(a) | |
self._b = b | |
def update_a(self, a): |
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
tf.reset_default_graph() | |
input_image = tf.placeholder(tf.float32, shape=[X_train_prep.shape[0], X_train_prep.shape[1]]) | |
U = tf.Variable(tf.truncated_normal([X_train_prep.shape[0], hidden_dim], stddev=0.01), name="U") | |
V = tf.Variable(tf.truncated_normal([hidden_dim, initial_dim], stddev=0.01), name="V") | |
approximation_loss = tf.sqrt(tf.reduce_sum(tf.square(tf.sub(input_image, tf.matmul(U, V))))) | |
# Define optimizer and other constants | |
init = tf.initialize_all_variables() | |
sess = tf.InteractiveSession() |
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
def get_P(n): | |
val = [1, 2, 1] * (n / 2) | |
col = [] | |
for i in xrange(n / 2): | |
col += 3*[i] | |
row = [] | |
for j in xrange((n-1)/2): | |
row += range(2*j, 2*j + 3) | |
P = scsp.csr_matrix((val, (row, col)), shape=(n, (n-1)/2)) | |
return 0.5 * P |
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
k = 256 #number of clusters | |
c = 4 #number of subspaces | |
R = np.eye(m, m) | |
opq_codes = np.zeros((n, c)) | |
opq_centroids = np.zeros((k, m/c, c)) | |
iterations = 30 | |
for it in xrange(iterations): | |
print "Num iter", it | |
mod_X = np.dot(X, R) | |
opq_Y = np.zeros((n, m)) |
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
def compute_grad_metric(w, x1, x2, label1, label2): | |
y = label1 == label2 | |
dx = (x1 - x2).reshape(64, 1) | |
mod_x = w.dot(dx) | |
if y: | |
return 2 * np.dot(w, np.dot(dx, dx.T)) | |
elif (1 - np.dot(mod_x.T, mod_x) > 0): | |
return -2 * np.dot(w, np.dot(dx, dx.T)) | |
else: |
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
# First variant | |
term_1 = x | |
for k in xrange(2): | |
print "Num iteration", k | |
Wx = x | |
for i in xrange(k+1): | |
Wx = W.dot(Wx) | |
WTWx = Wx | |
for i in xrange(k+1): | |
WTWx = W.T.dot(WTWx) |
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
def process_line(line, t2i, i2t, outfile): | |
''' | |
Find all person's ID, They have to link to the category | |
'Living_people' | |
''' | |
pattern = "\((\d+),'(.*?)',(.*?)\)" | |
current_page = None | |
for match in finditer(pattern, line): | |
topage, category, t = match.groups() | |
if category == "Living_people": |
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
# First version: len(person_id) = 15352 | |
person_id = {} | |
with open(in_dir + 'ID-title_dict.pickle', 'rb') as f: | |
id2title = cPickle.load(f) | |
with open(in_dir + 'person_id.txt') as f: | |
for p_id in f: | |
try: | |
t = id2title[int(p_id)] | |
person_id[int(p_id)] = 0 | |
except KeyError: |
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
class MyVector { | |
int* vector; | |
int size; | |
public: | |
MyVector() : vector(new int[1]), size(1) { } | |
MyVector(int size) { | |
vector = new int[size]; | |
this->size = size; | |
} | |
MyVector(int size, int num) { |