Skip to content

Commit ae3ef36

Browse files
committed
add demo file
1 parent dd846d7 commit ae3ef36

File tree

6 files changed

+175
-37
lines changed

6 files changed

+175
-37
lines changed

experiment/demo.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import sys
6+
import pickle
7+
8+
import numpy as np
9+
from six.moves import xrange # python2/3 compatible
10+
import tensorflow as tf
11+
import string
12+
import scipy
13+
import scipy.sparse as sparse
14+
import os
15+
16+
# import code of this project
17+
sys.path.insert(0, '../util/')
18+
from util import config_to_name
19+
sys.path.insert(0, '../model/')
20+
from embedding import fit_emb
21+
from embedding import evaluate_emb
22+
from embedding import dense_array_feeder
23+
from embedding import sparse_array_feeder
24+
from random_data import rand_data
25+
26+
def embedding_experiment(config, dataset):
27+
np.random.seed(seed=27)
28+
29+
## Step 1: load data
30+
print('Generating a dataset ...')
31+
32+
data = rand_data() # the training/test dataset generated by rand_data has two fields, but only 'scores' are needed here
33+
34+
trainset = data['trainset']['scores']
35+
testset = data['testset']['scores']
36+
37+
"""
38+
trainset: scores: a sparse matrix, each ij entry is the rating of movie j given by person i, or the count of item j in basket i
39+
testset: [same structure as trainset]
40+
"""
41+
42+
# one can always redefine zie.generate_batch(reviews, rind) to use other format of trainset and testset
43+
44+
print('The training set has %d rows and %d columns, and the test set has %d rows' %
45+
(trainset.shape[0], trainset.shape[1], testset.shape[0]))
46+
47+
48+
49+
50+
# batch_feeder is a function, which will be executed as batch_feeder(trainset[i])
51+
# its output will be fed into tf place holders
52+
batch_feeder = sparse_array_feeder
53+
54+
# fit an emb model
55+
print('Training set has size: ', trainset.shape)
56+
emb_model, logg = fit_emb(trainset, batch_feeder, config)
57+
print('Training done!')
58+
59+
print('Test set has size: ', testset.shape)
60+
test_llh = evaluate_emb(testset, batch_feeder, emb_model, config)
61+
print('Testing done!')
62+
63+
# Save result
64+
print('Check result...')
65+
emb_vec = emb_model['alpha']
66+
print('Embedding matrix has shape ', emb_vec.shape)
67+
# Save wherever you want
68+
69+
print('Done!')
70+
71+
if __name__ == '__main__':
72+
73+
dataset = 'random'
74+
dist = 'poisson'
75+
max_iter = 500
76+
nprint = 100
77+
78+
config = dict(
79+
# the dimensionality of the embedding vectors
80+
K=50,
81+
# the embedding distribution 'poisson' or 'binomial' (N=3)
82+
dist=dist,
83+
# ratio of negative samples. if there are N0 zeros in one row, only sample (0.1 * N0) from these zero,
84+
# it is equivalent to downweight zero-targets with weight 0.1
85+
neg_ratio=0.1,
86+
# number of optimization iterations
87+
max_iter=max_iter,
88+
# number of iterations to print objective, training log-likelihood, and validation log-likelihood, and debug values
89+
nprint=nprint,
90+
# weight for regularization terms of embedding vectors
91+
ar_sigma2=1,
92+
# uncomment the following line to use the base model
93+
#model='base',
94+
# uncomment the following line to use context selection. Only the prior 'fixed_bern' works for now
95+
model='context_select', prior='fixed_bern', nsample=30, hidden_size=[30, 15], histogram_size=40, nsample_test=1000, selsize=10,
96+
)
97+
98+
print('The configuration is: ')
99+
print(config)
100+
101+
embedding_experiment(config, dataset)
102+
103+
104+

