【发布时间】:2020-06-24 21:43:18
【问题描述】:
我正在关注“Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd Edition - Aurélien Geron”第 12 章的“基于模型内部的损失和指标”部分,他在其中展示了如何添加不依赖于标签和预测的自定义损失和指标。
为了说明这一点,我们添加了一个自定义的“重建损失”,方法是在上层隐藏层的顶部添加一个应该重现输入的层。损失是重建损失和输入之间的均方差。
他展示了添加自定义损失的代码,效果很好,但即使按照他的描述,我也无法添加指标,因为它会引发“ValueError”。他说:
同样,您可以通过以下方式添加基于模型内部的自定义指标 以您想要的任何方式计算它,只要结果是 度量对象。例如,您可以创建一个
keras.metrics.Mean对象 在构造函数中,然后在call()方法中调用它,并传递给它recon_loss,最后通过调用模型的add_metric()方法。
这是代码(我为自己添加的行添加了#MINE)
import tensorflow as tf
from tensorflow import keras
class ReconstructingRegressor(keras.models.Model):
def __init__(self, output_dim, **kwargs):
super().__init__(**kwargs)
self.hidden = [keras.layers.Dense(30, activation="selu",
kernel_initializer="lecun_normal")
for _ in range(5)]
self.out = keras.layers.Dense(output_dim)
self.reconstruction_mean = keras.metrics.Mean(name="reconstruction_error") #MINE
def build(self, batch_input_shape):
n_inputs = batch_input_shape[-1]
self.reconstruct = keras.layers.Dense(n_inputs)
super().build(batch_input_shape)
def call(self, inputs, training=None):
Z = inputs
for layer in self.hidden:
Z = layer(Z)
reconstruction = self.reconstruct(Z)
recon_loss = tf.reduce_mean(tf.square(reconstruction - inputs))
self.add_loss(0.05 * recon_loss)
if training: #MINE
result = self.reconstruction_mean(recon_loss) #MINE
else: #MINE
result = 0. #MINE, I have also tried different things here,
#but the help showed a similar sample to this.
self.add_metric(result, name="foo") #MINE
return self.out(Z)
然后编译和拟合模型:
training_set_size=10
X_dummy = np.random.randn(training_set_size, 8)
y_dummy = np.random.randn(training_set_size, 1)
model = ReconstructingRegressor(1)
model.compile(loss="mse", optimizer="nadam")
history = model.fit(X_dummy, y_dummy, epochs=2)
哪个抛出:
ValueError: in converted code:
<ipython-input-296-878bdeb30546>:26 call *
self.add_metric(result, name="foo") #MINE
C:\Users\Kique\Anaconda3\envs\piz3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py:1147 add_metric
self._symbolic_add_metric(value, aggregation, name)
C:\Users\Kique\Anaconda3\envs\piz3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py:1867 _symbolic_add_metric
'We do not support adding an aggregated metric result tensor that '
ValueError: We do not support adding an aggregated metric result tensor that is not the output of a `tf.keras.metrics.Metric` metric instance. Without having access to the metric instance we cannot reset the state of a metric after every epoch during training. You can create a `tf.keras.metrics.Metric` instance and pass the result here or pass an un-aggregated result with `aggregation` parameter set as `mean`. For example: `self.add_metric(tf.reduce_sum(inputs), name='mean_activation', aggregation='mean')`
阅读后,我尝试了类似的方法来解决该问题,但它只是导致了不同的错误。我该如何解决这个问题?这样做的“正确”方法是什么?
我在 Windows 上使用 conda,安装了 tensorflow-gpu 2.1.0。
【问题讨论】:
标签: tensorflow keras deep-learning keras-layer tf.keras