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

Commit 261d413

Browse files
authored
docs: model: tensorflow: Python API examples
Fixes: #433
1 parent b9e9758 commit 261d413

File tree

11 files changed

+192
-101
lines changed

11 files changed

+192
-101
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Docstrings and doctestable examples to `record.py`.
1010
- Inputs can be validated using operations
1111
- `validate` parameter in `Input` takes `Operation.instance_name`
12+
- Test tensorflow DNNEstimator documentation exaples in CI
13+
- Add python code for tensorflow DNNEstimator
1214
### Fixed
1315
- New model tutorial mentions file paths that should be edited.
1416

docs/plugins/dffml_model.rst

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -154,58 +154,32 @@ Generating train and test data
154154
make sure to take a BACKUP of files with same name in the directory
155155
from where this command is run as it overwrites any existing files.
156156

157-
.. code-block:: console
157+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/train_data.sh
158+
159+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/test_data.sh
160+
161+
Train the model
162+
163+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/train.sh
164+
165+
Assess the accuracy
166+
167+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/accuracy.sh
168+
169+
Output
170+
171+
.. code-block::
158172
159-
$ cat > train.csv << EOF
160-
Feature1,Feature2,TARGET
161-
0.93,0.68,3.89
162-
0.24,0.42,1.75
163-
0.36,0.68,2.75
164-
0.53,0.31,2.00
165-
0.29,0.25,1.32
166-
0.29,0.52,2.14
167-
EOF
168-
$ cat > test.csv << EOF
169-
Feature1,Feature2,TARGET
170-
0.57,0.84,3.65
171-
0.95,0.19,2.46
172-
0.23,0.15,0.93
173-
EOF
174-
$ dffml train \
175-
-model tfdnnr \
176-
-model-epochs 300 \
177-
-model-steps 2000 \
178-
-model-predict TARGET:float:1 \
179-
-model-hidden 8 16 8 \
180-
-sources s=csv \
181-
-source-filename train.csv \
182-
-model-features \
183-
Feature1:float:1 \
184-
Feature2:float:1 \
185-
-log debug
186-
Enabling debug log shows tensorflow losses...
187-
$ dffml accuracy \
188-
-model tfdnnr \
189-
-model-predict TARGET:float:1 \
190-
-model-hidden 8 16 8 \
191-
-sources s=csv \
192-
-source-filename test.csv \
193-
-model-features \
194-
Feature1:float:1 \
195-
Feature2:float:1 \
196-
-log critical
197173
0.9468210011
198-
$ echo -e 'Feature1,Feature2,TARGET\n0.21,0.18,0.84\n' | \
199-
dffml predict all \
200-
-model tfdnnr \
201-
-model-predict TARGET:float:1 \
202-
-model-hidden 8 16 8 \
203-
-sources s=csv \
204-
-source-filename /dev/stdin \
205-
-model-features \
206-
Feature1:float:1 \
207-
Feature2:float:1 \
208-
-log critical
174+
175+
Make a prediction
176+
177+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/predict.sh
178+
179+
Output
180+
181+
.. code-block:: json
182+
209183
[
210184
{
211185
"extra": {},
@@ -225,6 +199,10 @@ Generating train and test data
225199
}
226200
]
227201
202+
Example usage of Tensorflow DNNEstimator model using python API
203+
204+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/tfdnnr.py
205+
228206
The ``NaN`` in ``confidence`` is the expected behaviour. (See TODO in
229207
predict).
230208

model/tensorflow/dffml_model_tensorflow/dnnr.py

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -195,58 +195,32 @@ class DNNRegressionModel(Model):
195195
make sure to take a BACKUP of files with same name in the directory
196196
from where this command is run as it overwrites any existing files.
197197
198-
.. code-block:: console
199-
200-
$ cat > train.csv << EOF
201-
Feature1,Feature2,TARGET
202-
0.93,0.68,3.89
203-
0.24,0.42,1.75
204-
0.36,0.68,2.75
205-
0.53,0.31,2.00
206-
0.29,0.25,1.32
207-
0.29,0.52,2.14
208-
EOF
209-
$ cat > test.csv << EOF
210-
Feature1,Feature2,TARGET
211-
0.57,0.84,3.65
212-
0.95,0.19,2.46
213-
0.23,0.15,0.93
214-
EOF
215-
$ dffml train \\
216-
-model tfdnnr \\
217-
-model-epochs 300 \\
218-
-model-steps 2000 \\
219-
-model-predict TARGET:float:1 \\
220-
-model-hidden 8 16 8 \\
221-
-sources s=csv \\
222-
-source-filename train.csv \\
223-
-model-features \\
224-
Feature1:float:1 \\
225-
Feature2:float:1 \\
226-
-log debug
227-
Enabling debug log shows tensorflow losses...
228-
$ dffml accuracy \\
229-
-model tfdnnr \\
230-
-model-predict TARGET:float:1 \\
231-
-model-hidden 8 16 8 \\
232-
-sources s=csv \\
233-
-source-filename test.csv \\
234-
-model-features \\
235-
Feature1:float:1 \\
236-
Feature2:float:1 \\
237-
-log critical
198+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/train_data.sh
199+
200+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/test_data.sh
201+
202+
Train the model
203+
204+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/train.sh
205+
206+
Assess the accuracy
207+
208+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/accuracy.sh
209+
210+
Output
211+
212+
.. code-block::
213+
238214
0.9468210011
239-
$ echo -e 'Feature1,Feature2,TARGET\\n0.21,0.18,0.84\\n' | \\
240-
dffml predict all \\
241-
-model tfdnnr \\
242-
-model-predict TARGET:float:1 \\
243-
-model-hidden 8 16 8 \\
244-
-sources s=csv \\
245-
-source-filename /dev/stdin \\
246-
-model-features \\
247-
Feature1:float:1 \\
248-
Feature2:float:1 \\
249-
-log critical
215+
216+
Make a prediction
217+
218+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/predict.sh
219+
220+
Output
221+
222+
.. code-block:: json
223+
250224
[
251225
{
252226
"extra": {},
@@ -266,6 +240,10 @@ class DNNRegressionModel(Model):
266240
}
267241
]
268242
243+
Example usage of Tensorflow DNNEstimator model using python API
244+
245+
.. literalinclude:: /../model/tensorflow/examples/tfdnnr/tfdnnr.py
246+
269247
The ``NaN`` in ``confidence`` is the expected behaviour. (See TODO in
270248
predict).
271249

