python - 如何在 py_func 中访问 Tensorflow Graph?
问题描述
我正在尝试get_default_graph
从py_func
. 我的代码片段如下所示:
def test():
tf.get_default_graph().get_tensor_by_name("Variable_1:0")
t = tf.py_func(test, [], tf.float32)
我收到一个错误:
KeyError:“名称‘Variable_1:0’指的是一个不存在的张量。操作‘Variable_1’在图中不存在。”
但我确实命名了一个名为Variable_1
. 我的假设是在py_func
.
如何使上述代码段起作用?
解决方案
根据tf.py_func()的定义和给定示例,您需要传递输入并指定输出的数据类型(如果有多个)。
您的代码将需要像这样重写:
def test():
# Do whatever computation and return a variable of type float
inp = tf.get_default_graph().get_tensor_by_name("Variable_1:0")
t = tf.py_func(test, [inp], tf.float32)
推荐阅读
- php - PHP 使用 getimagesize() 获取图像高度和宽度
- ruby-on-rails - Xero - 接收 Ruby 的意图
- reactjs - ReactJs TypeScript:语法错误:意外标记,预期;
- javascript - 未发送 Mixpanel 事件
- python - 从给定字符串中提取文件名所需的正则表达式
- javascript - 全部更改
具有类的元素
- python - 在 GPS 数据上运行 DBSCAN:内存错误
- spring - 我可以将相同的@transaction 与许多 applicationContext 一起使用吗?
- haskell - 需要 Haskell 柯里化解释
- php - 如何在 PHP 中生成项目事件完成时间线