【发布时间】:2021-07-12 22:09:01
【问题描述】:
我正在尝试向 Keras 函数模型 (Tensorflow 2.5) 添加平均指标,但出现以下错误:
ValueError: Expected a symbolic Tensor for the metric value, received: tf.Tensor(0.0, shape=(), dtype=float32)
代码如下:
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [5 + i * 3 for i in x]
a = Input(shape=(1,))
output = Dense(1)(a)
model = Model(inputs=a,outputs=output)
model.add_metric(tf.keras.metrics.Mean()(output))
model.compile(loss='mse')
model.fit(x=x, y=y, epochs=100)
如果我删除以下行(引发异常的行):
model.add_metric(tf.keras.metrics.Mean()(output))
代码按预期工作。
我尝试禁用 Eager Execution,但我收到以下错误:
ValueError: Using the result of calling a `Metric` object when calling `add_metric` on a Functional Model is not supported. Please pass the Tensor to monitor directly.
上述用法几乎是从tf.keras.metrics.Mean 文档中复制而来的(请参阅使用 compile() API)
【问题讨论】:
标签: python tensorflow keras deep-learning metrics