Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 38c0931

Browse files
sk-ippdxjohnny
authored andcommitted
docs: model: tensorflow: Python code for DNNClassifier
Related: #433
1 parent bdac6cb commit 38c0931

File tree

10 files changed

+185
-70
lines changed

10 files changed

+185
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- Create a fresh archive of the git repo for release instead of cleaning
2525
existing repo with `git clean` for development service release command.
2626
- Simplified SLR tests for scratch model
27+
- Test tensorflow DNNClassifier documentation exaples in CI
2728

2829
## [0.3.4] - 2020-02-28
2930
### Added

docs/plugins/dffml_model.rst

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,59 +34,34 @@ tfdnnc
3434

3535
Implemented using Tensorflow's DNNClassifier.
3636

37-
.. code-block:: console
37+
First we create the training and testing datasets
38+
39+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/train_data.sh
40+
41+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/test_data.sh
42+
43+
Train the model
44+
45+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/train.sh
46+
47+
Assess the accuracy
48+
49+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/accuracy.sh
50+
51+
Output
52+
53+
.. code-block::
3854
39-
$ wget http://download.tensorflow.org/data/iris_training.csv
40-
$ wget http://download.tensorflow.org/data/iris_test.csv
41-
$ head iris_training.csv
42-
$ sed -i 's/.*setosa,versicolor,virginica/SepalLength,SepalWidth,PetalLength,PetalWidth,classification/g' *.csv
43-
$ head iris_training.csv
44-
$ dffml train \
45-
-model tfdnnc \
46-
-model-epochs 3000 \
47-
-model-steps 20000 \
48-
-model-predict classification:int:1 \
49-
-model-classifications 0 1 2 \
50-
-model-clstype int \
51-
-sources iris=csv \
52-
-source-filename iris_training.csv \
53-
-model-features \
54-
SepalLength:float:1 \
55-
SepalWidth:float:1 \
56-
PetalLength:float:1 \
57-
PetalWidth:float:1 \
58-
-log debug
59-
... lots of output ...
60-
$ dffml accuracy \
61-
-model tfdnnc \
62-
-model-predict classification:int:1 \
63-
-model-classifications 0 1 2 \
64-
-model-clstype int \
65-
-sources iris=csv \
66-
-source-filename iris_test.csv \
67-
-model-features \
68-
SepalLength:float:1 \
69-
SepalWidth:float:1 \
70-
PetalLength:float:1 \
71-
PetalWidth:float:1 \
72-
-log critical
7355
0.99996233782
74-
$ dffml predict all \
75-
-model tfdnnc \
76-
-model-predict classification:int:1 \
77-
-model-classifications 0 1 2 \
78-
-model-clstype int \
79-
-sources iris=csv \
80-
-source-filename iris_test.csv \
81-
-model-features \
82-
SepalLength:float:1 \
83-
SepalWidth:float:1 \
84-
PetalLength:float:1 \
85-
PetalWidth:float:1 \
86-
-caching \
87-
-log critical \
88-
> results.json
89-
$ head -n 33 results.json
56+
57+
Make a prediction
58+
59+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/predict.sh
60+
61+
Output
62+
63+
.. code-block:: json
64+
9065
[
9166
{
9267
"extra": {},
@@ -107,25 +82,11 @@ Implemented using Tensorflow's DNNClassifier.
10782
},
10883
"key": "0"
10984
},
110-
{
111-
"extra": {},
112-
"features": {
113-
"PetalLength": 5.4,
114-
"PetalWidth": 2.1,
115-
"SepalLength": 6.9,
116-
"SepalWidth": 3.1,
117-
"classification": 2
118-
},
119-
"last_updated": "2019-07-31T02:00:12Z",
120-
"prediction": {
121-
"classification":
122-
{
123-
"confidence": 0.9999984502792358,
124-
"value": 2
125-
}
126-
},
127-
"key": "1"
128-
},
85+
]
86+
87+
Example usage of Tensorflow DNNClassifier model using python API
88+
89+
.. literalinclude:: /../model/tensorflow/examples/tfdnnc/tfdnnc.py
12990

13091
**Args**
13192

