-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_deep.py
160 lines (126 loc) · 5.19 KB
/
tf_deep.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import tensorflow as tf
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import data
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from time import strftime
from tensorflow.contrib import learn, layers
from os import path
logdir = path.join(path.dirname(__file__), 'logs')
print(logdir)
class TFDeep:
def __init__(self, layers, param_delta=0.001, l2=0, ldir=strftime("%d_%b_%Y_%H:%M:%S")):
with tf.Graph().as_default():
self.X = tf.placeholder(tf.float32, [None, layers[0]], "X_input")
self.Yoh = tf.placeholder(
tf.float32, [None, layers[-1]], "Yp_target")
losses = []
net = self.X
for k in range(len(layers) - 1):
i = layers[k]
j = layers[k + 1]
# Xavier initialization for Relu
w = tf.Variable(tf.random_normal([i, j], 0, (2 / i)**0.5))
b = tf.Variable(tf.constant(0, tf.float32, [j]))
losses.append(tf.nn.l2_loss(w))
net = tf.matmul(net, w) + b
if k + 1 < len(layers):
net = tf.nn.relu(net)
self.logits = net
self.yp = tf.nn.softmax(self.logits)
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
self.logits, self.Yoh)) + l2 * tf.add_n(losses)
self.trainer = tf.train.AdamOptimizer(param_delta)
self.train_op = self.trainer.minimize(self.loss)
tf.scalar_summary('loss', self.loss)
correct_prediction = tf.equal(
tf.argmax(self.logits, 1), tf.argmax(self.Yoh, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.scalar_summary("accuracy", accuracy)
self.sess = tf.Session()
self.merged = tf.merge_all_summaries()
self.train_writer = tf.train.SummaryWriter(
path.join(logdir, ldir, 'train'), self.sess.graph)
self.val_writer = tf.train.SummaryWriter(path.join(logdir, ldir, 'val', self.sess.graph)
self.sess.run(tf.initialize_all_variables())
def train(self, X, Yoh_, param_niter):
"""Arguments:
- X: actual datapoints [NxD]
- Yoh_: one-hot encoded labels [NxC]
- param_niter: number of iterations
"""
self.transform=StandardScaler()
X=self.transform.fit_transform(X)
for i in range(param_niter):
if i % 100 == 0 or i == param_niter - 1:
summary, _=self.sess.run([self.merged, self.train_op], feed_dict={
self.X: X,
self.Yoh: Yoh_})
self.train_writer.add_summary(summary, i)
else:
self.sess.run([self.train_op], feed_dict={
self.X: X,
self.Yoh: Yoh_})
def fit(self, X, Y, Xv, Yv, batch_size, param_niter):
self.transform=StandardScaler()
X=self.transform.fit_transform(X)
N=X.shape[0]
for i in range(param_niter):
perm=np.random.permutation(N)
for idx in range(batch_size, N + 1, batch_size):
idxs=perm[idx - batch_size:idx]
batch_xs=X[idxs]
batch_ys=Y[idxs]
self.sess.run(self.train_op, feed_dict={
self.X: batch_xs, self.Yoh: batch_ys})
if i % 100 == 0 or i == param_niter - 1:
summary=self.sess.run(self.merged, feed_dict={
self.X: X,
self.Yoh: Y})
self.train_writer.add_summary(summary, i)
summary=self.sess.run(self.merged, feed_dict={
self.X: Xv,
self.Yoh: Yv})
self.val_writer.add_summary(summary, i)
def eval(self, X):
"""Arguments:
- X: actual datapoints [NxD]
Returns: predicted class probabilites [NxC]
"""
return self.sess.run(self.yp, feed_dict={self.X: self.transform.transform(X)})
def predict(self, X):
return np.argmax(self.eval(X), axis=1)
def count_params(self):
for var in tf.trainable_variables():
print("var", var.name)
if __name__ == "__main__":
# inicijaliziraj generatore slučajnih brojeva
np.random.seed(100)
tf.set_random_seed(100)
# instanciraj podatke X i labele Yoh
D=2
C=2
X, Y=data.sample_gmm_2d(5, C, 10)
oh=OneHotEncoder(sparse=False)
oh.fit(Y)
Yoh=oh.transform(Y)
Yoh.shape
X.shape
ll=0
print("lambda", ll)
# izgradi graf:
tflr=TFDeep([D, 10, C], 0.001, ll)
tflr.count_params()
# nauči parametre:
tflr.train(X, Yoh, 10000)
# dohvati vjerojatnosti na skupu za učenje
probs=tflr.eval(X)
ypp=np.argmax(probs, axis=1)
print(classification_report(Y.reshape(-1), ypp))
cm=confusion_matrix(Y.reshape(-1), ypp)
print("confusion matrix\n", cm)
# iscrtaj rezultate, decizijsku plohu
data.graph_data_pred(X, Y, tflr)
# plt.show()
tflr.sess.close()