-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathusage_example.py
More file actions
48 lines (39 loc) · 2.19 KB
/
Copy pathusage_example.py
File metadata and controls
48 lines (39 loc) · 2.19 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
import tensorflow as tf
from input_utils import input_fn, predict_input_fn
from models import LSTM, DeepSense, SADeepSense, TrASenD_BD, TrASenD_CA, TrASenD
if __name__ == "__main__":
# Directory Paths
TRAINING_DATA_FOLDER_PATH = "/..path../sepHARData_a/train"
EVAL_DATA_FOLDER_PATH = "/..path../sepHARData_a/eval"
MODEL_DIR_PATH = "/path/to/model/dir"
# -------------- GET ESTIMATOR
# Wrap DeepSense estimator in a tf.estimator.Estimator, passing all parameters.
# These are the ones specified by the authors of the DeepSense framework.
num_sensors = 2
batch_size = 64
hyper_params = {"learning_rate": 1e-4,
"beta1": 0.5,
"beta2": 0.9,
"l2_lambda_term": 5e-4}
num_output_classes = 6
ds_model = DeepSense(num_sensors, num_output_classes, hyper_params)
deepSense_classifier = tf.estimator.Estimator(
model_fn = ds_model.get_model_function(),
model_dir = MODEL_DIR_PATH,
params = None)
# -------------- TRAIN ESTIMATOR
# tf.estimator.Estimator wants an input function with no arguments, so wrap input_fn in a lambda.
training_input_function = lambda: input_fn(TRAINING_DATA_FOLDER_PATH, batch_size, num_sensors, num_output_classes, True)
deepSense_classifier.train(training_input_function, steps=1)
# -------------- EVALUATE METRICS
eval_input_function = lambda: input_fn(EVAL_DATA_FOLDER_PATH, batch_size, num_sensors, num_output_classes, False)
eval_result = deepSense_classifier.evaluate(eval_input_function, steps=10)
print("\nTest Set Accuracy: {accuracy:0.3f}\nMean per Class Accuracy: {mean_perClass_accuracy:0.3f}".format(**eval_result))
# -------------- EXAMPLE OF PREDICTION
predict_input_function = lambda: predict_input_fn("/Users/davidebuffelli/Desktop/final/sepHARData_a/eval/eval_0.csv", num_sensors, num_output_classes)
predictions = deepSense_classifier.predict(predict_input_function)
for p in predictions:
print("Predicted Class: ", p["class_ids"])
print("Probabilities: ", p["probabilities"])
print("Logits: ", p["logits"])
print("Transfer Learning input: ", p["tl_input"])