-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassignment_2.py
More file actions
126 lines (101 loc) · 4.05 KB
/
assignment_2.py
File metadata and controls
126 lines (101 loc) · 4.05 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
115
116
117
118
119
120
121
122
123
124
125
126
from __future__ import print_function
import os
import sys
import tensorflow as tf
import numpy as np
from subprocess import call
TRAINING_DATA = 'train_preprocessed.npy'
TRAINING_LABELS = 'train_preprocessed_labels.npy'
VALIDATION_DATA = 'valid_preprocessed.npy'
VALIDATION_LABELS = 'valid_preprocessed_labels.npy'
if not os.path.isfile(TRAINING_DATA):
call(["python", "preprocess_data.py", "TRAIN"])
if not os.path.isfile(VALIDATION_DATA):
call(["python", "preprocess_data.py", "VALID"])
if not os.path.isfile(TRAINING_LABELS):
call(["python", "preprocess_labels.py", "TRAIN"])
if not os.path.isfile(VALIDATION_LABELS):
call(["python", "preprocess_labels.py", "VALID"])
x_train = np.load('train_preprocessed.npy')
y_train = np.load('train_preprocessed_labels.npy')
x_test = np.load('valid_preprocessed.npy')
y_test = np.load('valid_preprocessed_labels.npy')
# Parameters
learning_rate = 0.001
batch_size = 1000
n_hidden_1 = 1500 # 1st layer number of features
n_hidden_2 = 800 # 2nd layer number of features
# best so far: 700, 300
training_epochs = 100
display_step = 1
stddev = 0.01
# Network Parameters
n_input = len(x_train[0])
n_classes = 104
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with relu activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with relu activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], stddev=stddev)),
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1], stddev=stddev)),
'b2': tf.Variable(tf.random_normal([n_hidden_2], stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_classes], stddev=stddev)),
}
# Construct model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))\
+ beta * tf.nn.l2_loss(weights['h1'])\
+ beta * tf.nn.l2_loss(weights['h2'])\
+ beta * tf.nn.l2_loss(weights['out'])\
+ beta * tf.nn.l2_loss(biases['h1'])\
+ beta * tf.nn.l2_loss(biases['h2'])\
+ beta * tf.nn.l2_loss(biases['out'])
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Initializing the variables
init = tf.initialize_all_variables()
print("Starting Optimization")
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(len(x_train) / batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x = x_train[i * batch_size: (i + 1) * batch_size]
batch_y = y_train[i * batch_size: (i + 1) * batch_size]
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch + 1), "cost=",
"{:.9f}".format(avg_cost))
print("Optimization Finished")
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Accuracy:", accuracy.eval({x: x_test, y: y_test}))
call(['speech-dispatcher']) # start speech dispatcher
call(['spd-say', '"your process has finished"'])