keras /张量流中的自定义损失函数

时间:2020-04-21 22:36:44

标签: python tensorflow machine-learning keras deep-learning

让我们假设我要构建一个custom loss function。该损耗函数应测量difference之间的two sets of weigths。假设我们有一个binary classificator。我们要从class0的权数中减去class1的谓词的权重。

问题:我如何在keras模型中提取重量,然后 减去那些重量?例如:

# define a composite model for updating generators by adversarial and cycle loss
def Model(generator,aux1,aux2,aux3,aux4,aux5 segmentation_model, image_shape):

  generator.trainable = True

  aux1.trainable=False
  aux2.trainable=False
  aux3.trainable=False
  aux4.trainable=False
  aux5.trainable=False

  segmentation_model.trainable=False

  # generated element
  input_B = Input(shape=image_shape)
  gen1_out = generator(input_B)

  # segmentation element, GAP = Global Activation Map (shape=(batch_size,channels))
  seg_out,GAP5,GAP4,GAP3,GAP2,GAP1=segmentation_model(gen1_out)

  # binary Aux-Classificator, output=[0,1]
  a1=aux1(GAP1)
  a2=aux2(GAP2)
  a3=aux3(GAP3)
  a4=aux4(GAP4)
  a5=aux5(GAP5)

  # ---> Now extract the weigths for class 0 and for class 1 and subtract them

  ...
  ...
  ...
  diff_a1= ....



  # define model
  model = Model([input_B] ,[seg_out,diff_a1,diff_a2,diff_a3,diff_a4,diff_a5])

  opt = Adam(lr=0.0002, beta_1=0.5)
  model.summary()
  return model

这里的lambda layer是合适的,还是我应该只使用custom layercall方法写一个output_shape

0 个答案:

没有答案