forked from keroro824/HashingDeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_full_softmax.py
More file actions
executable file
Β·114 lines (110 loc) Β· 4.9 KB
/
example_full_softmax.py
File metadata and controls
executable file
Β·114 lines (110 loc) Β· 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import numpy as np
import time
import math
import os
import glob
import tensorflow as tf
from config import config
from itertools import islice
#from scipy.sparse import csr_matrix
from util import data_generator, data_generator_tst
## Training Params
def main():
global config
feature_dim = config.feature_dim
n_classes = config.n_classes
hidden_dim = config.hidden_dim
n_train = config.n_train
n_test = config.n_test
n_epochs = config.n_epochs
batch_size = config.batch_size
lr = config.lr
#
if config.GPUs=='':
num_threads = config.num_threads
else:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = config.GPUs
#
train_files = glob.glob(config.data_path)
test_files = glob.glob(config.data_path)
#
x_idxs = tf.placeholder(tf.int64, shape=[None,2])
x_vals = tf.placeholder(tf.float32, shape=[None])
x = tf.SparseTensor(x_idxs, x_vals, [batch_size,feature_dim])
y = tf.placeholder(tf.float32, shape=[None,n_classes])
#
W1 = tf.Variable(tf.truncated_normal([feature_dim,hidden_dim], stddev=2.0/math.sqrt(feature_dim+hidden_dim)))
b1 = tf.Variable(tf.truncated_normal([hidden_dim], stddev=2.0/math.sqrt(feature_dim+hidden_dim)))
layer_1 = tf.nn.relu(tf.sparse_tensor_dense_matmul(x,W1)+b1)
#
W2 = tf.Variable(tf.truncated_normal([hidden_dim,n_classes], stddev=2.0/math.sqrt(hidden_dim+n_classes)))
b2 = tf.Variable(tf.truncated_normal([n_classes], stddev=2.0/math.sqrt(n_classes+hidden_dim)))
logits = tf.matmul(layer_1,W2)+b2
#
k=1
if k==1:
top_idxs = tf.argmax(logits, axis=1)
else:
top_idxs = tf.nn.top_k(logits, k=k, sorted=False)[1]
#
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
#
train_step = tf.train.AdamOptimizer(lr).minimize(loss)
#
if config.GPUs=='':
Config = tf.ConfigProto(inter_op_parallelism_threads=num_threads, intra_op_parallelism_threads=num_threads)
else:
Config = tf.ConfigProto()
Config.gpu_options.allow_growth = True
#
sess = tf.Session(config=Config)
sess.run(tf.global_variables_initializer())
#
training_data_generator = data_generator(train_files, batch_size, n_classes)
steps_per_epoch = n_train//batch_size
n_steps = n_epochs*steps_per_epoch
n_check = 50
#
begin_time = time.time()
total_time = 0
counter = 0
#
with open(config.log_file, 'a') as out:
for i in range(n_steps):
if i%n_check==0:
total_time+=time.time()-begin_time
print('Finished ',i,' steps. Time elapsed for last',n_check,'batches = ',time.time()-begin_time)
n_steps_val = n_test//batch_size
test_data_generator = data_generator_tst(test_files, batch_size)
tmp_k = 0
for h in range(20): # a few test batches to check the precision
idxs_batch, vals_batch, labels_batch = next(test_data_generator)
top_k_classes = sess.run(top_idxs, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch})
tmp_k += np.mean([len(np.intersect1d(top_k_classes[j],labels_batch[j]))/min(k,len(labels_batch[j])) for j in range(len(top_k_classes))])
print('test_acc: ',tmp_k/20)
print('#######################')
print(i,int(total_time),tmp_k/20 , file=out)
begin_time = time.time()
idxs_batch, vals_batch, labels_batch = next(training_data_generator)
sess.run(train_step, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch, y:labels_batch})
if i%steps_per_epoch==steps_per_epoch-1:
total_time+=time.time()-begin_time
print('Finished ',i,' steps. Time elapsed for last 100 batches = ',time.time()-begin_time)
n_steps_val = n_test//batch_size
test_data_generator = data_generator_tst(test_files, batch_size)
num_batches = 0
p_at_k = 0
for l in range(n_steps_val): # precision on entire test data
idxs_batch, vals_batch, labels_batch = next(test_data_generator)
top_k_classes = sess.run(top_idxs, feed_dict={x_idxs:idxs_batch, x_vals:vals_batch})
p_at_k += np.mean([len(np.intersect1d(top_k_classes[j],labels_batch[j]))/min(k,len(labels_batch[j])) for j in range(len(top_k_classes))])
num_batches += 1
#
print('Overall p_at_1 after ',num_batches,'batches = ', p_at_k/num_batches)
print(i, int(total_time), p_at_k/num_batches, file=out)
#
begin_time = time.time()
if __name__ == "__main__":
# execute only if run as a script
main()