model/tensorflow/examples/tfdnnr/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dffml accuracy \
2+
-model tfdnnr \
3+
-model-predict TARGET:float:1 \
4+
-model-hidden 8 16 8 \
5+
-sources s=csv \
6+
-source-filename test.csv \
7+
-model-features \
8+
Feature1:float:1 \
9+
Feature2:float:1 \
10+
-log critical
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
echo -e 'Feature1,Feature2,TARGET\n0.21,0.18,0.84\n' | \
2+
dffml predict all \
3+
-model tfdnnr \
4+
-model-predict TARGET:float:1 \
5+
-model-hidden 8 16 8 \
6+
-sources s=csv \
7+
-source-filename /dev/stdin \
8+
-model-features \
9+
Feature1:float:1 \
10+
Feature2:float:1 \
11+
-log critical
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cat > test.csv << EOF
2+
Feature1,Feature2,TARGET
3+
0.57,0.84,3.65
4+
0.95,0.19,2.46
5+
0.23,0.15,0.93
6+
EOF
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.assertRegex(lines[0], r"Accuracy: [-+]?\d*\.?\d+|\d+")
35+
# Check the TARGET
36+
self.assertIsInstance(round(ast.literal_eval(lines[1])["TARGET"]), int)
37+
38+
def test_python_filenames(self):
39+
with directory_with_csv_files() as tempdir:
40+
self.python_test("tfdnnr.py")
41+
42+
def test_shell(self):
43+
with directory_with_csv_files() as tempdir:
44+
# Run training
45+
subprocess.check_output(["bash", sh_filepath("train.sh")])
46+
# Check the Accuracy
47+
stdout = subprocess.check_output(
48+
["bash", sh_filepath("accuracy.sh")]
49+
)
50+
self.assertRegex(stdout.decode().strip(), r"[-+]?\d*\.?\d+|\d+")
51+
# Make the prediction
52+
stdout = subprocess.check_output(
53+
["bash", sh_filepath("predict.sh")]
54+
)
55+
records = json.loads(stdout.decode())
56+
# Check the TARGET
57+
self.assertIsInstance(
58+
round(records[0]["prediction"]["TARGET"]["value"]), int
59+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from dffml import CSVSource, Features, DefFeature
2+
from dffml.noasync import train, accuracy, predict
3+
from dffml_model_tensorflow.dnnr import DNNRegressionModel
4+
5+
model = DNNRegressionModel(
6+
features=Features(
7+
DefFeature("Feature1", float, 1), DefFeature("Feature2", float, 1),
8+
),
9+
predict=DefFeature("TARGET", float, 1),
10+
epochs=300,
11+
steps=2000,
12+
hidden=[8, 16, 8],
13+
)
14+
15+
# Train the model
16+
train(model, "train.csv")
17+
18+
# Assess accuracy (alternate way of specifying data source)
19+
print("Accuracy:", accuracy(model, CSVSource(filename="test.csv")))
20+
21+
# Make prediction
22+
for i, features, prediction in predict(
23+
model, {"Feature1": 0.21, "Feature2": 0.18, "TARGET": 0.84},
24+
):
25+
features["TARGET"] = prediction["TARGET"]["value"]
26+
print(features)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dffml train \
2+
-model tfdnnr \
3+
-model-epochs 300 \
4+
-model-steps 2000 \
5+
-model-predict TARGET:float:1 \
6+
-model-hidden 8 16 8 \
7+
-sources s=csv \
8+
-source-filename train.csv \
9+
-model-features \
10+
Feature1:float:1 \
11+
Feature2:float:1 \
12+
-log debug

0 commit comments

Comments
 (0)