对于转移学习,通常使用网络作为特征提取器来创建特征的数据集,在该数据集上训练另一个分类器(例如,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
Run Code Online (Sandbox Code Playgroud)
在为数据集创建迭代器时,执行此操作会失败.
ValueError: Cannot capture a stateful node by value.
Run Code Online (Sandbox Code Playgroud)
这是事实,网络的内核和偏见是变量,因此是有状态的.对于这个特殊的例子,他们不一定是这样.
有没有办法让Ops和特定tf.Variable对象无状态?
因为我正在使用tf.layers我不能简单地将它们创建为常量,并且设置trainable=False既不会创建常量,也不会将变量添加到GraphKeys.TRAINABLE_VARIABLES集合中.
我很难尝试使用reject_resample()和Dataset API进行一些平衡批处理。我可以使用图像和标签(整数)作为输入,因为您可以浏览一下代码,但是reject_resample()似乎无法正常工作。
注意:我正在使用Tensorflow v1.3
在这里,我定义数据集,数据集的分布以及所需的分布。
target_dist = [0.1, 0.0, 0.0, 0.0, 0.9]
initial_dist = [0.1061, 0.3213, 0.4238, 0.1203, 0.0282]
training_filenames = training_records
training_dataset = tf.contrib.data.TFRecordDataset(training_filenames)
training_dataset = training_dataset.map(tf_record_parser) # Parse the record into tensors.
training_dataset = training_dataset.repeat() # number of epochs
training_dataset = training_dataset.shuffle(buffer_size=1000)
training_dataset = tf.contrib.data.rejection_resample(training_dataset,
class_func=lambda _, c: c,
target_dist=target_dist,
initial_dist=initial_dist)
# Return to the same Dataset shape as was the original input
training_dataset = training_dataset.map(lambda _, data: (data))
training_dataset = training_dataset.batch(64)
handle = tf.placeholder(tf.string, shape=[]) …Run Code Online (Sandbox Code Playgroud)