如何在keras的自定义损失函数中调用经过预训练的解码器模型?

时间:2020-05-25 10:29:34

标签: keras loss-function

我之前训练过自动编码器模型,并保存了解码器模型。接下来,我训练了一个标记为“ netA”的新模型,我想在自定义损失函数中使用解码器模型。并尝试过,但出现错误,其中有我的代码和错误信息:

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))

错误是:

AssertionError Traceback(最近一次通话) 在 23 24 model.summary() ---> 25 model.compile(optimizer = Adam(lr = 1e-4),loss = custom_loss,metrics = ['mse']) 26 #checkpointer = ModelCheckpoint(文件路径='/ home / lidan / 3DFacePrediction / gene.face.autoencoder / gene.face.min.val_loss.hd5',monitor ='val_loss',verbose = 1,mode ='min',save_best_only =真的) 27

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / backend / tensorflow_backend.py 在symbolic_fn_wrapper(* args,** kwargs)中 73如果_SYMBOLIC_SCOPE.value: 74与get_graph()。as_default(): ---> 75 return func(* args,** kwargs) 其他76个: 77 return func(* args,** kwargs)

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / engine / training.py 在编译中(自我,优化器,损失,指标,loss_weights, sample_weight_mode,weighted_metrics,target_tensors,** kwargs) 第227节 228#层损失。 -> 229 self.total_loss = self._prepare_total_loss(掩码) 230 231#用于训练,测试和预测的功能

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / engine / training.py 在_prepare_total_loss中(自我,面具) 690 第691章 -> 692 y_true,y_pred,sample_weight = sample_weight) 693 第694章没钱了

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / losses.py在 致电(自我,y_true,y_pred,sample_weight) 69 scope_name ='lambda'如果self.name ==''else self.name 70与K.name_scope(scope_name): ---> 71损失= self.call(y_true,y_pred) 72 return loss_utils.compute_weighted_loss( 73个损失,sample_weight,reduction = self.reduction)

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / losses.py在 通话(self,y_true,y_pred) 每个样品130损失值。 131“”“ -> 132返回self.fn(y_true,y_pred,** self._fn_kwargs) 133 134 def get_config():

custom_loss中的

(y_true,y_pred) 3 def custom_loss(y_true,y_pred): 4 a =解码器(y_pred) ----> 5 b =解码器(y_true) 6 c = K.mean(K.square(a-b)) 7返回c

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / backend / tensorflow_backend.py 在symbolic_fn_wrapper(* args,** kwargs)中 73如果_SYMBOLIC_SCOPE.value: 74与get_graph()。as_default(): ---> 75 return func(* args,** kwargs) 其他76个: 77 return func(* args,** kwargs)

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / engine / base_layer.py 在通话中(自己,输入内容,**) 487#实际调用该图层, 488#收集输出,蒙版和形状。 -> 489输出= self.call(输入,** kwargs) (490)第490章 491

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / engine / network.py 通话中(自己,输入,掩码) 581返回self._output_tensor_cache [cache_key] 第582章 -> 583个output_tensors,_,_ = self.run_internal_graph(输入,掩码) 584返回output_tensors 585

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / engine / network.py 在run_internal_graph中(自己,输入,掩码) 第796章 797 [x._keras_shape for x in compute_tensors]) -> 798个形状= to_list(layer.compute_output_shape(input_shapes)) 第799章 800 [x._uses_learning_phase for x in compute_tensor]]

〜/ software / anaconda3 / lib / python3.7 / site-packages / keras / layers / core.py 在compute_output_shape(自己,input_shape)中 第915章 916断言input_shape和len(input_shape)> = 2 -> 917断言input_shape [-1] 第918章 919 output_shape [-1] = self.units

AssertionError:

我对错误信息感到困惑,因为解码器模型在y_pred中运行良好,而在y_true中失败。

有人可以帮我解决这个问题,还是只给我另一种方法将保存的解码器模型放入损失函数中?非常感谢你!

0 个答案:

没有答案