experiment/random_data.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
from scipy import sparse
3+
import pickle
4+
5+
'''
6+
Generate a random copy of data
7+
8+
input:
9+
output: dict with two fields:
10+
trainset: dict with two fields
11+
scores: a sparse matrix, each ij entry is the rating of movie j given by person i, or the count of item j in basket i
12+
atts : a matrix, each row is a feature vector extracted from person i, or basket i
13+
testset : [same structure as test set]
14+
15+
'''
16+
17+
def rand_data():
18+
n_rows = 200
19+
n_columns = 50
20+
n_feat = 5
21+
22+
23+
np.random.seed(27)
24+
# allocate more rows than necessary, to make sure each row has at least 2 non-zero entries
25+
score_mat = np.random.rand(n_rows * 2, n_columns)
26+
27+
score_mat[score_mat < 0.88] = 0
28+
score_mat[np.logical_and(0.96 <= score_mat, score_mat < 1)] = 3
29+
score_mat[np.logical_and(0.92 <= score_mat, score_mat < 0.96)] = 2
30+
score_mat[np.logical_and(0.88 <= score_mat, score_mat < 0.92)] = 1
31+
32+
33+
row_sum = np.sum(score_mat > 0, axis=1)
34+
score_mat = score_mat[row_sum >= 2, ]
35+
score_mat = score_mat[0 : n_rows, ]
36+
37+
feature = np.random.rand(n_rows, n_feat)
38+
39+
trainset = dict(scores=sparse.csr_matrix(score_mat[0:(n_rows / 2)]), atts=feature[0:(n_rows / 2)])
40+
testset = dict(scores=sparse.csr_matrix(score_mat[(n_rows / 2):]), atts=feature[(n_rows / 2):])
41+
42+
return dict(trainset=trainset, testset=testset)
43+
44+
45+
46+

model/conbernarray.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def logprob(logits, samples):
5757

5858
logprob_cbs = logprob_unc - tf.expand_dims(logprob_non0, 1) # expand to samples
5959

60-
check_point = tf.assert_less(tf.reduce_mean(logprob_cbs), 0.001, data=[tf.reduce_mean(logprob_cbs), tf.reduce_mean(logits), tf.reduce_mean(samples)])
61-
with tf.control_dependencies([check_point]):
62-
logprob_cbs = tf.identity(logprob_cbs)
60+
#check_point = tf.assert_less(tf.reduce_mean(logprob_cbs), 0.001, data=[tf.reduce_mean(logprob_cbs), tf.reduce_mean(logits), tf.reduce_mean(samples)])
61+
#with tf.control_dependencies([check_point]):
62+
# logprob_cbs = tf.identity(logprob_cbs)
6363

6464

6565
return logprob_cbs
@@ -135,9 +135,9 @@ def sample(logits, nsample):
135135
samples = samples * trunc_mask + trunc_flag
136136

137137

138-
check_point = tf.assert_greater(tf.reduce_mean(samples), 0.0, data=[tf.reduce_mean(logits), tf.reduce_mean(samples)])
139-
with tf.control_dependencies([check_point]):
140-
samples = tf.identity(samples)
138+
#check_point = tf.assert_greater(tf.reduce_mean(samples), 0.0, data=[tf.reduce_mean(logits), tf.reduce_mean(samples)])
139+
#with tf.control_dependencies([check_point]):
140+
# samples = tf.identity(samples)
141141

142142
return samples
143143

model/embedding.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_model(model_param, session, config):
6464

6565

6666

67-
def fit_emb(reviews, batch_feeder, config, save_path):
67+
def fit_emb(reviews, batch_feeder, config):
6868

6969
do_log_save = False
7070
do_profiling = False
@@ -171,8 +171,6 @@ def fit_emb(reviews, batch_feeder, config, save_path):
171171

172172

173173
model = get_model(model_param, session, config)
174-
save_file = save_path + ('iter%d' % step) + config_to_name(config) + '.pkl'
175-
pickle.dump(dict(model=model, logg=train_logg), open(save_file, "wb"))
176174

177175
if do_log_save:
178176
tf.train.Saver().save(session, log_save_path, step)
@@ -232,7 +230,7 @@ def evaluate_emb(reviews, batch_feeder, model, config):
232230
return dict(pos_llh=pos_llh, neg_llh=neg_llh)
233231