model/tensorflow/examples/tfdnnc/__init__.py

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
dffml accuracy \
2+
-model tfdnnc \
3+
-model-predict classification:int:1 \
4+
-model-classifications 0 1 2 \
5+
-model-clstype int \
6+
-sources iris=csv \
7+
-source-filename iris_test.csv \
8+
-model-features \
9+
SepalLength:float:1 \
10+
SepalWidth:float:1 \
11+
PetalLength:float:1 \
12+
PetalWidth:float:1 \
13+
-log critical
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
echo -e 'SepalLength,SepalWidth,PetalLength,PetalWidth\n5.9,3.0,4.2,1.5\n' | \
2+
dffml predict all \
3+
-model tfdnnc \
4+
-model-predict classification:int:1 \
5+
-model-classifications 0 1 2 \
6+
-model-clstype int \
7+
-sources iris=csv \
8+
-model-features \
9+
SepalLength:float:1 \
10+
SepalWidth:float:1 \
11+
PetalLength:float:1 \
12+
PetalWidth:float:1 \
13+
-source-filename /dev/stdin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wget http://download.tensorflow.org/data/iris_test.csv
2+
sed -i 's/.*setosa,versicolor,virginica/SepalLength,SepalWidth,PetalLength,PetalWidth,classification/g' *.csv
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
import ast
3+
import sys
4+
import json
5+
import tempfile
6+
import contextlib
7+
import subprocess
8+
import unittest.mock
9+
10+
from dffml.util.os import chdir
11+
12+
13+
def sh_filepath(filename):
14+
return os.path.join(os.path.dirname(__file__), filename)
15+
16+
17+
@contextlib.contextmanager
18+
def directory_with_csv_files():
19+
with tempfile.TemporaryDirectory() as tempdir:
20+
with chdir(tempdir):
21+
subprocess.check_output(["bash", sh_filepath("train_data.sh")])
22+
subprocess.check_output(["bash", sh_filepath("test_data.sh")])
23+
yield tempdir
24+
25+
26+
class TestExample(unittest.TestCase):
27+
def python_test(self, filename):
28+
# Path to target file
29+
filepath = os.path.join(os.path.dirname(__file__), filename)
30+
# Capture output
31+
stdout = subprocess.check_output([sys.executable, filepath])
32+
lines = stdout.decode().split("\n")
33+
# Check the Accuracy
34+
self.assertIn("Accuracy: 0.9", lines[0])
35+
# Check the classification
36+
self.assertEqual(
37+
round(ast.literal_eval(lines[1])["classification"]), 1
38+
)
39+
self.assertEqual(
40+
round(ast.literal_eval(lines[2])["classification"]), 2
41+
)
42+
43+
def test_python_filenames(self):
44+
with directory_with_csv_files() as tempdir:
45+
self.python_test("tfdnnc.py")
46+
47+
def test_shell(self):
48+
with directory_with_csv_files() as tempdir:
49+
# Run training
50+
subprocess.check_output(["bash", sh_filepath("train.sh")])
51+
# Check the Accuracy
52+
stdout = subprocess.check_output(
53+
["bash", sh_filepath("accuracy.sh")]
54+
)
55+
self.assertAlmostEqual(
56+
float(stdout.decode().strip()), 0.9, places=0
57+
)
58+
# Make the prediction
59+
stdout = subprocess.check_output(
60+
["bash", sh_filepath("predict.sh")]
61+
)
62+
records = json.loads(stdout.decode())
63+
# Check the classification
64+
self.assertAlmostEqual(
65+
round(records[0]["prediction"]["classification"]["value"]), 1
66+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from dffml import CSVSource, Features, DefFeature
2+
from dffml.noasync import train, accuracy, predict
3+
from dffml_model_tensorflow.dnnc import DNNClassifierModel
4+
5+
model = DNNClassifierModel(
6+
features=Features(
7+
DefFeature("SepalLength", float, 1),
8+
DefFeature("SepalWidth", float, 1),
9+
DefFeature("PetalLength", float, 1),
10+
DefFeature("PetalWidth", float, 1),
11+
),
12+
predict=DefFeature("classification", int, 1),
13+
epochs=3000,
14+
steps=20000,
15+
classifications=[0, 1, 2],
16+
clstype=int,
17+
)
18+
19+
# Train the model
20+
train(model, "iris_training.csv")
21+
22+
# Assess accuracy (alternate way of specifying data source)
23+
print("Accuracy:", accuracy(model, CSVSource(filename="iris_test.csv")))
24+
25+
# Make prediction
26+
for i, features, prediction in predict(
27+
model,
28+
{
29+
"PetalLength": 4.2,
30+
"PetalWidth": 1.5,
31+
"SepalLength": 5.9,
32+
"SepalWidth": 3.0,
33+
},
34+
{
35+
"PetalLength": 5.4,
36+
"PetalWidth": 2.1,
37+
"SepalLength": 6.9,
38+
"SepalWidth": 3.1,
39+
},
40+
):
41+
features["classification"] = prediction["classification"]["value"]
42+
print(features)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dffml train \
2+
-model tfdnnc \
3+
-model-epochs 3000 \
4+
-model-steps 20000 \
5+
-model-predict classification:int:1 \
6+
-model-classifications 0 1 2 \
7+
-model-clstype int \
8+
-sources iris=csv \
9+
-source-filename iris_training.csv \
10+
-model-features \
11+
SepalLength:float:1 \
12+
SepalWidth:float:1 \
13+
PetalLength:float:1 \
14+
PetalWidth:float:1 \
15+
-log debug
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wget http://download.tensorflow.org/data/iris_training.csv
2+
sed -i 's/.*setosa,versicolor,virginica/SepalLength,SepalWidth,PetalLength,PetalWidth,classification/g' *.csv

0 commit comments

Comments
 (0)