TomS
TomS

Reputation: 31

Correctly formatting JSON for tensorflow serving LSTM

I've got a tensorflow LSTM model where the predict function where the input is 100 rows with 5 columns. The shape when a numpy array is 1,100,5.

I'm struggling on how I correctly format a JSON request to send this to a tensorflow serving endpoint. Current hosted on google cloud but probably end up being the tensorflow serving docker image.

Any help on how to correctly format data to send to TF would be greatly appricieated as I'm struggling to find the correct formatting in their documentation.

Thanks!

Upvotes: 2

Views: 248

Answers (1)

SequenceToSequence
SequenceToSequence

Reputation: 514

A json file can be loaded as a dict. So lets say you have 5 features you could format it like this:

  {
   "feature1": [...],
   "feature2": [...],
   "feature3": [...],
   "feature4": [...],
   "feature5": [...]
  }

Then you can convert the dict to a list of lists and then convert that to a numpy array. Note: If you store your arrays as strings you can convert them using list comprehension.

with open('data.json') as json_file:
    data = json.load(json_file)
    # if list of strings
    f1 = [float(i) for i in data['feature1']]
    f2 = [float(i) for i in data['feature2']]
    f3 = [float(i) for i in data['feature3']]
    f4 = [float(i) for i in data['feature3']]
    f5 = [float(i) for i in data['feature5']]

    sample = np.array([f1,f2,f3,f4,f5])

Upvotes: 2

Related Questions