Model Keras中间层输出,用于计算损耗

Model Keras中间层输出,用于计算损耗,model,keras,theano,layer,loss,Model,Keras,Theano,Layer,Loss,我正在使用Keras和theano,来训练一个自动编码器模型。 我想使用模型的中间层表示,在损失函数(同一模型的自定义损失函数)中进行一些特定的计算 我怎么能这样做 我可以从模型中输出中间层。但随后将使用2个输出而不是1个输出对其进行训练。我只想使用最终输出来训练模型。我想我已经解决了这个问题 model1 = Model(input=x, output=y1) model2 = Model(input=x, output=[y2,y3]) model1.compile((optimizer=

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

我怎么能这样做


我可以从模型中输出中间层。但随后将使用2个输出而不是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\u loss\函数
访问
model1
的输出(
y1
)来计算损失。但是当我使用
model1.output[0]
inside
cutom\u loss\u 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.

请澄清:您想修改损耗,使其取决于网络的某些内部状态以及输出?确切地说..我想使用中间层+输出的内部特征表示来定义损耗函数..为此,我需要访问中间层的输出,作为模型但是像
classifier=Model(输入=输入,输出=[中间,最终]
这样的常规方法将训练网络的两种输出,我不需要这样做。