gpt4 book ai didi

TensorFlow:tf.contrib.data API 中的 "Cannot capture a stateful node by value"

转载 作者:行者123 更新时间:2023-12-04 10:14:49 25 4
gpt4 key购买 nike

对于迁移学习,人们通常使用网络作为特征提取器来创建特征数据集,在该数据集上训练另一个分类器(例如 SVM)。

我想使用 Dataset API ( tf.contrib.data ) 和 dataset.map() 来实现这个:

# feature_extractor will create a CNN on top of the given tensor
def features(feature_extractor, ...):
dataset = inputs(...) # This creates a dataset of (image, label) pairs

def map_example(image, label):
features = feature_extractor(image, trainable=False)
# Leaving out initialization from a checkpoint here...
return features, label

dataset = dataset.map(map_example)

return dataset

为数据集创建迭代器时,这样做会失败。
ValueError: Cannot capture a stateful node by value.

这是真的,网络的内核和偏差是变量,因此是有状态的。对于这个特定的例子,他们不必是。

有没有办法制作 Ops,特别是 tf.Variable 对象无状态?

因为我正在使用 tf.layers 我不能简单地将它们创建为常量,并设置 trainable=False也不会创建常量,但不会将变量添加到 GraphKeys.TRAINABLE_VARIABLES收藏。

最佳答案

不幸的是,tf.Variable本质上是有状态的。但是,只有在使用 Dataset.make_one_shot_iterator() 时才会出现此错误。创建迭代器。* 为避免该问题,您可以改用 Dataset.make_initializable_iterator() ,但需要注意的是,您还必须运行 iterator.initializer在返回的迭代器上 运行 tf.Variable 的初始化程序输入管道中使用的对象。

* 此限制的原因是 Dataset.make_one_shot_iterator() 的实现细节以及正在进行的 TensorFlow 函数 (Defun) 支持它用于封装数据集定义。由于使用查找表和变量等有状态资源比我们最初想象的更受欢迎,我们正在寻找放宽此限制的方法。

关于TensorFlow:tf.contrib.data API 中的 "Cannot capture a stateful node by value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com