234232
def sparse_array_feeder(batch):
235-
_, nz_ind, values = scipy.sparse.find(batch)
233+
_, nz_ind, values = sparse.find(batch)
236234
return nz_ind, values
237235

238236

model/graph_builder.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ def sample_negatives(self, context, config):
3737
cat_dist = tf.contrib.distributions.Categorical(probs=prob)
3838
sample = cat_dist.sample(nneg)
3939

40-
# sanity check
41-
context_zero = tf.assert_equal(tf.gather(prob, context), 0.0)
42-
other_prob = tf.assert_equal(tf.reduce_sum(tf.cast(tf.abs(prob - (1.0 / normalizer)) < 1e-6, tf.int32)), \
43-
movie_size - ncontext)
44-
with tf.control_dependencies([context_zero, other_prob]):
45-
sample = tf.identity(sample)
46-
4740
return sample
4841

4942
def log_dist_prob(self, target, target_label, emb_score, config, zero_labels=False):
@@ -198,7 +191,9 @@ def calculate_bernoulli_logpb(self, target, context, is_same_set, comb, comb_bin
198191
def calculate_noisy_elbo(self, target, target_label, context, context_label, is_same_set, training, config):
199192

200193
if is_same_set:
201-
with tf.control_dependencies([tf.assert_greater(tf.shape(context)[0], 2)]):
194+
# if is_same_set, the variable "context" here contains both the index of the target item and also indices of context items.
195+
# it need to has at least 2 elements, otherwise the target item has no context items, and such row should be removed.
196+
with tf.control_dependencies([tf.assert_greater(tf.shape(context)[0], 1)]):
202197
context = tf.identity(context)
203198

204199
# generate configurations

model/inference_network.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,23 @@ def build_network(self, target_label, context_scores, b_logit, is_same_set, nsam
157157

158158
logits = logits + b_logit
159159

160-
if is_same_set:
161-
self.debug_var.append(h_fc1)
162-
self.debug_var.append(h_fc2)
163-
self.debug_var.append(logits)
164-
165-
# assert logits is not nan or -inf
166160
if is_same_set: # set the diagnal to be negative so that 1) diagnal element of a sample is always 0 2) it does not have gradient
167161
logits = tf.matrix_set_diag(logits, tf.ones([ntarget]) * (-50.0))
168162

169-
check_point = tf.assert_greater(tf.reduce_mean(logits), -10000.0, data=[tf.reduce_mean(logits),
170-
tf.reduce_mean(context_scores),
171-
tf.reduce_mean(b_logit),
172-
tf.reduce_mean(feat),
173-
tf.reduce_mean(self.W_fc1),
174-
tf.reduce_mean(self.W_fc2),
175-
tf.reduce_mean(self.W_fc3),
176-
tf.reduce_mean(self.b_fc1),
177-
tf.reduce_mean(self.b_fc2),
178-
tf.reduce_mean(self.b_fc3)
179-
])
180-
with tf.control_dependencies([check_point]):
181-
logits = tf.identity(logits)
163+
# assert logits is not nan or -inf
164+
#check_point = tf.assert_greater(tf.reduce_mean(logits), -10000.0, data=[tf.reduce_mean(logits),
165+
# tf.reduce_mean(context_scores),
166+
# tf.reduce_mean(b_logit),
167+
# tf.reduce_mean(feat),
168+
# tf.reduce_mean(self.W_fc1),
169+
# tf.reduce_mean(self.W_fc2),
170+
# tf.reduce_mean(self.W_fc3),
171+
# tf.reduce_mean(self.b_fc1),
172+
# tf.reduce_mean(self.b_fc2),
173+
# tf.reduce_mean(self.b_fc3)
174+
# ])
175+
#with tf.control_dependencies([check_point]):
176+
# logits = tf.identity(logits)
182177

183178
samples = cba.sample(logits, nsample)
184179
logprob = cba.logprob(logits, samples)

0 commit comments

Comments
 (0)