Keras中间层输出来计算损耗

时间:2017-05-23 05:41:49

标签: model keras theano layer loss

我正在使用Keras和theano,来训练自动编码器模型。 我想使用模型的中间层表示在损失函数中做一些特定的计算(对同一模型的自定义损失函数)。

我怎么能这样做?

我可以从模型中输出中间层。但随后它将使用2个输出而不是一个输出进行训练。我想仅使用最终输出来训练模型。

1 个答案:

答案 0 :(得分:1)

我想我想出来了。

model1 = Model(input=x, output=y1)
model2 = Model(input=x, output=[y2,y3])

model1.compile((optimizer='sgd', loss=cutom_loss_function)
model2.compile((optimizer='sgd', loss=cutom_loss_function)

model2.fit(data, [targets2, targets3], , nb_epoch=epochs, batch_size=batch_size, verbose=2, shuffle=True, validation_split=0.1, callbacks=[checkpointer])

但是,我希望我的cutom_loss_function访问model1,(y1)的输出来计算损失。但是当我在model1.output[0]内使用cutom_loss_function()时,会出现以下错误。

ValueError: GpuElemwise. Input dimension mis-match. Input 1 (indices start at 0) has shape[2] == 48, but the output's size on that axis is 2304.
Apply node that caused the error: GpuElemwise{Composite{sqr((i0 - scalar_sigmoid((i1 + i2))))}}[(0, 1)](GpuDimShuffle{x,0,1,2}.0, GpuCorrMM{half, (1, 1)}.0, GpuReshape{4}.0)
Toposort index: 341
Inputs types: [CudaNdarrayType(float32, (True, False, False, False)), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, (True, True, True, True))]
Inputs shapes: [(1, 1, 2304, 2), (1, 1, 48, 48), (1, 1, 1, 1)]
Inputs strides: [(0, 0, 1, 2304), (0, 0, 48, 1), (0, 0, 0, 0)]
Inputs values: ['not shown', 'not shown', CudaNdarray([[[[ 0.]]]])]
Outputs clients: [[GpuReshape{2}(GpuElemwise{Composite{sqr((i0 - 
scalar_sigmoid((i1 + i2))))}}[(0, 1)].0, MakeVector{dtype='int64'}.0)]]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.