tensorflow签名如何与tf.placeholder一起作为函数参数?

问题描述 投票:0回答:1

如何将tf.placeholder作为参数传递给由autograph转换的python函数?

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)

当我尝试使用tf.saved_model.simple_save导出图形时出现以下错误:

tensorflow.contrib.autograph.pyct.transformer.AutographParseError:AttributeError:Tensor(“占位符:0”,形状=(),dtype =字符串)没有属性拆分违规来源:s.split

print(autograph.to_code(foo))显示以下内容。我希望我能编写一个python函数来处理参数s作为字符串而不是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__)

回溯(最近一次调用最后一次):文件“/var/folders/jc/0jvly0mn6sb5rk92tst0rgnr0000gn/T/tmp5pj2fv2o.py”,第7行,在tf__foo res = ag __。converted_call(s.split,True,False,{},sep) AttributeError:'Tensor'对象没有属性'split'

笔记

  • 导出图表以进行预测。因此,无需向占位符提供数据。
  • Eager execution未启用,因为它与tf.placeholder不兼容
  • 在tensorflow 1.10,python 3.5
tensorflow
1个回答
1
投票

Autograph只是不将任何python代码转换为tensorflow操作。它(现在?)关注控制流程 - 特别是while_loops,它们确实是某种东西。

因此,要在签名中分割一个字符串,你仍然需要调用好旧的tf.string_split

实际上,由于您的功能不包含任何控制流操作,因此它不会真正受益于签名功能。

© www.soinside.com 2019 - 2024. All rights reserved.