對於傳輸學習,通常使用網絡作爲特徵提取器來創建要素的數據集,在該數據集上訓練另一個分類器(例如, SVM)。TensorFlow:tf.contrib.data中的「無法通過值捕獲有狀態節點」API
我想實現這個使用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.layers
,我不能簡單地將它們創建爲常量,並且設置trainable=False
既不會創建常量,也不會將變量添加到GraphKeys.TRAINABLE_VARIABLES
集合中。