ruhong
ruhong

Reputation: 1903

How does tensorflow autograph work with tf.placeholder as function argument?

How to pass tf.placeholder as argument into a python function transformed by autograph?

from tensorflow.contrib import autograph

@autograph.convert()
def foo(s):
    sep = ' '
    res = s.split(sep)
    return sep.join(res)

x = tf.placeholder(tf.string, shape=[])
y = foo(x)

gives the following error when I attempt to export the graph with tf.saved_model.simple_save:

tensorflow.contrib.autograph.pyct.transformer.AutographParseError: AttributeError: Tensor("Placeholder:0", shape=(), dtype=string) has no attribute split Offending source: s.split

print(autograph.to_code(foo)) shows the following. I wish I could write a python function that handles the argument s as a string instead of a Tensor.

def tf__foo(s):
  try:
    with tf.name_scope('foo'):
      sep = ' '
      res = ag__.converted_call(s.split, True, False, {}, sep)
      return ag__.converted_call(sep.join, True, False, {}, res)
  except:
    ag__.rewrite_graph_construction_error(ag_source_map__)

Traceback (most recent call last): File "/var/folders/jc/0jvly0mn6sb5rk92tst0rgnr0000gn/T/tmp5pj2fv2o.py", line 7, in tf__foo res = ag__.converted_call(s.split, True, False, {}, sep) AttributeError: 'Tensor' object has no attribute 'split'

Notes

Upvotes: 2

Views: 288

Answers (1)

P-Gn
P-Gn

Reputation: 24651

Autograph just does not convert any python code to tensorflow operations. It focuses (for now?) on control flow -- especially while_loops, which are really something.

So to split a string in autograph you still need to call good old tf.string_split.

Actually since your function does not contain any control flow operation, it does not really benefit from autograph features.

Upvotes: 1

Related Questions