-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRU_sigmoid.py
75 lines (61 loc) · 2.89 KB
/
GRU_sigmoid.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
import tensorflow as tf
import numpy as np
import pickle
import os
data_path = os.path.dirname(os.path.realpath(__file__)) + "\\data\\"
X_train, y_train = pickle.load(open(data_path + "train.p", "rb"))
X_test, y_test = pickle.load(open(data_path + "test.p", "rb"))
X_val, y_val = pickle.load(open(data_path + "val.p", "rb"))
'''y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)
y_val = y_val.reshape(-1, 1)'''
y_train = np.reshape(y_train, (-1, 1))
y_test = np.reshape(y_test, (-1, 1))
y_val = np.reshape(y_val, (-1, 1))
n_steps = 99
n_inputs = 26
n_neurons = 128
n_outputs = 1
n_layers = 3
learning_rate = 0.001
n_epochs = 20
batch_size = 128
n_batches = int(len(X_train)/batch_size)
X_batches = np.array_split(X_train, n_batches)
Y_batches = np.array_split(y_train, n_batches)
X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_outputs])
keep_prob = tf.placeholder_with_default(1.0, shape=())
cells = [tf.contrib.rnn.GRUCell(n_neurons) for layer in range(n_layers)]
#cells_proj = [tf.contrib.rnn.OutputProjectionWrapper(cell, output_size=n_outputs, activation=tf.nn.softmax) for cell in cells]
cells_drop = [tf.contrib.rnn.DropoutWrapper(cell, input_keep_prob=keep_prob) for cell in cells]
multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cells_drop)
outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)
stacked_rnn_outputs = tf.reshape(outputs, [-1, n_neurons])
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs, activation=tf.sigmoid)
outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])
logits = outputs[:,-1,:]
#logits = tf.layers.dense(outputs[:,-1,:], n_outputs, activation=tf.nn.softmax)
xentropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
#????????? Noe rart her
accuracy = tf.metrics.accuracy(labels=y, predictions=tf.cast(logits, tf.int32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(tf.local_variables_initializer())
init.run()
for epoch in range(n_epochs):
for i in range(n_batches):
X_batch, y_batch = X_batches[i], Y_batches[i]
sess.run(training_op, feed_dict={X: X_batch, y: y_batch, keep_prob: .5})
acc_train = sess.run(accuracy, feed_dict={X: X_batch, y: y_batch})
acc_test = sess.run(accuracy, feed_dict={X: X_test, y: y_test})
log = logits.eval(feed_dict={X: X_test, y: y_test})
y_val = y.eval(feed_dict={X: X_test, y: y_test})
print(['%.4f' % elem for elem in log[10:20]])
print(y_val[10:20])
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost), "Val accuracy:", accuracy.eval({X: X_val, y: y_val}))
print("Optimization Finished!")
print("Test accuracy:", accuracy.eval({X: X_test, y: y_test}))