-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeta.py
133 lines (93 loc) · 4.03 KB
/
beta.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
#This code is for loading and using the model on never before seen data
import alpha
def preproc(unclean_batch_x):
#Convert values to range 0-1
temp_batch = unclean_batch_x / unclean_batch_x.max()
return temp_batch
def batch_creator(batch_size, dataset_length, dataset_name):
#Create batch with random samples and return appropriate format
batch_mask = rng.choice(dataset_length, batch_size)
batch_x = eval('trainingDataNumpy')[[batch_mask]].reshape(-1, input_num_units)
batch_y = eval('trainingLabelsNumpy')[[batch_mask]].reshape(-1,output_num_units)
return batch_x, batch_y
def predict_new_data(filename):
#Take in tone and eye data
#create a vector after cleaning
#fix with numpy and reshape and call it inputvector
eye_vector = clean_voice_data(eye.start_alone(6))
tone_vector = clean_tone_data(analyze_user_answer(filename))
inputVector=combine_vectors_for_final(tone_vector,eye_vector)
inputVector = numpy.float32(inputVector).reshape(1,150)
print(inputVector)
with tf.Session() as session:
saver.restore(session, "model")
#feed vector into model
feed_dict = {x: inputVector}
classification = session.run(activation_OP, feed_dict)
#Output predicted value
print(classification)
#predict_new_data("user_final_input.wav")
def get_additional_input_from_user(filename):
try:
tone_vector=pickle.load( open( "tone_vector_from_user.p", "rb" ) )
eye_vector=pickle.load( open( "eye_vector_from_user.p", "rb" ) )
except:
tone_vector=[]
eye_vector=[]
print("havent created it yet")
eye_vector.append(clean_voice_data(eye.start_alone(6)))
tone_vector.append(clean_tone_data(analyze_user_answer(filename)))
pickle.dump( tone_vector, open( "tone_vector_from_user.p", "wb" ) )
pickle.dump( eye_vector, open( "eye_vector_from_user.p", "wb" ) )
def train_model_with_new_input():
label_vector=[1,0,0,1,1,0,1,0,1,0]
tone_vector=pickle.load( open( "tone_vector_from_user.p", "rb" ) )
eye_vector=pickle.load( open( "eye_vector_from_user.p", "rb" ) )
inputVector=combine_vectors_for_final(tone_vector,eye_vector)
trainingDataNumpy = numpy.float32(inputVector)
trainingLabelsNumpy = numpy.float32(label_vector)
#Setting up
seed = 128
rng = numpy.random.RandomState(seed)
# number of neurons in each layer
input_num_units = NUM_COLS
hidden_num_units = 100
output_num_units = 1
x = tf.placeholder(tf.float32, [None, input_num_units])
y = tf.placeholder(tf.float32, [None, output_num_units])
# set remaining variables
epochs = 200
batch_size = 5
learning_rate = 0.01
weights = {
'hidden': tf.Variable(tf.random_normal([input_num_units, hidden_num_units], seed=seed)),
'output': tf.Variable(tf.random_normal([hidden_num_units, output_num_units], seed=seed))
}
biases = {
'hidden': tf.Variable(tf.random_normal([hidden_num_units], seed=seed)),
'output': tf.Variable(tf.random_normal([output_num_units], seed=seed))
}
#set up layers
hidden_layer = tf.add(tf.matmul(x, weights['hidden']), biases['hidden'])
hidden_layer = tf.nn.relu(hidden_layer)
output_layer = tf.matmul(hidden_layer, weights['output']) + biases['output']
activation_OP = tf.nn.sigmoid(output_layer, name="activation")
#define cost
cost = tf.nn.l2_loss(activation_OP-y, name="squared_error_cost")
#adam gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
#initialize variables
init=tf.global_variables_initializer()
with tf.Session() as session:
saver.restore(session, "model")
sess.run(init)
for epoch in range(epochs):
avg_cost = 0
total_batch = int(trainingDataNumpy.shape[0]/batch_size)
for i in range(total_batch):
batch_x, batch_y = batch_creator(batch_size, trainingDataNumpy.shape[0],'train')
_, c, output= sess.run([optimizer, cost,activation_OP], feed_dict = {x: batch_x, y: batch_y})
avg_cost += c / total_batch
print("Epoch:", (epoch+1), "cost =", "{:.5f}".format(avg_cost))
print("\nTraining complete!")
saver.save(sess, "model")