0

I am trying to create a model graph where my input is tensorflow variable which I am inputting from my java program In my code, I am using numpy methods where I need to convert my tensorflow variable input to numpy array input

Here, is my code snippet

import tensorflow as tf
import numpy as np
eps = np.finfo(float).eps
EXPORT_DIR = './model'

def standardize(x):
   med0 = np.median(x)
   mad0 = np.median(np.abs(x - med0))
   x1 = (x - med0) / (mad0 + eps)
   return x1

#tensorflow input variable
a = tf.placeholder(tf.float32, name="input")
with tf.Session() as session:
session.run(tf.global_variables_initializer())
 #Converting the input variable to numpy array
 tensor = a.eval()

 #calling standardize method
 numpyArray = standardize(tensor)

 #converting numpy array to tf
 tf.convert_to_tensor(numpyArray)

 #creating graph
 graph = tf.get_default_graph()
 tf.train.write_graph(graph, EXPORT_DIR, 'model_graph.pb', as_text=False)

I am getting error: InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input' with dtype float in line tensor = a.eval()

When I am giving constant value in place of placeholder then it's working and generating the graph. But I want to input from my java code. Is there any way to do that or do I need to convert all my numpy methods to tensorflow methods

Silky
  • 21
  • 5
  • Are you just converting to numpy and back to standardize? I would rather do that directly in TF. Nevertheless what might be missing above is the feed_dict. Try a.eval(feed_dict={a: }) – sladomic Jan 18 '18 at 16:00
  • Actually, I don't have any values to feed right now because I am using this as model for my android application. Please refer [link](https://blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc) for more information – Silky Jan 19 '18 at 02:14

1 Answers1

0

placeholder is just an empty variable in tensorflow, to which you can feed numpy values. Now, what you are trying to do does not make sense. You can not get value out of an empty variable.

If you want to standardize your tensor, why convert it to numpy var first? You can directly do this using tensorflow.

The following taken from this stackoverflow ans

def get_median(v):
    v = tf.reshape(v, [-1])
    m = v.get_shape()[0]//2
    return tf.nn.top_k(v, m).values[m-1]

Now, you can implement your function as

def standardize(x):
    med0 = get_median(x)
    mad0 = get_median(tf.abs(x - med0))
    x1 = (x - med0)/(mad0 + eps)
    return x1
layog
  • 4,661
  • 1
  • 28
  • 30
  • Its not just the standardize method but there are various other methods which are using numpy.I had already tried the above approach but there was a difference in output values. Thanks for the solution – Silky Jan 19 '18 at 02:01