Is there any way to perform the equivalent of gcloud ml-engine local predict --model-dir=$MODEL_DIR --json-instances=$JSON_INSTANCE
within a jupyter notebook?
Asked
Active
Viewed 50 times
0

reese0106
- 2,011
- 2
- 16
- 46
1 Answers
2
Let me give a quick answer; one that may be updated at some point in the future. Basically, this answer should apply. For example:
import json
from tensorflow.contrib import predictor
def columnarize(instancse):
out = {}
for instance in instances:
for k, v in instance.items():
out.setdefault(k, []).append(v)
return out
def mapify(outputs, fetch_tensors):
return dict(zip(fetch_tensors.iterkeys(), outputs))
def rowify(columns):
out = []
num_instances = len(next(columns.itervalues()))
for row in range(num_instances):
out.append({
name: output[row, ...].tolist()
for name, output in columns.iteritems()
})
return out
instances = [
{"x": [6.4, 3.2, 4.5, 1.5], "y": -1},
{"x": [5.8, 3.1, 5.0, 1.7], "y": 5},
]
predict_fn = predictor.from_saved_model(export_dir)
outputs = predict_fn(columnarize(instances))
predictions = rowify(mapify(outputs, predictor._fetch_tensors))
print(predictions)

rhaertel80
- 8,254
- 1
- 31
- 47
-
`rowify()` is the only part that doesn't appear to be working. Basically it seems like you are trying to get the response formatted in the same way the instances went in, correct? – reese0106 Jul 25 '18 at 22:05
-
I added `mapify()`. I think `tensorflow.contrib.predictor` might have a bug, because the docs say it returns a dict, but it doesn't. – rhaertel80 Jul 26 '18 at 17:35