as_list() 未在 y_t_rank = len(y_t.shape.as_list()) 上的未知 TensorShape 上定义并且与指标相关 [英] as_list() is not defined on an unknown TensorShape on y_t_rank = len(y_t.shape.as_list()) and related to metrics
问题描述
TF 2.3.0.dev20200620
TF 2.3.0.dev20200620
对于带有 sigmoid 二进制输出的模型,我在 .fit(...) 期间遇到了这个错误.我使用 tf.data.Dataset 作为输入管道.奇怪的是它取决于指标:
I got this error during .fit(...) for a model with a sigmoid binary output. I used tf.data.Dataset as the input pipeline. The strange thing is it depends on the metric:
不工作:
model.compile(
optimizer=tf.keras.optimizers.Adam(lr=1e-4, decay=1e-6),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy']
)
工作:
model.compile(
optimizer=tf.keras.optimizers.Adam(lr=1e-4, decay=1e-6),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()]
)
但据我所知,准确性"应该没问题.事实上,使用 tf.keras.preprocessing.image_dataset_from_directory 而不是使用我自己的 tf.data.Dataset 自定义设置(如果需要可以提供),不会出现这样的错误.这是教程 https://keras.io/examples/vision/image_classification_from_scratch 中的情况.
But as I understood, 'accuracy' should be fine. In fact, instead of using my own tf.data.Dataset custom setup (can be provided if needed), using tf.keras.preprocessing.image_dataset_from_directory give no such error. This is the case from tutorial https://keras.io/examples/vision/image_classification_from_scratch.
跟踪粘贴在下面.请注意,这与其他 2 个较旧的问题不同.它以某种方式涉及指标.
Trace is pasted below. Notice this is diff from other 2 older questions. it involves somehow the metrics.
值错误:在用户代码中:
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2526 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2886 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:759 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:388 update_state
self.build(y_pred, y_true)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:319 build
self._metrics, y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1139 map_structure_up_to
**kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1235 map_structure_with_tuple_paths_up_to
*flat_value_lists)]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1234 <listcomp>
results = [func(*args, **kwargs) for args in zip(flat_path_list,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1137 <lambda>
lambda _, *values: func(*values), # Discards the path arg.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:419 _get_metric_objects
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:419 <listcomp>
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:440 _get_metric_object
y_t_rank = len(y_t.shape.as_list())
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:1190 as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
推荐答案
在使用准确度"指标时遇到了完全相同的问题.
Had exactly the same problem when using 'accuracy' metric.
我关注了https://github.com/tensorflow/tensorflow/issues/32912#issuecomment-550363802 示例:
def _fixup_shape(images, labels, weights):
images.set_shape([None, None, None, 3])
labels.set_shape([None, 19]) # I have 19 classes
weights.set_shape([None])
return images, labels, weights
dataset = dataset.map(_fixup_shape)
帮助我解决了问题.
但是,在我的例子中,不是像上面 kawingkelvin 那样使用一个 map 函数来加载和 set_shape,我需要使用两个 map 函数,因为 TF 代码中存在一些错误.
But, in my case, instead of using one map function, as kawingkelvin did above, to load and set_shape inside, I needed to use two map functions because of some errors in the TF code.
我的最终解决方案是使用以下顺序:dataset.batch.map(get_data).map(fix_shape).prefetch
The final solution for me was to use the following order:
dataset.batch.map(get_data).map(fix_shape).prefetch
注意:批处理可以在 map(get_data) 之前和之后完成,具体取决于 get_data 函数的创建方式.Fix_shape 必须在之后完成.
NOTE: batch can be done both before and after map(get_data) depending on how your get_data function is created. Fix_shape must be done after.
这篇关于as_list() 未在 y_t_rank = len(y_t.shape.as_list()) 上的未知 TensorShape 上定义并且与指标相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!