tensorflow serving uninitialized

173 Views Asked by At

Hello I want to initialize variable named result in the code below. I tried to initialize with this code* when I tried to serving.

sess.run(tf.global_variables_initializer(),feed_dict= {userLat:0,userLon:0})

I just want to initialize the variable.

The reason for using the variable is to write validate_shape = false.

The reason for using this option is to resolve error 'Outer dimension for outputs must be unknown, outer dimension of 'Variable:0' is 1' when deploying the model version to the Google Cloud ml engine.

Initialization with the following code will output a value when feed_dict is 0 when attempting a prediction.

sess.run(tf.global_variables_initializer(),feed_dict= {userLat:0,userLon:0})

Is there a way to simply initialize the value of result?

Or is it possible to store the list of stored tensor values as a String with a comma without shape?

It's a very basic question. I'm sorry. I am a beginner of the tensor flow. I need help. Thank you for reading.

import tensorflow as tf
import sys,os

#define filename queue
filenameQueue =tf.train.string_input_producer(['./data.csv'],
shuffle=False,name='filename_queue')

# define reader
reader = tf.TextLineReader()
key,value = reader.read(filenameQueue)

#define decoder
recordDefaults = [ ["null"],[0.0],[0.0]]
sId,lat, lng = tf.decode_csv(
value, record_defaults=recordDefaults,field_delim=',')

taxiData=[]

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    for i in range(18):
        data=sess.run([sId, lat, lng])
        tmpTaxiData=[]
        tmpTaxiData.append(data[0])
        tmpTaxiData.append(data[1])
        tmpTaxiData.append(data[2])
        taxiData.append(tmpTaxiData)
    coord.request_stop()
    coord.join(threads)   

from math import sin, cos,acos, sqrt, atan2, radians 

#server input data
userLat = tf.placeholder(tf.float32, shape=[])
userLon = tf.placeholder(tf.float32, shape=[])

R = 6373.0  
radian=0.017453292519943295


distanceList=[]

for i in taxiData:
  taxiId=tf.constant(i[0],dtype=tf.string,shape=[])
  taxiLat=tf.constant(i[1],dtype=tf.float32,shape=[])
  taxiLon=tf.constant(i[2],dtype=tf.float32,shape=[])



distanceValue=6371*tf.acos(tf.cos(radian*userLat)*
tf.cos(radian*taxiLat)*tf.cos(radian*taxiLon-
radian*126.8943311)+tf.sin(radian*37.4685225)*tf.sin(radian*taxiLat))

  tmpDistance=[]

  tmpDistance.append(taxiId)
  tmpDistance.append(distanceValue)

  distanceList.append(tmpDistance)


# result sort
sId,distances=zip(*distanceList)
indices = tf.nn.top_k(distances, k=len(distances)).indices

gather=tf.gather(sId, indices[::-1])[0:5]
result=tf.Variable(gather,validate_shape=False)

print "Done training!"


# serving

import os
from tensorflow.python.util import compat

model_version = 1
path = os.path.join("Taximodel", str(model_version))
builder = tf.saved_model.builder.SavedModelBuilder(path)

with tf.Session() as sess:
    builder.add_meta_graph_and_variables(
    sess,
        [tf.saved_model.tag_constants.SERVING],
        signature_def_map= {
            "serving_default": 
tf.saved_model.signature_def_utils.predict_signature_def(
                inputs= {"userLat": userLat, "userLon":userLon},
                outputs= {"result": result})
    })

builder.save()

print 'Done exporting'
1

There are 1 best solutions below

0
On

You can try to define the graph so that the output tensor preserves the shape (outer dimension) of the input tensor.

For example, something like:

#server input data
userLoc = tf.placeholder(tf.float32, shape=[None, 2])

def calculate_dist(user_loc):
  distanceList = []
  for i in taxiData:
    taxiId=tf.constant(i[0],dtype=tf.string,shape=[])
    taxiLat=tf.constant(i[1],dtype=tf.float32,shape=[])
    taxiLon=tf.constant(i[2],dtype=tf.float32,shape=[])
    distanceValue=6371*tf.acos(tf.cos(radian*user_loc[0])*
        tf.cos(radian*taxiLat)*tf.cos(radian*taxiLon-
        radian*126.8943311)+tf.sin(radian*37.4685225)*tf.sin(radian*taxiLat))
    tmpDistance=[]
    tmpDistance.append(taxiId)
    tmpDistance.append(distanceValue)
    distanceList.append(tmpDistance)
  # result sort
  sId,distances=zip(*distanceList)
  indices = tf.nn.top_k(distances, k=len(distances)).indices
  return tf.gather(sId, indices[::-1])[0:5]

result = tf.map_fn(calculate_dist, userLoc)