-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemotion_logistic_0.2_161001.py
115 lines (99 loc) · 3.69 KB
/
emotion_logistic_0.2_161001.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
#Ryan Shin: [email protected]
#Date: 160927
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
print ("Packages loaded")
#Load data
cwd = os.getcwd()
loadpath = cwd + "/dataset/data_gray.npz"
l = np.load(loadpath)
#Check what's included
print (l.files)
#Parse data
trainimg = l['trainimg']
trainlabel = l['trainlabel']
testimg = l['testimg']
testlabel = l['testlabel']
ntrain = trainimg.shape[0]
nclass = trainlabel.shape[1]
dim = trainimg.shape[1]
ntest = testimg.shape[0]
print ("%d train images loaded" % (ntrain))
print ("%d test images loaded" % (ntest))
print ("%d dimentional input" % (dim))
print ("%d classes" % (nclass))
#Define network
tf.set_random_seed(0)
#Param. of Log. Regression
learning_rate = 0.001
training_epochs = 1000
batch_size = 10
display_step = 100
with graph.as_default():
graph = tf.Graph()
#Saver init
ckpt_dir = './ckpt_dir'
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir)
global_step = tf.Variable(0, name='global_step', trainable=False)
# Create Graph for Logistic Regression
x = tf.placeholder("float", [None, dim])
y = tf.placeholder("float", [None, nclass])
W = tf.Variable(tf.zeros([dim, nclass]), name = 'weights')
b = tf.Variable(tf.zeros([nclass]))
#Define functions
WEIGHT_DECAY_FACTOR = 0.000001
l2_loss = tf.add_n([tf.nn.l2_loss(v)
for v in tf.trainable_variables()])
_pred = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(_pred),
reduction_indices=1))
cost = cost + WEIGHT_DECAY_FACTOR*l2_loss
#Optimizer
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
_corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1))
accr = tf.reduce_mean(tf.cast(_corr, tf.float32))
saver = tf.train.Saver()
print ("Functions ready")
with tf.Session(graph=graph) as session:
init = tf.initialize_all_variables().run()
ckpt = tf.train.get_checkpoint_state(ckpt_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(session, ckpt.model_checkpoint_path) #restore all Variable
start = global_step.eval() # get last globak
print("global_step :", start)
#Launch the graph
#sess = tf.Session()
#Optimize
#sess.run(init)
#Training Cycle
for epoch in xrange(training_epochs):
avg_cost = 0.
num_batch = int(ntrain/batch_size)
# Loop over all batches
for i in xrange(num_batch):
randidx = np.random.randint(ntrain, size=batch_size)
batch_xs = trainimg[randidx, :]
batch_ys = trainlabel[randidx, :]
# Fit Training using batch data
session.run(optm, feed_dict={x: batch_xs, y: batch_ys})
# Compute Average loss
avg_cost += session.run(cost,
feed_dict = {x: batch_xs, y: batch_ys})/num_batch
if (epoch % 10 == 0):
print('Minbath loss at step ', epoch, ':', avg_cost)
global_step.assign(epoch).eval()
saver.save(session, ckpt_dir + '/model.ckpt', global_step=global_step)
# Display logs per epoch step
if epoch % display_step == 0:
train_acc = session.run(accr, feed_dict={x: batch_xs, y: batch_ys})
print ("Epoch: %03d/%03d cost: %.9f" %
(epoch, training_epochs, avg_cost))
print (" Training accuracy: %.3f" % (train_acc))
test_acc = session.run(accr, feed_dict={x: testimg, y: testlabel})
print (" Test accuracy: %.3f" % (test_acc))
print ("Optimization Finished")
#sess.close()
print ("Session closed.")