ngdlm
ngdlm copied to clipboard
TypeError: unsupported operand type(s) for *=: 'NoneType' and 'int'
input_shape = (28, 28)
latent_dim = 128
encoder_input = layers.Input(shape=input_shape)
encoder_output = encoder_input
encoder_output = layers.Reshape(input_shape + (1,))(encoder_input)
encoder_output = layers.Conv2D(16, (3, 3), activation="relu", padding="same")(encoder_output)
encoder_output = layers.MaxPooling2D((2, 2), padding="same")(encoder_output)
encoder_output = layers.Conv2D(8, (3, 3), activation="relu", padding="same")(encoder_output)
encoder_output = layers.MaxPooling2D((2, 2), padding="same")(encoder_output)
encoder_output = layers.Conv2D(8, (3, 3), activation="relu", padding="same")(encoder_output)
encoder_output = layers.MaxPooling2D((2, 2), padding="same")(encoder_output)
encoder_output = layers.Flatten()(encoder_output)
encoder = models.Model(encoder_input, encoder_output)
# Create the decoder.
decoder_input = layers.Input(shape=(latent_dim,))
decoder_output = decoder_input
#decoder_output = layers.Dense(128, activation="relu")(decoder_output)
decoder_output = layers.Reshape((4, 4, 8))(decoder_output)
decoder_output = layers.Conv2D(8, (3, 3), activation="relu", padding="same")(decoder_output)
decoder_output = layers.UpSampling2D((2, 2))(decoder_output)
decoder_output = layers.Conv2D(8, (3, 3), activation="relu", padding="same")(decoder_output)
decoder_output = layers.UpSampling2D((2, 2))(decoder_output)
decoder_output = layers.Conv2D(16, (3, 3), activation="relu")(decoder_output)
decoder_output = layers.UpSampling2D((2, 2))(decoder_output)
decoder_output = layers.Conv2D(1, (3, 3), activation="sigmoid", padding="same")(decoder_output)
decoder_output = layers.Reshape((28, 28))(decoder_output)
decoder = models.Model(decoder_input, decoder_output)
# Create the VAE.
vae = ngdlmodels.VAE(encoder, decoder, latent_dim=latent_dim)
vae.compile(optimizer='adadelta', reconstruction_loss="binary_crossentropy")
vae.summary()
# Train.
print("Train...")
history = vae.fit(
x_input_train, x_input_train,
epochs=100,
batch_size=32,
shuffle=True,
validation_data=(x_input_test, x_input_test)
)
# Evaluate.
print("Evaluate...")
loss = vae.model.evaluate(x_input_test, x_input_test)
print("Loss:", loss)
Throws error
TypeError Traceback (most recent call last)
<ipython-input-22-1acf0e514e9b> in <module>()
31 # Create the VAE.
32 vae = ngdlmodels.VAE(encoder, decoder, latent_dim=latent_dim)
---> 33 vae.compile(optimizer='adadelta', reconstruction_loss="binary_crossentropy")
34 vae.summary()
35
~/anaconda3/lib/python3.6/site-packages/ngdlm/models.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
433 # Compile model.
434 loss = vae_loss
--> 435 self.autoencoder.compile(optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, **kwargs)
436
437
~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
330 with K.name_scope(self.output_names[i] + '_loss'):
331 output_loss = weighted_loss(y_true, y_pred,
--> 332 sample_weight, mask)
333 if len(self.outputs) > 1:
334 self.metrics_tensors.append(output_loss)
~/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py in weighted(y_true, y_pred, weights, mask)
401 """
402 # score_array has ndim >= 2
--> 403 score_array = fn(y_true, y_pred)
404 if mask is not None:
405 # Cast the mask to floatX to avoid float64 upcasting in Theano
~/anaconda3/lib/python3.6/site-packages/ngdlm/models.py in vae_loss(loss_inputs, loss_outputs)
419 else:
420 r_loss = self.loss
--> 421 r_loss *= inputs_dim
422
423 # kl loss.
TypeError: unsupported operand type(s) for *=: 'NoneType' and 'int'
Thanks!
We have decided to not use "reconstruction_loss" anymore. Instead we use "loss" again.