我使用Keras和theano,来训练一个自动编码器模型。我想使用模型的中间层表示在损失函数(同一模型的自定义损失函数)中进行一些特定的计算。
我怎么能这样做呢?
我可以从模型中输出中间层。但随后它将使用2个输出而不是1个输出进行训练。我想只使用最终的输出来训练模型。
发布于 2017-05-24 13:24:47
我想我想通了。
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)的输出来计算损失。但是当我在cutom_loss_function()中使用model1.output[0]时,它给出了下面的错误。
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.https://stackoverflow.com/questions/44126650
复制相似问题