-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment.py
More file actions
120 lines (94 loc) · 3.8 KB
/
assignment.py
File metadata and controls
120 lines (94 loc) · 3.8 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 3 12:48:45 2019
@author: cindyli
"""
from __future__ import absolute_import
#from matplotlib import pyplot as plt
import numpy as np
from preprocessing import get_data
import tensorflow as tf
from model import Model
import matplotlib.pyplot as plt
def train(model, train_inputs, train_labels):
'''
Trains the model on all of the inputs and labels.
:param model: the initialized model to use for the forward
pass and backward pass
:param train_inputs: train inputs (all inputs to use for training)
:param train_inputs: train labels (all labels to use for training)
:return: None
'''
# TODO: Iterate over the training inputs and labels, in model.batch_size increments
# TODO: For every batch, compute then descend the gradients for the model's weights
for i in range(0, len(train_labels), model.batch_size):
inputs = train_inputs[i:i+model.batch_size]
labels = train_labels[i:i+model.batch_size]
with tf.GradientTape() as tape:
pred = model.call(inputs)
loss = model.loss(pred, labels)
gradients = tape.gradient(loss, model.trainable_variables)
model.optimizer.apply_gradients(zip(gradients, model.trainable_variables))
def test(model, test_inputs, test_labels):
"""
Tests the model on the test inputs and labels. For this assignment,
the inputs should be the entire test set, but in the future we will
ask you to batch it instead.
:param test_inputs: MNIST test data (all images to be tested)
:param test_labels: MNIST test labels (all corresponding labels)
:return: accuracy - Float (0,1)
"""
# TODO: Iterate over the testing inputs and labels
# TODO: Return accuracy across testing set
pred = model.call(test_inputs)
loss = model.loss(pred, test_labels)
return loss
def main():
'''
Read in MNIST data, initialize your model, and train and test your model
for one epoch. The number of training steps should be your the number of
batches you run through in a single epoch. You should receive a final accuracy on the testing examples of > 80%.
:return: None
'''
# TODO: load MNIST train and test examples into train_inputs, train_labels, test_inputs, test_labels
fr, km = get_data('COS071212_MOCAP.mat')
indices = tf.range(0, len(fr))
tf.random.shuffle(indices)
fr = tf.gather(fr, indices)
km = tf.gather(km, indices)
eighty_p = int(len(fr) * 0.8)
train_inp = fr[:eighty_p]
train_lab = km[:eighty_p]
test_inp = fr[eighty_p:]
test_lab = km[eighty_p:]
# TODO: Create Model
model = Model(29)
# TODO: Train model by calling train() ONCE on all data
results = 0
final_results = 0
num_epochs = 200
loss_list = []
for i in range(num_epochs):
print("EPOCH: ", i)
indices = tf.range(0, len(train_inp))
tf.random.shuffle(indices)
train_inp = tf.gather(train_inp, indices)
train_lab = tf.gather(train_lab, indices)
print("training")
train(model, train_inp, train_lab)
# TODO: Test the accuracy by calling test() after running train()
print("testing")
results = test(model, test_inp, test_lab)
loss_list.append(results)
print("results: ", results)
final_results += results
epoch_list = tf.range(0, num_epochs)
plt.xlabel('Epoch')
plt.ylabel('Loss per Epoch')
plt.title('Loss Between Predicted and Actual Kinematic Positions')
plt.plot(epoch_list, loss_list)
plt.show()
print("final_results: ", final_results / num_epochs)
if __name__ == '__main__':
main()