I trained a auto-encoder model before and saved decoder model. Next, I train a new model that labeled as 'netA', I want to use decoder model in custom loss function. and tried, but I got error, there was my code and error information:
def custom_loss(y_true,y_pred):
a = decoder(y_pred)
b = decoder(y_true)
c = K.mean(K.square(a-b))
return c
input_feature = 409
output_feature = 256
model = Sequential()
model.add(Dense(256, activation = 'relu',input_shape=(input_feature,)))
model.add(Dense(128, activation = 'relu'))
model.add(Dense(64))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(128,activation='relu'))
model.add(Dense(output_feature,activation='sigmoid'))
model.summary()
model.compile(optimizer = Adam(lr = 1e-4),loss=custom_loss, metrics = ['mse'])
history = model.fit(x_train_pca_scale, y_train_scale_coding, epochs = 200, batch_size = 32, verbose= 2,validation_data = (x_test_pca_scale, y_test_scale_coding))
the error is :
AssertionError Traceback (most recent call last) in 23 24 model.summary() ---> 25 model.compile(optimizer = Adam(lr = 1e-4),loss=custom_loss, metrics = ['mse']) 26 #checkpointer = ModelCheckpoint(filepath='/home/lidan/3DFacePrediction/gene.face.autoencoder/gene.face.min.val_loss.hd5', monitor='val_loss',verbose=1,mode='min',save_best_only=True) 27
~/software/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs) 73 if _SYMBOLIC_SCOPE.value: 74 with get_graph().as_default(): ---> 75 return func(*args, **kwargs) 76 else: 77 return func(*args, **kwargs)
~/software/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs) 227 # loss_weight_2 * output_2_loss_fn(...) + 228 # layer losses. --> 229 self.total_loss = self._prepare_total_loss(masks) 230 231 # Functions for train, test and predict will
~/software/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in _prepare_total_loss(self, masks) 690 691 output_loss = loss_fn( --> 692 y_true, y_pred, sample_weight=sample_weight) 693 694 if len(self.outputs) > 1:
~/software/anaconda3/lib/python3.7/site-packages/keras/losses.py in call(self, y_true, y_pred, sample_weight) 69 scope_name = 'lambda' if self.name == '' else self.name 70 with K.name_scope(scope_name): ---> 71 losses = self.call(y_true, y_pred) 72 return losses_utils.compute_weighted_loss( 73 losses, sample_weight, reduction=self.reduction)
~/software/anaconda3/lib/python3.7/site-packages/keras/losses.py in call(self, y_true, y_pred) 130 Loss values per sample. 131 """ --> 132 return self.fn(y_true, y_pred, **self._fn_kwargs) 133 134 def get_config(self):
in custom_loss(y_true, y_pred) 3 def custom_loss(y_true,y_pred): 4 a = decoder(y_pred) ----> 5 b = decoder(y_true) 6 c = K.mean(K.square(a-b)) 7 return c
~/software/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs) 73 if _SYMBOLIC_SCOPE.value: 74 with get_graph().as_default(): ---> 75 return func(*args, **kwargs) 76 else: 77 return func(*args, **kwargs)
~/software/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs) 487 # Actually call the layer, 488 # collecting output(s), mask(s), and shape(s). --> 489 output = self.call(inputs, **kwargs) 490 output_mask = self.compute_mask(inputs, previous_mask) 491
~/software/anaconda3/lib/python3.7/site-packages/keras/engine/network.py in call(self, inputs, mask) 581 return self._output_tensor_cache[cache_key] 582 else: --> 583 output_tensors, _, _ = self.run_internal_graph(inputs, masks) 584 return output_tensors 585
~/software/anaconda3/lib/python3.7/site-packages/keras/engine/network.py in run_internal_graph(self, inputs, masks) 796 input_shapes = unpack_singleton( 797 [x._keras_shape for x in computed_tensors]) --> 798 shapes = to_list(layer.compute_output_shape(input_shapes)) 799 uses_learning_phase = any( 800 [x._uses_learning_phase for x in computed_tensors])
~/software/anaconda3/lib/python3.7/site-packages/keras/layers/core.py in compute_output_shape(self, input_shape) 915 def compute_output_shape(self, input_shape): 916 assert input_shape and len(input_shape) >= 2 --> 917 assert input_shape[-1] 918 output_shape = list(input_shape) 919 output_shape[-1] = self.units
AssertionError:
I felt confused about the error information,because the decoder model worked well in y_pred and failed in y_true.
Could someone help me solve it or just give me another way to put a saved decoder model in loss function? Thank you very much!