我正在尝试使用 tf.Dataset.cache 但它似乎没有影响。
我有3个问题请教:
你想在什么时候缓存你的数据集?我假设它将在任何具有随机行为的映射操作之前。是否建议在任何其他映射之前从 TFRecord 文件进行初始解析后缓存数据集?
如何衡量缓存对速度优化的影响?
我会假设我总是想将我的图像缓存到内存中。至少其中的一部分,并让管道更快地馈送网络。我什么时候要缓存到文件?
谢谢!
TF1.4使Keras成为不可或缺的一部分.当尝试使用具有propratery输入功能的Keras模型创建Estimators时(即,不使用tf.estimator.inputs.numpy_input_fn)事情不起作用,因为Tensorflow无法将模型与Input函数融合.
我使用的是tf.keras.estimator.model_to_estimator
keras_estimator = tf.keras.estimator.model_to_estimator(
keras_model = keras_model,
config = run_config)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn,
max_steps=self.train_steps)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn,
steps=None)
tf.estimator.train_and_evaluate(keras_estimator, train_spec, eval_spec)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Cannot find %s with name "%s" in Keras Model. It needs to match '
'one of the following:
Run Code Online (Sandbox Code Playgroud)
我在这里找到了一些关于这个主题的参考资料(奇怪的是它隐藏在主分支的TF文档中 - 与此相比)
如果您有同样的问题 - 请参阅下面的答案.可能会为您节省几个小时.
[在@mrry评论之后编辑#1] 我正在使用(伟大和惊人的)数据集API以及tf.contrib.data.rejection_resample来为输入训练管道设置特定的分布函数.
在将tf.contrib.data.rejection_resample添加到input_fn之前,我使用了一次性Iterator.唉,当开始使用后者时,我尝试使用dataset.make_initializable_iterator() - 这是因为我们引入了管道状态变量,并且在输入管道中的所有变量都是init之后需要初始化迭代器.正如@mrry在这里写的那样.
我将input_fn传递给估算器并由实验包装.
问题是 - 在哪里挂钩迭代器的init?如果我尝试:
dataset = dataset.batch(batch_size)
if self.balance:
dataset = tf.contrib.data.rejection_resample(dataset, self.class_mapping_function, self.dist_target)
iterator = dataset.make_initializable_iterator()
tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer)
else:
iterator = dataset.make_one_shot_iterator()
image_batch, label_batch = iterator.get_next()
print (image_batch)
Run Code Online (Sandbox Code Playgroud)
和映射功能:
def class_mapping_function(self, feature, label):
"""
returns a a function to be used with dataset.map() to return class numeric ID
The function is mapping a nested structure of tensors (having shapes and types defined by dataset.output_shapes
and dataset.output_types) to a scalar tf.int32 tensor. …Run Code Online (Sandbox Code Playgroud)