-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-dnn-classifier.py
84 lines (57 loc) · 2.43 KB
/
example-dnn-classifier.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
import pandas as pd
import tensorflow as tf
def retrieveData():
trainingData = pd.read_csv("training-data.csv", header=None).as_matrix()
testData = pd.read_csv("test-data.csv", header=None).as_matrix()
return trainingData, testData
def separateFeaturesAndCategories(trainingData, testData):
trainingFeatures = formatFeatures(trainingData[:, :-1])
trainingCategories = trainingData[:, -1:]
testFeatures = formatFeatures(testData[:, :-1])
testCategories = testData[:, -1:]
return trainingFeatures, trainingCategories, testFeatures, testCategories
def formatFeatures(features):
formattedFeatures = dict()
numColumns = features.shape[1]
for i in range(0, numColumns):
formattedFeatures[str(i)] = features[:, i]
return formattedFeatures
def defineFeatureColumns(features):
featureColumns = []
for key in features.keys():
featureColumns.append(tf.feature_column.numeric_column(key=key))
return featureColumns
def instantiateClassifier(featureColumns):
classifier = tf.estimator.DNNClassifier(
feature_columns = featureColumns,
hidden_units = [20, 30, 20],
n_classes = 10
)
return classifier
def train(features, labels, batchSize):
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
return dataset.shuffle(1000).repeat().batch(batchSize)
def test(features, labels, batchSize):
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)).batch(batchSize)
return dataset
def trainClassifier(classifier, trainingFeatures, trainingCategories):
classifier.train(
input_fn = lambda:train(trainingFeatures, trainingCategories, 100),
steps = 50
)
def testClassifier(classifier, testFeatures, testCategories):
accuracy = classifier.evaluate(
input_fn = lambda:test(testFeatures, testCategories, 100)
)
return accuracy
def main():
trainingData, testData = retrieveData()
trainingFeatures, trainingCategories, testFeatures, testCategories = \
separateFeaturesAndCategories(trainingData, testData)
featureColumns = defineFeatureColumns(trainingFeatures)
classifier = instantiateClassifier(featureColumns)
trainClassifier(classifier, trainingFeatures, trainingCategories)
accuracy = testClassifier(classifier, testFeatures, testCategories)
print('Test accuracy: {accuracy:0.3f}\n'.format(**accuracy))
if __name__ == "__main__":
main()