我构建了一个使用收缩损耗的单层自动编码器(显示了代码)。如何修改代码以合并多个隐藏层?我需要使用什么重量?
def contractive_loss(x_test, decoded_imgs):
#Calculating mean square error
mse = K.mean(K.square(x_test - decoded_imgs), axis=1)
#W = K.variable(value=model.get_layer('encoded').get_weights()[0])
# N x N_hidden
W = encoder.get_weights()[0] # N x N_hidden
W = K.transpose(W) # N_hidden x N
#h = model.get_layer('encoded').output
h = encoder.output
dh = h * (1 - h) # N_batch x N_hidden
# N_batch x N_hidden * N_hidden x 1 = N_batch x 1
contractive = lam * K.sum(dh**2 * K.sum(W**2, axis=1), axis=1)
return mse + contractive
autoencoder.compile(optimizer='adadelta', loss=contractive_loss)