gpt4 book ai didi

tensorflow - as_list() 未在 y_t_rank = len(y_t.shape.as_list()) 上的未知 TensorShape 上定义并与指标相关

转载 作者:行者123 更新时间:2023-12-04 17:26:54 34 4
gpt4 key购买 nike

TF 2.3.0.dev20200620

我在 .fit(...) 期间为具有 sigmoid 二进制输出的模型遇到此错误。我使用 tf.data.Dataset 作为输入管道。奇怪的是它取决于指标:

不工作:

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 .

跟踪粘贴在下面。请注意,这与其他 2 个较旧的问题不同。它以某种方式涉及指标。

ValueError: 在用户代码中:

/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.

最佳答案

在使用“准确度”指标时遇到了完全相同的问题。

我关注了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)

帮助我解决了问题。

但是,在我的例子中,由于 TF 代码中的一些错误,我需要使用两个映射函数,而不是像上面的 kawingkelvin 那样使用一个映射函数来加载和设置形状。

我的最终解决方案是使用以下顺序:dataset.batch.map(get_data).map(fix_shape).prefetch

注意:批处理可以在 map(get_data) 之前和之后完成,具体取决于 get_data 函数的创建方式。 Fix_shape 必须在之后完成。

关于tensorflow - as_list() 未在 y_t_rank = len(y_t.shape.as_list()) 上的未知 TensorShape 上定义并与指标相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62585490/

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