-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
125 lines (121 loc) · 4.45 KB
/
utils.py
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
115
116
117
118
119
120
121
122
123
124
125
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
from tensorflow.contrib import layers
# Model construction utilities below adapted from
# https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html#deep-mnist-for-experts
def encoder(input_tensor, output_size):
'''Create encoder network.
Args:
input_tensor: a batch of flattened images [batch_size, 28*28]
Returns:
A tensor that expresses the encoder network
'''
net = tf.reshape(input_tensor, [-1, 28, 28, 1])
net = layers.conv2d(net, 32, 5, stride=2)
net = tf.nn.relu(net)
#net = layers.batch_norm(net)
net = layers.conv2d(net, 64, 5, stride=2)
net = tf.nn.relu(net)
#net = layers.batch_norm(net)
net = layers.conv2d(net, 128, 5, stride=2, padding='VALID')
net = tf.nn.relu(net)
net = layers.batch_norm(net)
net = layers.dropout(net, keep_prob=0.9)
net = layers.flatten(net)
return layers.fully_connected(net, output_size, activation_fn=None)
def decoder(input_tensor):
'''Create decoder network.
If input tensor is provided then decodes it, otherwise samples from
a sampled vector.
Args:
input_tensor: a batch of vectors to decode
Returns:
A tensor that expresses the decoder network
'''
net = tf.expand_dims(input_tensor, 1)
net = tf.expand_dims(net, 1)
net = layers.conv2d_transpose(net, 128, 5, padding='VALID')
net = tf.nn.relu(net)
#net = layers.batch_norm(net)
net = layers.conv2d_transpose(net,64 , 3, padding='VALID')
net = tf.nn.relu(net)
#net = layers.batch_norm(net)
net = layers.conv2d_transpose(net, 32, 5, stride=2)
net = tf.nn.relu(net)
#net = layers.batch_norm(net)
net = layers.conv2d_transpose(
net, 1, 5, stride=2, activation_fn=tf.nn.sigmoid)
net = layers.flatten(net)
return net
def kl_divergence(p,q):
y = tf.div(p,q)+1e-6
return tf.reduce_mean(-tf.nn.softmax_cross_entropy_with_logits(p, y))
def cluster_acc(Y_pred, Y):
from sklearn.utils.linear_assignment_ import linear_assignment
assert Y_pred.size == Y.size
D = max(Y_pred.max(), Y.max())+1
w = np.zeros((D,D), dtype=np.int32)
for i in xrange(Y_pred.size):
w[Y_pred[i], Y[i]] += 1
ind = linear_assignment(w.max() - w)
return sum([w[i,j] for i,j in ind])*1.0/Y_pred.size, w
def squared_dist(A,B):
expanded_a = tf.expand_dims(A, 1)
expanded_b = tf.expand_dims(B, 0)
distances = tf.reduce_sum(tf.squared_difference(expanded_a, expanded_b), 2)
return distances
def calc_q(feature,cluster,cluster_num=10,batch_size=128):
q = 1 + squared_dist(feature,cluster)
q = 1./q
q_sum = tf.reduce_sum(1 + squared_dist(feature,cluster),0)
q_sum = 1./q_sum
q = q/q_sum
return q
def calc_p(q,cluster_num=10,batch_size=128):
f = tf.reduce_sum(q,1)
f = 1./f
f = tf.reshape(f,[cluster_num,1])
div_value = tf.matmul(q*q,f)
div_value = tf.reduce_sum(div_value,0)
p = tf.matmul(q*q,f)
p = p / div_value
return q
def weight_variable(shape,stddev=0.1,name=None,train=True):
#initial = tf.random_normal(shape, stddev=0.1, dtype=tf.float32)#
initial = tf.truncated_normal(shape, stddev=stddev) #default 0.1
if name:
#W = tf.get_variable(name, shape=shape,
# initializer=tf.contrib.layers.xavier_initializer())
return tf.Variable(initial,name=name,trainable=train)
else:
return tf.Variable(initial)
def bias_variable(shape,init=0.1,name=None):
initial = tf.constant(init, shape=shape)
if name:
return tf.Variable(initial,name=name)
else:
return tf.Variable(initial)
def shuffle_aligned_list(data):
"""Shuffle arrays in a list by shuffling each array identically."""
num = data[0].shape[0]
p = np.random.permutation(num)
return [d[p] for d in data]
def batch_generator(data, batch_size, shuffle=True):
"""Generate batches of data.
Given a list of array-like objects, generate batches of a given
size by yielding a list of array-like objects corresponding to the
same slice of each input.
"""
if shuffle:
data = shuffle_aligned_list(data)
batch_count = 0
while True:
if batch_count * batch_size + batch_size >= len(data[0]):
batch_count = 0
if shuffle:
data = shuffle_aligned_list(data)
start = batch_count * batch_size
end = start + batch_size
batch_count += 1
yield [d[start:end] for d in data]