Open Crispy13 opened 3 years ago
Ran in Colab - getting the same problem. It's to do with the add_loss function. add_loss(0.05) throws the same error. add_loss(lambda: 0.05) doesn't throw an error
including the recon_loss always throws the error
Hi @GeorgeEngland ,
Thanks for your feedback. Unfortunately, due to an issue introduced in TF 2.2 (#46858), it is currently not possible to use add_loss()
along with the build()
method. So I updated the code in the notebook: I now create the reconstruct layer in the constructor instead of the build()
method. Unfortunately, this means that the number of units in this layer must be hard-coded (alternatively, it could be passed as an argument to the constructor).
Here's the updated code:
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.reconstruct = keras.layers.Dense(8) # workaround for TF issue #46858
self.reconstruction_mean = keras.metrics.Mean(name="reconstruction_error")
#Commented out due to TF issue #46858, see the note above
#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:
result = self.reconstruction_mean(recon_loss)
self.add_metric(result)
return self.out(Z)
In Chapter 12,
Making with the above code and training it raised error: